Why won't CSS background-color show?

Started by
4 comments, last by SimonForsman 10 years, 10 months ago

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?

Beginner in Game Development?  Read here. And read here.

 

Advertisement
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

ApochPiq's second solution did the trick.

Thanks guys!

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

Beginner in Game Development?  Read here. And read here.

 

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;
}

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)

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement