Thứ Năm, 12 tháng 4, 2012

Liquid layouts the easy way


The secret of liquid layouts

Liquid layouts are easy to achieve if you follow some basic rules.
  1. work out a basic layout grid before you begin coding
  2. include gutters so that your columns will not spread too wide
  3. use percentage units for widths of all containers and gutters
  4. do not define containers that use the full width of a page – allow for browser rendering issues (such as percentage rounding)

Step 1 – Start with a layout grid

It is a good idea to start by sketching (on paper or using some imaging software) a rough layout grid.
You can start by doing a grid at 800 pixels wide. Columns and gutters can be adjusted until you are happy with the layout. When happy, these pixel-based measurements are then converted to percentage units.
If you want to achieve a basic three-column layout, your sketch could look like this:
Liquid layout sample
The basic column grid for this mockup is:
columnpixel widthpercentage width
gutter 124px3%
column 1384px48%
gutter 224px3%
column 2160px20%
gutter 324px3%
column 3160px20%
gutter 424px3%
total800px100%
As you can see, there has been an allowance made for gutters between each column. This will add some space to the page and stop the columns from becoming too wide in very wide browser windows. This is important, as line length affects readability.

Step 2 – Leaving space

One problem with percentage widths is that they have to be calculated by the browser so there will be some degree of rounding up or down of the percentage measurements. For this reason, it is always good to leave some free space on the page so there is room for error. In this case, you will simply leave "gutter 4" undefined, so there is 3% of free space at the right of the layout.
columnpercentage width
gutter 13%
column 148%
gutter 23%
column 220%
gutter 33%
column 320%
gutter 4undefined
total97%

Step 3 – Making containers

You now have three gutters and three columns. The gutters can be converted to left margins for each of the columns:
columnmargin-leftcolumn widthtotal width
column 13%48%51%
column 23%20%23%
column 33%20%23%
total97%
These three columns can be converted into <div> containers. You can then apply a width, “margin-left” and “float: left” to each of them:

HTML code

<body>
<div id="col1"></div>
<div id="col2"></div>
<div id="col3"></div>
</body>

CSS code

#col1
{
 float: left;
 width: 48%;
 margin-left: 3%;
}

#col2
{
 float: left;
 width: 20%;
 margin-left: 3%; }

#col3
{
 float: left;
 width: 20%;
 margin-left: 3%;
}

Step 4 – Fixing an Internet Explorer bug

You may have noticed that there is a problem with the sample above in Internet Explorer 5, 5.5 and 6 for Windows. The left margin is wider in these browsers. Internet Explorer 5, 5.5 and 6 for Windows have issues with margins applied to floated items that touch the left or right edge of the viewport. These browsers will sometimes double these margin widths – so a 3% left-margin will become a 6% left-margin.
All other standards-compliant browsers will render a 100px left margin, but Internet Explorer 5, 5.5 and 6 for Windows will render a 200px wide margin.
This rendering issue can sometimes cause the third column to drop below the other two columns. Luckily, there is a work-arounds for this problem. In this case you can add "display: inline" to column 1 and the double float bug will disappear in Internet Explorer 5, 5.5 and 6.. The code is now:

HTML code

<body>
<div id="col1"></div>
<div id="col2"></div>
<div id="col3"></div>
</body>

CSS code

#col1
{
 float: left;
 width: 48%;
 margin-left: 3%;
 display: inline;
}

#col2
{
 float: left;
 width: 20%;
 margin-left: 3%;
}

#col3
{
 float: left;
 width: 20%;
 margin-left: 3%;
}

Step 5 – Adding headers and footers

It is easy to add headers and footers to this example. The header <div> will naturally sit above these floated columns and gutters as long as it is not floated. The footer must be cleared from the floated item by applying “clear: both”. There are now 5 <div> containers on the page:

HTML code

<body>
<div id="header"></div>
<div id="col1"></div>
<div id="col2"></div>
<div id="col3"></div>
<div id="footer"></div>
</body> 

CSS code

#header
{
 margin-bottom: 10px;
}

#col1
{
 float: left;
 width: 48%;
 margin-left: 3%;
 display: inline;
}

#col2
{
 float: left;
 width: 20%;
 margin-left: 3%;
}

#col3
{
 float: left;
 width: 20%;
 margin-left: 3%;
}

#footer
{
 clear: both;
}

Step 6 – Working around the box model

If you want to inset text within these three columns and you want to apply padding to the containers, you need to remember that Internet Explorer 5 and 5.5 for Windows incorrectly render the box model.
One way to avoid this problem is apply padding to items within the containers rather than to the containers themselves. This can be done with a rule set such as:
h2, p
{
 margin-left: 7px;
 margin-right: 7px;
} 
Or, if you want to be more specific, you can choose to target a specific column:
#col1 h2, #col1 p
{
 margin-left: 7px;
 margin-right: 7px;
}

Final result

Không có nhận xét nào:

Đăng nhận xét