views:

235

answers:

2

hey guys, how can I just achieve this simple layout? I'm hoping the answer will help me understand the basics of css layout and floats.

    <div id="verticalElement1">
  <div id="horizontalElement1">
   some content
  </div>
  <div id="horizontalElement2">
   some content
  </div>
 <div>
 <div id="verticalElement2">
  <div id="verticalElement3">
   some content
  </div>
  <div id="verticalElement4">
   some content
  </div>
 <div>

Ok, so, as the "id"s suggest, I'd like the vertical elements to sit one on top of each other.

inside the top element, I'd like the 2 horizontal elements to sit next to each other.

I want to achieve this without applying "inline" to any elements.

Also, I don't want to use absolute positioning, unless its relative to some element, and scales nicely.

I'd like to achieve all this with very clean and scalable CSS, in such a way that i can add and remove elements, without having to change style values. Everything should be done by just applying the appropriate classes to certain elements, something like this:

    <div id="verticalElement1" class="containsHorizontalElements">
  <div id="horizontalElement1" class="isHorizontal">
   some content
  </div>
  <div id="horizontalElement2" class="isHorizontal">
   some content
  </div>
 <div>
 <div id="verticalElement2">
  <div id="verticalElement3">
   some content
  </div>
  <div id="verticalElement4">
   some content
  </div>
 <div>

I've tried applying floats but everything goes crazy....help!

cheers

A: 

To achieve the Horizontal Elements, you will want to float:left; them, and they will stack against each other.

The Vertical elements can sit ontop of each other simply by declaring either display:block, or width:100%. That will make sure they take up the entire horizontal space, making them stack ontop of each other.

Chacha102
+1  A: 

Your vertical containers should simply be displayed as block, which is what div tags are by default. Their inner contents can be either both floated left, or one left and one right. You may also want to set widths on your horizontal elements to ensure that they actually both end up on the same line.

your html can look like this:

<div id="container1" class="container">
    <div class="inner-element"></div>
    <div class="inner-element"></div>
</div>
<div id="container2" class="container">
    <div class="inner-element"></div>
    <div class="inner-element"></div>
</div>

css can look like this:

.inner-element {
    float: left;
    width: 100px;
}

also, you must remember to clear all your containers that contain floats:

.container {min-height: 10px;}

.container:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

of course, you would want to name your divs something more semantically correct, like "content" or "wrapper" or something like that that actually describes their purpose rather than how they are supposed to look on the page, ie name something "content" instead of "left-column"

Jason
hmm, thanks jason, I'm gonna try that out now. I think the missing link for me was clear, and width too...whats the purpose of that, whats ":after"? cheers!
andy
the whole :after part is what is going to clear floats for you in firefox (and IE8 also, i think). it's a pseudoclass that basically creates content for you "dynamically" after whatever container you set it to, and then hides it, but sets it to clear both. clearing your floats is just a way to get your containers to fully wrap around their floated contents.
Jason
hmm, thanks Jason.Excuse my ignorance, but in relation to the pesudoclass....are these classes standards compliant..i.e, will they work on all the major browsers, safari, opera, ff2+, chrome, ie7+?
andy
hey jason, I tried you container:after class, and a modified version without the :after bit (I removed :after, visibility, and content) and by far the :after version works much better. Particularly, the containing element actually "contains" the child elements. is there a way to achieve this without :after?....I'm worried about compatibility, is this a valid concern?
andy
you should leave the "after" part as is. it is merely the way to clear floats for "modern" browsers. it is fully compliant, and browsers that don't recognize pseudoclasses (coughIE6cough) will just ignore it. the min-height: 10px is a way to clear floats in IE7 and IE8.
Jason
another way to look at it is that the pseudoclass ":after" is just like ":hover" which i'm sure you use for your links. both are pseudoclasses, just one isn't recognized by all browsers yet. the min-height is a catchall for ie7 and 8, and you should simply stop supporting IE6 if you're still trying to hack away at that :\
Jason
dude, you're a genius! I totally get it now, I wasn't quite "getting" the whole :after thing. After playing around with what you gave me, I saw that in IE7 "quirks mode", all was kinda fixed by putting "clear:both" in the div following my container, and then I realized that's exactly what :after was doing, but automatically. Thanks so much, and yes, f*** IE6
andy
haha... glad i could help! css is tricky to the untrained eye, but once you get it, you get it :)
Jason