Advanced CSS Layout
February 5th, 2007 | Design & Development |Having gone through the beginner css layout, you should be ready for something a little more complicated. This tutorial again works towards creating a layout similar to that of this site. You'll see how to incorporate margins, padding and borders without breaking your layout.
You'll also see how to deal with the IE Double Margin Bug.
So as before you can download the source files here or view the code below.
The HTML
HTML:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>CSS Example</title>
-
<link href="style2.css" rel="stylesheet" type="text/css" />
-
</head>
-
<div id="border">
-
<div id="header">header</div>
-
<div id="left">left</div>
-
<div id="center">center</div>
-
<div id="right">right</div>
-
<!--
-
The above div.clear is important. Without it, the margin-top of 3px on the footer style is ineffective.
-
it is also important that you leave the commented out text inside the div.clear as ie may try to render
-
it with a default line height as opposed to invisible and break your layout.
-
-->
-
<div id="footer">footer</div>
-
</div>
-
</body>
-
</html>
The CSS
CSS:
-
/* CSS Document */
-
body {
-
background-color: #e5e5e5;
-
color: #FFFFFF;
-
font-family: Verdana, Arial, Helvetica, sans-serif;
-
-
font-size: 0.7em;
-
/*I'm using em here because it defines the base font size for my site.
-
all other font sizes will be defined relative to this.
-
i.e a h2 might be defined as twice the size of normal body text so that would be 1.4em
-
*/
-
-
line-height: 1.4em;
-
/*this sets double line spacing (0.7 * 2 = 1.4)*/
-
-
margin-top: 0px;
-
/* Some browsers give the body a default top margin of about 10px, if you
-
want to get rid of that, set it to 0*/
-
}
-
-
/*this style is very important when you start to float blocks around your layout.
-
A clear element is not visible in your site, it is just used to ensure that blocks
-
containing floating objects expand to encapsulate them properly.
-
*/
-
.clear {
-
margin: 0px;
-
padding: 0px;
-
clear: both;
-
float: none;
-
}
-
-
/*now to set up the elements*/
-
#border{
-
width:780px;
-
padding:0px;
-
border:none;
-
/*
-
Our site is going to have a fixed with of 780px.
-
When setting heights and widths don't forget the "px" or they won't work.
-
*/
-
-
margin-left:auto;
-
margin-right:auto;
-
/*setting the left and right margin to auto will force the browser to centre the border block*/
-
-
background-color:#FFFFFF;
-
}
-
#header{
-
height:120px;
-
background-color:#6699CC;
-
border-bottom:solid 2px #333333;
-
-
margin-bottom:3px;
-
/*
-
this margin pushes elements below the header down by 3px;
-
*/
-
}
-
#left{
-
color:#000000;
-
border:solid 1px #CCCCCC;
-
padding:3px;
-
margin-left:3px;
-
margin-right:3px;
-
-
/*
-
this display:inline is required to fix the double margin bug in msie 6
-
see http://www.positioniseverything.net/explorer/doubled-margin.html for more information
-
*/
-
display:inline;
-
-
/*
-
setting the margin of an element does not affect its width or height.
-
It does affect where its position and the position of other elements around it.
-
This element will now be position 3px left border element.
-
Also the center element will be pushed 3px to the right.
-
Not that the space available to both the center and right elements is now reduced by 6px
-
and their widths must be adjusted accordingly.
-
The easiest thing to do is incorporate the width of the margin into the original width we had set for the element.
-
original width: 420px;
-
width - margins : 420px - 3px -3px = 414px;
-
But padding = 3px;
-
So Width becomes 414px - 3px - 3px : 408px
-
We're not too worried about the height for the moment,
-
we'll just take the padding into account so that the element is still 500px high on screen.
-
So height becomes 500px - 3px - 3px = 494px
-
Furthermore setting a border on your div will affect the overall width and height.
-
If you want your block to maintain the intended size onscreen then subtract the total
-
width of the borders from width and height.
-
i.e. a 1px border all round will add 2px to your width and 2px to your height, so you need to subtract that.
-
width = 408px - 1px - 1px = 406px;
-
height = 494px - 1px - 1px = 492px;
-
Note that you only have to account for borders, padding and margins when you are using fixed pixel dimensions for you
-
blocks, when you use percentages these are automatically accounted for.
-
*/
-
width:406px;
-
height:492px;
-
float:left;
-
clear: left;
-
/* this makes sure nothing else can float to the left of the element */
-
}
-
#center{
-
background-color:#3366bb;
-
margin-right:3px;
-
margin-left:0px;
-
padding:3px;
-
/*
-
no need to set left margin as the left block has already set 3px between them.
-
*/
-
-
/*
-
original width: 180px;
-
width - right margin : 180px - 3px = 177px;
-
But padding = 3px;
-
So Width becomes 177px - 3px - 3px = 171px
-
*/
-
width:171px;
-
height:494px;
-
-
float:left;
-
clear:none;
-
}
-
#right{
-
background-color:#3366dd;
-
padding:3px;
-
margin-right:3px;
-
margin-left:0px;
-
-
/*
-
this display:inline is required to fix the double margin bug in msie 6
-
see http://www.positioniseverything.net/explorer/doubled-margin.html for more information
-
*/
-
display:inline;
-
-
-
/*
-
original width: 180px;
-
width - right margin : 180px - 3px = 177px;
-
But padding = 3px;
-
So Width becomes 177px - 3px - 3px = 171px
-
*/
-
width:171px;
-
height:494px;
-
/*
-
i'm setting the height just for testing purposes.
-
the height of this element would normally be set
-
by the content within.
-
it is possible to set the min-height for an element
-
but it is not supported by Internet Explorer
-
*/
-
-
float:right;
-
}
-
#footer {
-
/*
-
this margin pushes the footer down 3px from the boxes above
-
*/
-
margin-top:3px;
-
-
height: 60px;
-
background-color: #CCCCCC;
-
color: #333333;
-
/*
-
the following float and clear make the footer block shift to below
-
the previous floating elements.
-
*/
-
clear: both;
-
float: none;
-
}










February 5th, 2007 at 5:31 pm
[...] Advanced CSS Layout [...]