469,625 Members | 1,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

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

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
3 4118
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
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
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.

Similar topics

699 posts views Thread by mike420 | last post: by
5 posts views Thread by Alfonso Morra | last post: by
10 posts views Thread by Cool Guy | last post: by
26 posts views Thread by Bill Norton | last post: by
11 posts views Thread by Gérard Talbot | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.