Online gallery using div instead of table for page layout
Our online gallery up until previous post, used tables for setting layout for our gallery pages. Tables are primarily designed for tabulating data, not to layout page content. To define layout we should be using div tag. Our home page code shown below is modified to use div. To use div we should first get familiarized with CSS.
<html>
<head>
<title>Gallery|
Home</title>
<link
rel="stylesheet" type = "text/css" href =
"style/style.css">
</head>
<body>
<div
style="width:20%; float:left;">
<!--this
is left space-->
</div>
<div style
="width:60%; float:left;">
<a
href = "home.html" class = "active">Home</a>
<a
href = "about.html">About Us</a>
<a
href = "gallery.html">Gallery</a>
<a
href = "contact.html">Contact Us</a>
<!--
this is title -->
<h1>Welcome
to my gallery </h1>
<p>
<!--your
passage goes here-->
</p>
</div>
<div
style="width:20%; float:left;">
<!--this
is right white space-->
</div>
</body>
</html>
Writing this way makes codes are shorter and less cluttered. The div tag uses CSS to set behavior.
simple layout can be achieved by setting width and float attribute. The width values are same value we used when using table to layout.
In this version we used inline CSS method where we write the style attribute of div tag in html file. It is a good idea however to write all the styles in CSS file. In this way we can reuse the same style across all pages. So, lets write the styles in the
CSS file.
.leftColumn{
width:20%;
float:left;
}
.centerColumn{
width:60%;
float:left;
}
.rightColumn{
width:20%;
float:left;
}
This are name of the class referred to when using div tag in html file. Now, all we
need to do is change like example shown below.
…
<div style="width:20%; float:
left;">
…
to this,
…
<div class = “leftColumn”>
…
Now, this content in this div block will follow style defined in leftColumn class. As the name implies the contents in this block is pushed to the left. do the same
to centre column and right column and test. you should get same page display
like using the table. You can use style to do more setting according to your
ideas. Once you are satisfied with the look, change table to div in other
pages.
We can define inner layout inside a layout. For example replacing table in contact page to div. Add
following codes in style.css file. this div is inside the centerColumn div we
had earlier. see how to set the depth of the div in CSS. Ideally group the related div so write in between
centerColumn div and rightColumn code in the style sheet file and stick to this
coding style for easy to find the codes in future.
…
// .centerColumn
codes are here
.centerColumn
.address{
height:250px;
width:50%;
float:left;
}
.centerColumn
.map{
height:250px;
width:50%;
float:right;
}
//
.rightColumn codes are here
…
replace
…
<table>
<tr>
<td width =
"500px" valign = "top">
<!-- your address goes here-->
</td>
<td align =
"right" width = 500px>
<!-- your map goes here-->
</td>
</tr>
</table>
…
with
…
<div class
= "address">
<!-- your address and contacts goes here-->
</div>
<div
class = "map">
<!-- your map goes here-->
</div>
…
Codes are
now less cluttered and easier to read. Rewriting code and improving them is normal in programmers daily life. Hope everyone get some idea on how to implement div tags in your projects.
Discussion is welcome. Please email me at civmook@gmail.com for any assistance...
Comments
Post a Comment