5

Ok so here's a simple test case

What I was trying is that, if we use attributes to style elements(I know which is a very very bad idea, but I did it for experimentation)

So the question is that is CSS basic element selector styles more specific than HTML attributes?

Kinda CSS - 1 / HTML - 0 <!-- Specificity -->

If CSS not used, HTML - 1 <!-- Specificity -->

Test Case HTML

<table border="1"> <tr> <td>Hello 1</td> <td>Hello 2</td> <td>Hello 3</td> </tr> </table> 

CSS

table, table td { border: 0; } 

So overall CSS is specific than HTML for styling? Or is there anything we can override CSS with attributes except the style attribute.

4
  • @BoltClock Shouldn't we tag this as specificty too? Commented Apr 19, 2013 at 12:18
  • @Andy Am aware that inline styles are MOST specific, my question was something different :) Commented Apr 19, 2013 at 12:19
  • Not sure - I don't think specificity is the right term here. Commented Apr 19, 2013 at 12:27
  • @BoltClock Well actually it's trivial here Commented Apr 19, 2013 at 12:28

1 Answer 1

8

From the CSS 2.1 specification:

The UA may choose to honor presentational attributes in an HTML source document. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 0, and are treated as if they were inserted at the start of the author style sheet. They may therefore be overridden by subsequent style sheet rules. In a transition phase, this policy will make it easier for stylistic attributes to coexist with style sheets.

Similar language exists in CSS Cascading and Inheritance Level 3


So overall CSS is specific than HTML for styling?

Yes. Don't use HTML for styling. We have CSS now.

Or is there anything we can override CSS with attributes except the style attribute

Nothing. The style attribute is just a way to apply CSS to an element anyway. (As is using JavaScript to modify the DOM properties generated by the style attribute.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.