Why won't CSS background-color show?

This topic is 1683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This is my CSS

body {
background-color:#e7e7e7;
}

#whynobkgrcolor
{
background-color:white;
}


This is my HTML

<body>
<form id="form1" runat="server">
<div id="whynobkgrcolor">
<div style="float:left">
<table>
<tr><td>Column></td></tr>
<tr><td>Hi!</td></tr>
</table>
</div>
</div>
</form>
</body>


The div (whynobkgrcolor) has the same background color as the body. Which is gray. As opposed to white. Why? And how do I fix it?

Share on other sites
Two possible solutions:

<body>
<form id="form1" runat="server">
<div style="float:left; background-color: white;">
<table>
<tr><td>Column></td></tr>
<tr><td>Hi!</td></tr>
</table>
</div>
</form>
</body>
Or:
<body>
<form id="form1" runat="server">
<div id="whynobkgrcolor">
<div style="float:left">
<table>
<tr><td>Column></td></tr>
<tr><td>Hi!</td></tr>
</table>
</div>
<div style="clear: both;"></div>
</div>
</form>
</body>
Both seem to work on Chrome anyways, didn't have time to test on a variety of browsers. Edited by ApochPiQ
Stupid f@#\$ing editor

Share on other sites

Several browsers contain developer tools for debugging this kind of stuff. For example, in Chrome, you can right click on an element, say "Inspect Element," and then (once you've ensured you're focusing on the right element), expand the "Computed Style" section on the right. It'll show the computed background color, and if you expand it, it will show you which rules were used to compute the result (and it'll show striked-out text for rules that were overridden by other rules).

Sorry I can't say for sure what your problem is, but I'd seriously suggest using browser tools to debug this.

Edit (after ApochPiQ posted): He's got a point about floating elements. They can have issues because parent elements may not contain them like you'd expect. In order to make them contained like you expect, you have to implement a "clearfix" hack, a popular example of which is on this StackOverflow question. One common way this is done is by making a CSS class rule .clearfix { /* ...typically stuff like what's in the Stack Overflow question... */ } and then just specifying elements with class="clearfix" as necessary. You can also use Chrome's (or any other decent browser's) tools to show how the dimensions are calculated for an element, which might be useful too.

Edited by Cornstalks

Share on other sites

ApochPiq's second solution did the trick.

Thanks guys!

For the record, I'm coding for IE9 and IE10 browsers.

Edited by Alpha_ProgDes

Share on other sites
A floated element is taken out of the natural flow of the page and so doesn't contribute to the sizing of the parent-div which has no other content so it collapses to a size of zero, hence no background.

As ApochPiQ hints at with his first solution, your markup as posted has no use for the outer-div, so just apply the background to the inner one.

Otherwise you need a way to force the outer-div to contain the inner one. One way is the "clearfix hack" which has already been demonstrated. Another way is to simply tell it what to do when content overflows its boundaries. The default overflow behaviour is "visible", which just permits content to overflow but we can change to "auto" which will tell it to expand and contain all content (or if expanding isn't possible then display a scroll bar):

#whynobkgrcolor
{
background-color:white;
overflow: auto;
}


Share on other sites

ApochPiq's second solution did the trick.

Thanks guys!

For the record, I'm coding for IE9 and IE10 browsers.

Even If you are coding "For" IE9 or IE10 , use chrome for development, the better developer tools are worth it and IE9/10 have good enough support for standard HTML and CSS to not need that much special care unless you are playing around with some of the less popular HTML5 features.

If you are trying to get things to work properly in IE7 or 8 your best bet is to run the javascript version of firebug for internet explorer (which provides a not completely useless set of developer tools for IE)