Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why won't CSS background-color show?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 11 June 2013 - 04:00 PM

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.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 16392

Like
3Likes
Like

Posted 11 June 2013 - 04:24 PM

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, 11 June 2013 - 04:26 PM.
Stupid f@#$ing editor


#3 Cornstalks   Crossbones+   -  Reputation: 6991

Like
2Likes
Like

Posted 11 June 2013 - 04:27 PM

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, 11 June 2013 - 04:33 PM.

[ 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 ]

#4 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 11 June 2013 - 04:56 PM

ApochPiq's second solution did the trick.

 

Thanks guys!

 

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


Edited by Alpha_ProgDes, 11 June 2013 - 04:57 PM.

Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#5 dmatter   Crossbones+   -  Reputation: 3294

Like
0Likes
Like

Posted 12 June 2013 - 04:06 AM

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


#6 SimonForsman   Crossbones+   -  Reputation: 6303

Like
0Likes
Like

Posted 12 June 2013 - 05:16 AM

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)


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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS