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
Your vertical containers should simply be displayed as block, which is what
divtags 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:
css can look like this:
also, you must remember to clear all your containers that contain floats:
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”