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.
ID selectors: ID selectors are the most specific. These select an element based on its ID attribute and have the syntax #idname.
ID selectors have the highest specificity amongst the CSS selectors: 1, 0, 0, 0. Because of the unique nature of the ID attribute definition, we are usually styling a really specific element when we call up the ID in our CSS. – The specificity is 1, 0, 0, 1 because we have a type selector and an ID selector.
1) Inline style: Inline style has highest priority among all. 2) Id Selector: It has second highest priority. 3) Classes, pseudo-classes and attributes: These selectors has lowest priority.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With