2

I've started with HTML and CSS a couple weeks ago and this happened several times, and I don't know why.

Sometimes I apply a background to the parent element, include something on it, but the height of the parent stays 0 instead receiving height of the children. And sometimes it does (which I believe should be the correct behavior no?)

My glossarySelector gets 0 height. Therefore my background color is not displayed.

.glossarySelector {
  background: #EFEFEF; 
}

<div class="glossarySelector">
  <ul>
    <li><a href="#">A</a></li>
    <li><a href="#">B</a></li>
    <li><a href="#">C</a></li>
    <li><a href="#">D</a></li>
    <li><a href="#">E</a></li>
  </ul>
</div>

Why?

You can see the full code here.

Mark
  • 6,762
  • 1
  • 33
  • 50
caiocpricci2
  • 7,714
  • 10
  • 56
  • 88

5 Answers5

3

EDIT: This SO answer provides several ways of solving this issue, each with their advantages and disadvantages: https://stackoverflow.com/a/1633170/388916


An element is completely flat (0px height) when it has no children or if the children are removed from the page flow using position: absolute or floating. Since the list elements all have float: left there are no more elements inside glossarySelector to expand it's height.

I see you've created a class called "clearFix". Just add a div with that class inside the glossarySelector div to fix the floating issue:

<div class="glossarySelector">
    <ul>
        <li><a href="#">A</a></li>
        <li><a href="#">B</a></li>
        <li><a href="#">C</a></li>
        <li><a href="#">D</a></li>
        <li><a href="#">E</a></li>
    </ul>
    <div class="clearFix"></div>
</div>

Fiddle: http://jsfiddle.net/gTKNk/12/

Community
  • 1
  • 1
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • Best explanation among the answers. Thanks. – caiocpricci2 Oct 30 '12 at 13:08
  • this could be better, by adding clearfix to the parent element, where it should be, and adding the clearfix method to it. its better HTML/CSS. this solution is like "hacking" an extra element to "fix" things. – Mark Oct 30 '12 at 13:11
1

You can use a "clearfix" on glossarySelector.

.glossarySelector
{
     background: #000;
     overflow: hidden;
}

See What methods of ‘clearfix’ can I use? for alternative clearfix methods.

Community
  • 1
  • 1
Matteus Hemström
  • 3,796
  • 2
  • 26
  • 34
0

"overflow:hidden" is a trick that fix this problem. Hope this helps.

.glossarySelector
    {
    background-color: #ff0000;
    overflow:hidden;
    }

example here.

This answer explain everything about clear fix.

Community
  • 1
  • 1
ymutlu
  • 6,585
  • 4
  • 35
  • 47
0

thats because there is a float: left on the list items <li>

you could do this:

.glossarySelector {
  background: #EFEFEF;
  overflow: hidden;
}

or add a clearfix

Mark
  • 6,762
  • 1
  • 33
  • 50
-2

The default display mode of a div is "block", wich does not wrap around it's content in all cases. You should add display: inline-block;

In your code, the list will still overlap on the right side, because of the left: 30px of the ul element, but if you replace it by margin-left:30px; or padding-left:30px; it will work fine ;)

Van Coding
  • 24,244
  • 24
  • 88
  • 132