More complex specificity example
suggest changediv {
font-size: 7px;
border: 3px dotted pink;
background-color: yellow;
color: purple;
}
body.mystyle > div.myotherstyle {
font-size: 11px;
background-color: green;
}
#elmnt1 {
font-size: 24px;
border-color: red;
}
.mystyle .myotherstyle {
font-size: 16px;
background-color: black;
color: red;
}
<body class="mystyle">
<div id="elmnt1" class="myotherstyle">
Hello, world!
</div>
</body>
What borders, colors, and font-sizes will the text be?
font-size:
The font size is 24px. #elmnt1 has specificity (1, 0, 0), which beats every other rule here.
The border is 3px dotted red. border-color: red comes from #elmnt1. The width and style come from the div rule, because #elmnt1 does not set them.
The background is green. It is set by div (0, 0, 1), body.mystyle > div.myotherstyle (0, 2, 2), and .mystyle .myotherstyle (0, 2, 0). The middle selector wins.
The color is red. It is set by div and by .mystyle .myotherstyle (0, 2, 0). The class selector wins. #elmnt1 does not set color.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents