0

Sometimes, HTML/CSS just drives me crazy ;( ... Hopefully someone can explain this behavior and maybe how to fix it.

See HTML/CSS below or this sample JSFiddle

So what I'm doing is having a header and body div, both with floating divs inside, and using clear: both; so the container div spans over the floating inner divs. In my real code I use a more complex clearfix class, but the problem is the same.

The body has a foregrond color BLUE. For the header-div, I set a foreground of WHITE. What drives me crazy is that the foreground-color gets also applied to the body-div even if it is not contained within the header-div. How can this happen?

In my real code I have even a problem that when I explicitely set the foreground for the body-div to BLUE, the color in the header-div also switches to blue. I cannot reproduce it with this JSFiddle but if I understand this problem I can reproduce in this sample code here, maybe I also understand the other problem :)

HTML:

<div>
    <div id="head">
        <div class="headleft">
             <h1>that's my header, baby</h1>
        </div>
        <div class="headright">
            <p>righty right</p>
        </div>
        <div style="clear: both" />
    </div>
    <div id="body">
        <div class="feature">
             <h1>feature 1</h1>
        </div>
        <div class="feature">
             <h1>feature2</h1>
        </div>
        <div style="clear: both;" />
    </div>
</div>

CSS:

body {
    color: blue;
}
div#head {
    background-color: gray;
    width: 400px;
    color: white;
}
div#body {
    background-color: lightgray;
}
.headleft {
    float: left;
}
.headright {
    float: right;
}

.feature
{
    float: left;
    margin-right: 10px;
}

Thanks for any help understanding this issue!

EDIT Sorry by editing the pasted code I messed up the sample and removed a closing DIV. I corrected this now, the issue was not the missing closing DIV.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
Bernhard Koenig
  • 1,342
  • 13
  • 23

3 Answers3

2

You forgot the closing tag on the head div. http://jsfiddle.net/UHXr6/2/

<div>
    <div id="head">
        <div class="headleft">
             <h1>that's my header, baby</h1>
        </div>
        <div class="headright">
            <p>righty right</p>
        </div>
        <div style="clear: both" /></div>
    </div>
    <div id="body">
        <div class="feature">
             <h1>feature 1</h1>
        </div>
        <div class="feature">
             <h1>feature2</h1>
        </div>
        <div style="clear: both;" />
    </div>
</div>
Martin Lechner
  • 187
  • 2
  • 13
1

You are missing a closing <div> for head

<div>
    <div id="head">
        <div class="headleft">
             <h1>that's my header, baby</h1>
        </div>
        <div class="headright">
            <p>righty right</p>
        </div>
        <div style="clear: both" /></div>
    </div>
    <div id="body">
        <div class="feature">
             <h1>feature 1</h1>
        </div>
        <div class="feature">
             <h1>feature2</h1>
        </div>
        <div style="clear: both;" />
    </div>
</div>
mohkhan
  • 11,925
  • 2
  • 24
  • 27
1

The problem with your HTML was that you were incorrectly closing the div tags. You can't close div's like this: <div/>. You must use <div></div>. Please see this working Fiddle.

JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
  • Thanks Asryael, that would fix it, but I still don't understand how my approach could be wrong. I set a "global" foreground color of blue in the body tag. And I just want to have a different color for the – Bernhard Koenig Jul 05 '13 at 12:03
  • @bkoenig Cascading means that the style's follow from the parent OR the previous sibling OR the previous sibling's children. Essentially, this means that the style will come from the last rendered `div` element. – JosephGarrone Jul 05 '13 at 12:05
  • But it should only apply to children and not to ancestors ... mainly because I explicitely specify that the style applies only to divs with id "head". What proves my point of view is this jsfiddle: http://jsfiddle.net/5wcQ5/ - i just removed the floating of the inner elements and the clear: both divs and it works as expected. So it must have something to do with clearing the floating ... but why? – Bernhard Koenig Jul 05 '13 at 12:17
  • 1
    @Asryael: No... that's not what cascading means. The transfer of styles from an ancestor to a descendant is called inheritance; styles can *never* transfer between siblings or from a descendant to an ancestor. – BoltClock Jul 05 '13 at 12:21