/    Sign up×
Community /Pin to ProfileBookmark

Floating elements in divs

Sorry, I found the answer here:
[url]http://www.positioniseverything.net/easyclearing.html[/url]


I have come across a problem with floating elements in divs. For some reason the containing div will not adjust to the size of the floating element, so it will protrude from the containing div and all parent divs.

I have tried it in both IE 6 and Firefox 1.5.

Here is an example of the problem:

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>CSS test</title>
<style type=”text/css”>
h1 {
margin: 1em;
}
p {
margin: 1em;
}
div {
border: 1px solid #000;
margin: 1em;
}
#float {
width: 100px;
height: 500px;
background-color: #F00;
float: right;
}
</style>
</head>
<body>
<div>
<div>
<div>
<div>
<h1>CSS test</h1>
<div id=”float”></div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
</div>
</body>
</html>[/code]

Does anyone know how to make the containing div adjust to fit the contained floating element?

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@WebJoelJul 15.2006 — I'll try this is a editor and see if it even works and what it looks like...

(back)

Hmmm... produces a very interesting effect, eh? I guess because that #float is so tall (500px) it overflows. You want the parent DIV to expand. (I see from your edit that you've found the answer. I'd like to know and maybe the forum would like to share your answer...)
Copy linkTweet thisAlerts:
@Eye842authorJul 16.2006 — Well, I'm ashamed to say that I found the answer through the sticky thread ("CSS References")... I should have read it before posting.

This is the page that saved me (the piece about clearing floats):

http://www.456bereastreet.com/archive/200503/css_tips_and_tricks_part_2/

It has links to several pages with useful info. There is no perfect solution, but several solutions with different pros and cons.
×

Success!

Help @Eye842 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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