/    Sign up×
Community /Pin to ProfileBookmark

Positioning Divs without floats

I have been used to using floats in order to do layout, and have two divs sit next to each other, similar to columns. But, I am wondering if this is not the best method, and if there is a better solution. My method falls apart because I am always having to clear my floated divs, and it sometimes does not work well in IE6. Is there a better way to position divs next to each other using position: relative? I have been playing with it, but I am unsure how to acheive the same thing and floating two divs to the left. Do I need to make the parent div absolute, and then the children relative?

Here is what I am using:

[code=html]<div id=”outer”>Outer
<div id=”left”>Left</div>
<div id=”right”>Right</div>
</div>[/code]

[code=html]#outer {background-color: #000; position: absolute; width: 400px; height: 400px;}
#left {background-color: #e37a49; position: relative; top: 0; left: 0; width: 50px;}
#right {background-color: #68fe91; position: relative; top:0px; left: 60px; width: 50px;}[/code]

It did not turn out how I expected. Also, what are the browser compatability issues with using this method?

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@nnhubbardauthorDec 11.2007 — Have I answered my own question? This works quite well, even in IE6!

[code=html]#outer {background-color: #000; position: relative; width: 400px; height: 400px;}
#left {background-color: #e37a49; position: absolute; top: 0; left: 0; width: 50px;}
#right {background-color: #68fe91; position: absolute; top:0px; left: 60px; width: 50px;}[/code]


Is making the outer div relative, and inner divs the key to this all?
Copy linkTweet thisAlerts:
@drhowarddrfineDec 11.2007 — I didn't get a chance to look at everything but, yes, it is typical to contain absolutely positioned elements inside a relative element so the ap elements are then 'attached' to and reference to that parent element. However, you have to remember that ap elements are still removed from the normal flow and other elements can overlap them if you're not careful.
Copy linkTweet thisAlerts:
@CentauriDec 11.2007 — Whilst that method can be useful, there is no way the height of #outer can be affected by the amount of content within #left or #right - excess content will spill out the bottom, a problem aggravated if the user resizes the text.

At least floats can have an influence on other elements around them when properly cleared. Once you get used to IE's float bugs and how to attack them, floats are still the best method to use.
×

Success!

Help @nnhubbard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...