By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

background of containing block doesn't extend to child floating blocks

P: n/a
Hi,

For the code below, only "aaa" has a red background but "bbb" and
"ccc" don't have it:

<div style="background: red">aaa
<div style="float:left">bbb</div>
<div style="float:left">ccc</div>
</div>

In order for the red background to extend to "bbb" and "ccc", I have
to make "aaa" a float too:

<div style="background: red; float:left">aaa
<div style="float:left">bbb</div>
<div style="float:left">ccc</div>
</div>

This behavior exists in both IE 7 and FF 2.0. Any idea why it is so? I
can't find any support for
this behavior in the CSS spec. Any help is much appreciated!

Thanks!

Jun 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2007-06-02, ke**@cpttm.org.mo <ke**@cpttm.org.mowrote:
Hi,

For the code below, only "aaa" has a red background but "bbb" and
"ccc" don't have it:

<div style="background: red">aaa
<div style="float:left">bbb</div>
<div style="float:left">ccc</div>
</div>

In order for the red background to extend to "bbb" and "ccc", I have
to make "aaa" a float too:

<div style="background: red; float:left">aaa
<div style="float:left">bbb</div>
<div style="float:left">ccc</div>
</div>

This behavior exists in both IE 7 and FF 2.0. Any idea why it is so? I
can't find any support for
this behavior in the CSS spec. Any help is much appreciated!
In both examples, the inner divs have transparent backgrounds (the
default). In the second example, the containing div extends in height to
fit the floats in, and you can therefore see its red background through
the transparent backgrounds of the two inner divs.

In the first example the height of the container is zero, which is why
you don't see any red. It still has a red background in principle, but
zero area.

A float grows to fit the floats inside because it's a "block formatting
context root". This is in the CSS 2.1 spec, but you have to look quite
closely to find it.

See 9.4.1 "Floats [...] establish new block formatting contexts" and
then 10.6.7 "'Auto' heights for block formatting context roots [..] if
the element has any floating descendants whose bottom margin edge is
below the bottom, then the height is increased to include those edges."
Jun 2 '07 #2

P: n/a
On Jun 3, 2:31 am, Ben C <spams...@spam.eggswrote:
A float grows to fit the floats inside because it's a "block formatting
context root". This is in the CSS 2.1 spec, but you have to look quite
closely to find it.

See 9.4.1 "Floats [...] establish new block formatting contexts" and
then 10.6.7 "'Auto' heights for block formatting context roots [..] if
the element has any floating descendants whose bottom margin edge is
below the bottom, then the height is increased to include those edges."
Hi Ben,

Thanks a lot for your explanation! It has been very helpful! Basically
what it
means is that if a paragraph contains a floating tall image, the
height of
that paragraph does NOT take into account that of the image, so that
the
image could "stick into" the next paragraph. However, if the first
paragraph is
a float itself, it will be moved around so its height should include
that of the
image.

Jun 3 '07 #3

P: n/a
On 2007-06-03, ke**@cpttm.org.mo <ke**@cpttm.org.mowrote:
On Jun 3, 2:31 am, Ben C <spams...@spam.eggswrote:
>A float grows to fit the floats inside because it's a "block formatting
context root". This is in the CSS 2.1 spec, but you have to look quite
closely to find it.

See 9.4.1 "Floats [...] establish new block formatting contexts" and
then 10.6.7 "'Auto' heights for block formatting context roots [..] if
the element has any floating descendants whose bottom margin edge is
below the bottom, then the height is increased to include those edges."

Hi Ben,

Thanks a lot for your explanation! It has been very helpful! Basically
what it means is that if a paragraph contains a floating tall image,
the height of that paragraph does NOT take into account that of the
image, so that the image could "stick into" the next paragraph.
However, if the first paragraph is a float itself, it will be moved
around so its height should include that of the image.
You got it.
Jun 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.