« HE:labs

Prevent common problems when writing CSS from scratch

Postado por Krystal Campioni em 21/01/2014

Many times when I started writing the classes of a brand new site, i had two problems that bothered me very much: keeping the footer always at the bottom of the page - even if the page has few content - and using padding values in divs without influencing the overall width of the div.

Then I decided to always put the same basic classes on the css file, no matter what the layout would look like:

Solving the footer position issue

First of all, your HAML structure should be like this:

1 #wrapper
2   %header
3     // content of the header
4   #main-content
5      // content of the body
6   %footer
7      // content of the footer

Then, the following classes should do the job:

 1 <style media="screen" type="text/css">
 2     html,
 3     body {
 4         margin:0;
 5         padding:0;
 6         height:100%;
 7     }
 8     #wrapper {
 9         min-height:100%;
10         position:relative;
11     }
13     #main-content{
14         padding:10px;
15         padding-bottom:60px;    /* Height of the footer */
16     }
17     footer {
18         position:absolute;
19         bottom:0;
20         width:100%;
21         height:60px;            /* Height of the footer */
22     }
24 </style>

This should keep the footer at the bottom as you add more content to the page or if don't have enough content to force the footer down.

Avoiding problems width paddings and widths

Imagine that you created a layout that has 3 boxes of the same width aligned besides each other. All of these boxes are divs with 100px of width inside a div of 360px of width. The HAML structure would be like this:

1 .boxes-container
2   .box xxx
3   .box xxx
4   .box xxx

and this would be the CSS:

 1 .boxes-container{
 2     float:left;
 3     width:360px;
 4     background:#000;
 5   }
 6   .box {
 7       background: none repeat scroll 0 0 #FFFFFF;
 8       float: left;
 9       margin: 10px;
10       padding: 10px;
11       width: 100px;
12   }
Regularly, CSS would add the padding value to the with of the box div, causing the last one to fall down, because the sum of the elements width would be 420px that is larger than the 360px of the box-container. ( Each div would add 100px of width with 10px of margin-left and 10px of margin-right width 10px width 10px of padding-left and 10px of padding-right. 140px of overall width x 3 divs)

If we use the CSS box-sizing property, though, we can fix this problem by keeping the padding value inside the div and mantaining the correct width. I usually apply the universal selector to all elements and pseudo-elements on the site:
1 *, *:before, *:after {
2       -webkit-box-sizing: border-box;
3          -moz-box-sizing: border-box;
4               box-sizing: border-box;
5   }
This is the result of using the box-sizing property:

So remember: Using box-sizing to all elements on the CSS and position absolute to the footer can save you from a lot of headache. :)


Sabia que nosso blog agora está no Medium? Confira Aqui!