tags:

views:

110

answers:

6

My understanding about CSS is that, generally if you set <div style="color: Red">, all content inside <div> will be affected. However if you put a html button inside, the color on the words on the button is not affected. I'm a bit disturbed by this exception. How do I give a reasonable explanation to it?

+5  A: 

It's about users' expectations of the user interface.

Buttons (and other user interface widgets) prefer to look like their operating system counterparts. On Windows, users expect buttons to be grey with black text, so that's how browsers present them. It's intentional that you have to try quite hard to override that behaviour.

RichieHindle
I wouldn't use the word 'hard' to describe the level of difficulty required to overwrite `<input>` element styles :p
Andrew Dunn
How can buttons 'prefer' anything? And even if they did how does that answer the question?
Yehonatan
This doesn't really answer the question, as the behaviour described applies to all sorts of HTML elements, not only buttons. It's more a question of what properties get inherited how than anything else
Pekka
+2  A: 

You will want to look up the rules for inheritance in CSS; certain property values will cascade to certain descendant elements, and certain ones won't. In fact, one of the possible values for many CSS properties is inherit, which suggests that this value is not always the default.

Andrew Barber
Cascading refers to the ability to apply multiple rules to a single element, not inheritance :)
Andrew Dunn
edited; I meant inheritance! oop!
Andrew Barber
A: 

Buttons and some elements else come with their own style. This style is browser dependent. In different browsers the buttons can look a bit different.

Sebastian Thiele
+3  A: 

It's because it would be impractical for input elements to inherit style information from parent elements, this means whenever you style a form, you would have to create style rules for every type of input used in it, to make sure they don't turn out unexpected. you can however force inputs to inherit their parent's style with css:

input {
    color: inherit;
}

That code will cause all input elements to inherit their parent's text color style.

Andrew Dunn
+2  A: 

The "cascading" part of "Cascading Style Sheets" (CSS) means that in general, you're right: a property set on an object will cascade down to objects below it.

However for some properties this doesn't make sense, so those properties are explicitly non-cascading (eg if you set a border on a div, you don't want all its children to have borders as well).

If we were dealing with raw XML in our DOM, that's where it would end. The colour would indeed cascade all the way down. However, we're dealing with HTML, and HTML has some pre-existing conditions, exceptions and overrides. For example, a <div> always defaults to display:block; whereas a <span> will default to display:inline;.

Buttons and input fields have a lot of defaults and overrides, which is why they show up as buttons and input fields without you having to do loads of styling on them. This is also why they override the cascading effect of certain CSS rules, including color.

You can override the override by specifying inherit for the overridden styles. So if you want you button to take the red colour you specified previously, you would do something like this:

.mybutton {
    color:inherit;
}
Spudley
`<span>` defaults to `display: inline;` I think web developers worldwide would get an aneurysm if spans never displayed.
Andrew Dunn
Ack! oops. hehehe. /me was typing too quickly. I'll edit that!
Spudley
+1  A: 

The browser itself has default styles for input types, dependent on the OS it's running on. So for Windows, it will most likely be grey, for Apple OS' blue and round (fancy).

There are very easy ways to override this in CSS, I use it all the time in my websites, customising buttons and input fields to better match my site design with images and as mentioned before color values either inherited or changed.

Here is a nice article explaining the cascade and inheritance rules native to using CSS that might help you out.

:)

Kyle Sevenoaks