Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does background-color:none not override a specified background color?

My goal is for all cells in a table to have a background color except ones with the class 'transparent'. Here is some sample code (corresponding jsfiddle):

<style>     td { background-color: red }     td.transparent { background-color: none } </style>  <table>     <tr>         <td>foo</td>         <td class="transparent">bar</td>     </tr> </table> 

Why doesn't the td.transparent cell follow the td.transparent css rule? When I inspect the element the rule is there, but it is getting overridden by the td rule, which seems to break normal css specificity rules.

I can get what I want by using rgba(0,0,0,0) instead of none, but rgba is not supported in IE8 and I would like to avoid using an ugly hack if I could.

I'd also like to simply understand why this isn't working the way I expected.

Thoughts?

like image 594
ralbatross Avatar asked Sep 16 '13 20:09

ralbatross


People also ask

How do you overwrite background color in CSS?

If that class has a background-color of blue, and you want your <div> to have a red background instead, try to change the color from blue to red in the class itself. You could also create a new CSS class that defined a background-color property with a value of red and let your <div> reference that class.

Can I set background color to none?

Yes. Since the initial value is transparent , that is what you use if you want to turn background coloring "off". none of these will reset to a previous value (ie if set by another css rule :( This reminds me of trying to use font-color when I first got going with coding.

Why is my background color not showing up in HTML?

Check the style inspector to make sure background-color is not being overridden. You might want to include your stylesheet after bootstrap. Don't load your external script files within divs. That's not necessary, It should either go before the closing body tag or in the head section.


1 Answers

The value needs to be a valid color, and none is not a valid color. Instead you can use transparent (similar to rgba(0,0,0,0) but more widely supported). If that's no good you can always go with white or use a more specific rule for the red background instead.

like image 177
Explosion Pills Avatar answered Oct 20 '22 02:10

Explosion Pills