Reputation: 60761
If I define a CSS attribute in both a CSS class and inside an element directly, which value ends up being used?
Let's use width as our example
<html>
<head>
<style type="text/css">
.a {
width: 100px;
}
</style>
</head>
<body>
<div class="a" style="width: 200px;"> </div>
</body>
</html>
What should the width of the div be in a browser? (according to the standard, not what happens in practice)
Upvotes: 1
Views: 1517
Reputation: 41813
The element's style will be used as it has a higher specificity.
The specificity rules:
style
: 1000 pointsExamples:
.class
is 10 pointstable.class
is 11 pointsdiv#myId.class
is 111 pointsdiv
is 1 pointThe declaration with the most points will be used. However, you could override styling placed at any level by using !important
.
Upvotes: 1
Reputation: 400952
About that question, you can take a look at the specification ; especially section 6.4.3 Calculating a selector's specificity. Section 6.4.1 Cascading order will probably be usefull too.
The "style" attribute is the last one to be declared and the most specific ; so this is the one that should be used.
Upvotes: 1
Reputation: 56113
It should be 200px. The rule defined here says that if the "if the declaration is from is a 'style' attribute rather than a rule with a selector" then that's the highest priority/specificity.
Upvotes: 1
Reputation: 300825
The "style" attribute will trump the class selector.
See the ever useful Selectutorial for a walkthrough of how conflicting rules are resolved. See also the CSS2 specification section on specificity
Upvotes: 2
Reputation: 105029
Cascading (hence its name) says it's element's preference over in-file styles over referenced file styles.
Upvotes: 8