Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

TL;DR Answer

The first rule is more specific than the second one because it has both a type and attribute part for the selector, and thus has precedence:

input[type="text"] { } /* type + attribute for specificity */ .top_bar_login_form_input { } /* only class for specificity, so *less* specificity */ 

Longer answer

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

Why that is misleading:

(Tanks to @BoltClock's insightsinsights.)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClockthe answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

TL;DR Answer

The first rule is more specific than the second one because it has both a type and attribute part for the selector, and thus has precedence:

input[type="text"] { } /* type + attribute for specificity */ .top_bar_login_form_input { } /* only class for specificity, so *less* specificity */ 

Longer answer

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

Why that is misleading:

(Tanks to @BoltClock's insights.)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

TL;DR Answer

The first rule is more specific than the second one because it has both a type and attribute part for the selector, and thus has precedence:

input[type="text"] { } /* type + attribute for specificity */ .top_bar_login_form_input { } /* only class for specificity, so *less* specificity */ 

Longer answer

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

Why that is misleading:

(Tanks to @BoltClock's insights.)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

Added TL;DR section.
Source Link
Jeroen
  • 64.4k
  • 47
  • 231
  • 372

The wrongTL;DR Answer

The first rule is more specific than the second one because it has both a type and attribute part for the selector, and thus has precedence:

input[type="text"] { } /* type + attribute for specificity */ .top_bar_login_form_input { } /* only class for specificity, so *less* specificity */ 

Longer answer:

You'd think thatYou'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

The correctionsWhy that is misleading:
  

(thanksTanks to @BoltClock's insights.)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

The wrong answer:

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

The corrections:
 (thanks to @BoltClock's insights)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

TL;DR Answer

The first rule is more specific than the second one because it has both a type and attribute part for the selector, and thus has precedence:

input[type="text"] { } /* type + attribute for specificity */ .top_bar_login_form_input { } /* only class for specificity, so *less* specificity */ 

Longer answer

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

Why that is misleading: 

(Tanks to @BoltClock's insights.)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

Wow, that remark was meant well but entirely in the wrong place...
Source Link
Jeroen
  • 64.4k
  • 47
  • 231
  • 372

The wrong answer: (thanks to @BoltClock's insights)

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

The corrections:
(thanks to @BoltClock's insights)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

The wrong answer: (thanks to @BoltClock's insights)

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

The corrections:

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

The wrong answer:

You'd think that the type="text" bit, which is an attribute selector, is more specific than a class selector, in accordance with the MDN page on specificity:

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The above quote was in the MDN article at the time this answer was written.

The corrections:
(thanks to @BoltClock's insights)

The above only seems correct, but that's because you typically include the element in the selector (e.g. input[type="text"]) when doing an attribute selector. However, what's sneaky: the input bit matters.

Suppose we have the following markup:

<input class="my-class" type="text" value="some value" /> 

In the following scenario the input renders red:

[type="text"] { color: green; } .my-class { color: red; } /* last rule matched */ 

If we reverse the rules in a scenario, the input will render green:

.my-class { color: red; } [type="text"] { color: green; } /* last rule matched */ 

This is because both selectors have equal specificity. Now introducing the input selector to the attribute rule will make one of them more specific, which can be seen in this scenario:

input[type="text"] { color: green; } /* most _specific_ rule matched */ .my-class { color: red; } 

The W3 spec makes my head hurt, but it does have the details on why the above works. See also the answer by @BoltClock and comments in those code examples for info on how the specificity is calculated.

Spelling fix, text flow fixes
Source Link
Jeroen
  • 64.4k
  • 47
  • 231
  • 372
Loading
My answer was wrong, as noted in the comments. Updated the answer with more details.
Source Link
Jeroen
  • 64.4k
  • 47
  • 231
  • 372
Loading
Source Link
Jeroen
  • 64.4k
  • 47
  • 231
  • 372
Loading