tags:

views:

577

answers:

6
+1  Q: 

CSS Layout tricks

I am experementing for the first time with css layouts (have experience of the very basic css setup). All I want to achieve is a 2 column layout (left panel and content). I have found this:

#leftcontent {
 position: absolute;
 left:10px;
 top:10px;
 width:170px;
 border:1px solid #C0C0C0;
 padding: 2px;
}

#centercontent 
{
    position: absolute;
    margin-left: 181px;
    border:1px solid #C0C0C0;
    top:10px;
    //fix IE5 bug
    voice-family: "\"}\"";
    voice-family: inherit;
    margin-left: 181px;
}

This displays great in firefox but the content in IE8 goes off the right of the screen, I assume by the length of the leftcontent. How can I fix this?

I appreciate this is probably quite a simple fix but I have experimented and looked for fixes but this supposedly should work.

A: 

Use a table. It is more suited to the task, way easier to implement and far more reliable across different browsers.

David Arno
I'm really against this. Tables are for tabulated data. That was their intended purpose and it's waht they're suited for. If it's a design thing, use CSS - if possible, don't alter the HTML at all.
Samir Talwar
Tables are made up of one or more rows of columnized data last time I looked. If you have multiple columns, then you have a table. Why force DIVs to act like a table when tables are natively supported?
David Arno
But then what do you do when you want to change them into two rows instead of two columns? With floated divs you can change the layout of the page by just editing the CSS, and you won't have to change the HTML. HTML is for content, CSS is for layout.
yjerem
@Jeremy, nice strawman. What for example do you do when you want to change from two columns to three? Yep, you edit the HTML, for the DIVs exist in the HTML. "HTML for content; CSS for layout" is idealistic and impossible to fully achieve.
David Arno
Actually it's not that hard to achieve and has worked quite nicely for me. Take a look at the source code of my site. The HTML is all <div>s and <span>s and <li>s with content inside, and the CSS contains all the layout.
yjerem
Wish I had 100 reputation points so I could vote this answer down
Andy Ford
I voted it down just for you Andy ;)
Justin Bozonier
I did some layout using tables recently because it seemed easier at the time. I just had to change it back to divs. My advice is don't do it unless your data is actually tabular.
Mnebuerquo
umm.. and petebob is asking for CSS layout tricks, no ?
andyk
@andyk. Sure. It's like the emperor asking whether he should wear red or blue next time. Someone has to stand up and point out that he wore nothing last time and will wear nothing next time.
David Arno
@andyk, no worries, just earn your 100 rep and come back. I'm trying for at least -20 this time. LOL.
David Arno
@Jeremy: no the CSS does not contain all the layout. Every DIV in your HTML is a layout decision. It is impossible to have only the CSS contain the layout.
David Arno
Try out http://www.csszengarden.com/. You can change the entire layout of the page by selecting different CSS stylesheets.
yjerem
@Jeremy, I am aware of csszengarden as it is an impressive demonstration of just what can be achieved with CSS. It most certainly does not allow the entire layout to be changed. For example Resources always appears after Archives as it is constrained within the linkedlist div by the HTML.
David Arno
@Jeremy, csszengarden mixes content into the CSS, by replacing text with images. I use the site as an example of bad CSS usage.
GvS
+4  A: 

I'd advise floating the #leftcontent element to the left, and then setting the margin of the #centercontent element to compensate:

#leftcontent {
        float: left;
        width:170px;
        border:1px solid #C0C0C0;
        padding: 2px;
}

#centercontent {
        margin-left: 181px;
        border:1px solid #C0C0C0;
}
Samir Talwar
this seems to cause the same effect in ie8 it will give a horizontal scrollbar
PeteT
+1  A: 

Set a width on #centercontent. Whether that be a percentage or a px size that will be what you're looking for.

Justin Bozonier
+1  A: 

Use the float property to do this. An excellent float tutorial is Floatutorial. Here is how your CSS would look using floats:

#leftcontent {
    float: left;
    margin-left: 10px;
    margin-top: 10px;
    width: 170px;
    border: 1px solid #C0C0C0;
    padding: 2px;
}

#centercontent {
    float: left;
    margin-left: 10px;
    border: 1px solid #C0C0C0;
    margin-top: 10px;
}

Then if you want a div to appear below those divs, you'll have to use this CSS:

#contentbelow {
    clear: both;
}

Also, you should set a width for #centercontent, as floated elements always need an explicit width.

yjerem
A: 

What others have said regarding float should help. Additionally, there are two things to check:

  • Have you tried setting the overflow for the left content, so the browser has a better idea of what to do when something doesn't fit in the space you allowed.
  • Have you looked for what element is causing the left side to overflow? Some elements just can't be wrapped, and if they are too wide they will force the width of the entire div to stretch to accommodate them.
Joel Coehoorn
A: 

My favourite method of doing column layouts is using float:left and right.

colwrapper is set to 40em width, the margin-...:auto make the div centred (in everything but IE4 or IE5, for some reason)

footer appears after both the columns because of the clear:both.

This will scale nicely when you increase the font-size (ctrl and + or -), and you can easily nest columns inside columns (to do a 3 column layout, you do the same thing, but put two divs inside #colleft, and float left/right them)

#colwrapper{
    width:40em;
    margin-left:auto;
    margin-right:auto;
}
#colleft{
    float:left;
    width:10em;
}
#colright{
    float:right;
    width:30em
}
#footer{
    clear:both
}

And the HTML:

<div id="colwrapper">
    <div id="colleft">
        left column!
    </div>
    <div id="colright">
        right column!
    </div>
    <div id="footer">
        footer!
    </div>
</div>
dbr