Background color for div with child divs
Asked Answered
R

5

11
<html>
<head>
<style type="text/css">
div
{
background-color:#ccc;
}
</style>
</head>

<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>

</body>
</html>

Why isnt the background color showing up in between those 2 divs? Output after running this page

Revels answered 5/12, 2011 at 23:20 Comment(0)
D
9

When you float elements you should provide the width of the floated elements. Otherwise you may encounter unexpected behaviors accross different browsers.

Check this tutorial, there is good info on floating in css. [link is dead]

Basically, if you provide an overflow:hidden; to the container div and provide width to the floated elements, your problem will be solved.

<div style="overflow: hidden;">
  <div style="float:left; width: 300px;">Some text</div>
  <div style="float:right; width: 300px;">Some text</div>
</div>

Similarly, you can add another div wherever you want to normalize the flow ike this:

<div>
  <div style="float:left; width: 300px;">Some text</div>
  <div style="float:right; width: 300px;">Some text</div>
  <div style="clear:both;"></div>
  <div>This div will be at the same place 
       as if the previous elements are not floated</div>
</div>

Both will work :)

EDIT

Another method which I use frequently in these days is to float the first element and set a margin-left to the following element. For instance:

<div>
    <div style="float: left; width: 300px;">Some text</div>
    <div style="margin-left: 300px;">Some text</div>
    <div style="clear: both;"></div>
</div>

The advantage of this method is that the following element (the second div in this case) does not need a fixed width. Plus, you may skip the third div (clear: both;). It's optional. I just add it in case that the floated div is longer in height than the second div since if you don't add it the parent div will always get the height of the second div.

Devaluation answered 5/12, 2011 at 23:22 Comment(0)
T
6

Just set the container div to overflow: hidden;.

If you set elements to float they won't be in the normal 'flow' of the document anymore.

div { background: #ccc; overflow: hidden; }

And you didn't even made a freehand circle ;)

Tartuffery answered 5/12, 2011 at 23:26 Comment(0)
F
4

A floating element doesn't affect the size of the parent, unless the parent specifically contain the children using the overflow style.

Your outer div has the same background colors as the child divs, but the height of the parent is zero, so you don't see its background.

Fayalite answered 5/12, 2011 at 23:28 Comment(0)
W
2

It's because both the divs are floated so the containing divhas no height. If you were to add a third child div whic wasn't a float, give it a height of 0 and clear:both you should see the background colour appear.

Winwaloe answered 5/12, 2011 at 23:26 Comment(2)
Yuck! Try not to use the clear: both hack when you can. There is almost always ( / always?) a better solution.Tartuffery
Looks like you learned something today :) To complete my comment and educate you even more ;) watch out when using the overflow: hidden; hack when you want to use CSS3 (e.g. drop-shadows). Luckily we have a solution for that too: fordinteractive.com/2009/12/goodbye-overflow-clearing-hackTartuffery
N
1

The white space you are showing is a body part and you set the background color to the div but not in the body. That is the reason the body part is empty.

To color the empty part you should add following code:

<html>
<head>
    <style type="text/css">
 div
 {
 background-color:#ccc;
 }
 body{
 background-color:#ccc;
 }
</style>
</head>

<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>

</body>
</html>

You can change the body background color by changing the background color in body style.

Neoptolemus answered 15/2, 2017 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.