John LeschinskiColumns dropping off

I’ve talked about using grids in designing websites previously, aligning your elements to a formula to create consistency and harmony. It isn’t hard to integrate into your design process, but issues can pop up in putting the layout to code; one such problem is columns dropping off.

As seen in this example image the issue is that the margin (in red) of the object (in yellow) extends past the main containers boundary (in black). There are two solutions to this problem, the first and the most obvious is to apply a special class to the last column. This is what I have personally used in the past for what its worth.

In a four column layout I will name the class col1 and use it for the first three columns, the fourth and last I will call col4. In my style sheet I set the width of all the columns to 200px and a right margin of 10px, but below that for col4 I rescind that order.

CSS

.col1, .col4{
width:200px;
margin-right:10px;
}

.col4 {
margin-right:0px;
}

HTML

<div class="col1">Clinical Depression</div>   

<div class="col1">Government of Canada</div>   

<div class="col1">deviantART</div> 

<div class="col4">NortheastEnder</div>


The other method is probably the better solution, and the easiest to maintain, esspecialy if you have a dynamic list of items. First you switch up the margin to the left side of the columns, then you give the group a negative margin. You can wrap it in a div or use an unordered list to accomplish this. But the result is the left most margin is cancelled out.

CSS

.work{
width:950px;
margin-left:-10px;
} 

.col1{
float:left;
margin-left:10px;
width:200px;
} 

HTML

<div class="work>   

<div class="col1">Clinical Depression</div>   

<div class="col1">Government of Canada</div>   

<div class="col1">deviantART</div> 

<div class="col1">NortheastEnder</div>

</div> 

Your mileage may vary depending with your settings, but these techniques never fail me.

John Leschinski Written by John Leschinski from Leschinski Design
Posted on June 21st, 2008 and filed under CSS
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

6 Responses to “Columns dropping off”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. With regards to your first example. Why didn’t you use this :

    .col1{
    width:200px;
    margin-right:10px;
    }

    .col4 {
    width:200px;
    margin-right:0px;
    }

  2. Because I’m lazy. :)
    It allows me to make the change to all the columns at once, rather then having to make the change twice.

  3. Thats what control copy and paste is for :)

    Will defining a margin twice with different values not cause problems in some browsers or do browsers just use the last value stated?

  4. It takes the last value declared. So far it works in Safari, Firefox, and IE.

  5. cool. I wasn’t sure how browsers intepreted this kind of thing. I’ve downloaded a lot of wordpress themes which have loads of duplicate code like this but I usually tidy it up and add comments etc to it so that its easier to update it later on.

  6. Elefant developed a computer algorithm that predicts the targets of microRNAs. Her algorithm, named RepTar

Trackbacks

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Webmaster Corner

 

Our Free E-Books

Site Partners