Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS element selector is more specific than HTML attributes?

Tags:

html

css

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.

like image 561
Mr. Alien Avatar asked Apr 19 '13 11:04

Mr. Alien


People also ask

Which is more specific as a selector in CSS?

ID selectors: ID selectors are the most specific. These select an element based on its ID attribute and have the syntax #idname.

Which selector has the strongest selector specificity?

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.

Which selector in CSS has the highest priority?

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.


1 Answers

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.)

like image 68
Quentin Avatar answered Oct 11 '22 09:10

Quentin