what is the difference between
body{
background: #4b4b4b;
}
and
*{
background: #4b4b4b;
}
which has higher priority?
what is the difference between
body{
background: #4b4b4b;
}
and
*{
background: #4b4b4b;
}
which has higher priority?
The body
selector has higher priority, but the *
selector applies more broadly, so in <body>foo<p>bar</p></body>
the body
selector will determine the background of the text foo
, but the *
selector will determine the background of the <p>
element.
Note, also that many browsers create an element around the <body>
that includes its margins and scrollbars, so the *
selector may also determine the color of that region.
body
selects the body element, *
selects all elements.
Out of those two, body
has higher priority.
What is the difference?
body
is an element selector (selects an element body
) while *
is a universal selector (selects all elements).
Which has higher specificity (the proper term for priority)?
When calculating specificity of a selector (Think of it as a binary number):
1000
.0100
.0010
0001
. 0000
.Thus the specificity of body
is 0001
and the specificity of *
is 0000
. body
wins.
Some HTML elements have a default background color, such as <input>
, <select>
, etc. Using *
will affect them as well instead of only the <body>
and all children with a transparent background.