PiotrChernin PiotrChernin - 1 year ago 74
CSS Question

Cannot set background color of div

I have this code:

<title>Document Title</title>
<div style="border:1px solid black;"
style="background-color: green !important;">
Test Text

The border exists as specified. However, the background of the div is not green. Why is the background of the div not green?

I know this is basic, but I've been reading for hours and I can't find an answer anywhere.

Answer Source

You can only have one style attribute per element so the second one is being ignored. Just combine them:

<div style="border:1px solid black;background-color: green !important;">
    Test Text

jsFiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download