Completing a stylesheet is like waging a war between selectors. You are constantly overriding previously defined styles with new ones:
1/* You might initially want all the H2 tags to have font-size: 14px and color: blue. */ 2h2 { 3 font-size: 14px; 4 color: blue; 5} 6 7/* But then you have a very particular page where the background is also blue, so you need your headings to be white. */ 8h2 { 9 color: white; 10}
This happens all the time, and at some point, it can be challenging to override previous styles. You must organize your styles properly and start from the least specific to the most specific.
These very specific selectors will help you a lot. They will be your best weapon when fighting your CSS war!
📺 Here is a super cool video (3:40 min) explaining specificity.
1#someDiv > p { 2 color: blue; 3}
This statement targets the
tags that are direct children of the div with the ID someDiv and turns them blue. Note that it only works for the children of that div, not necessarily all its descendants. Let’s explore this further with the following example.
1p + p { 2 color: red; 3}
We used the adjacent sibling selector to change the second and third paragraphs to red. This seems very tricky, doesn’t it? Instinctively, we would expect the first paragraph to be red as well. After all, the first paragraph is on the same level as the next two, and, has siblings.
However, this selector only applies to elements that are preceded by something else. In this instance, only the paragraphs preceded directly by a sibling paragraph will be targeted.
The first paragraph in the list is preceded by the div
, so it remains unchanged.
1#someDiv * { 2 color: red; 3}
The previous CSS code turns red every element inside a specific div, this includes items such as links, that have a default color set to something else and wouldn’t be affected by simply targeting the div.
1div * p { 2 color: red; 3}
You can take this as far as you want; the following targets the "grandchildren" of the div. You will find this chaining method used frequently in CSS debugging tricks.
1a[href='http://4geeksacademy.com/'] {color: blue;}
If we want to change the font color of the "4Geeks" link, we could use pseudo selectors. However, doing so would assume that the list stays in that order, and, browser support isn’t the best. Instead, what we can do is use an attribute selector to target the specific href
that we’re interested in.
1div[id*='section'] {color: red;}
The following code targets any div
with the word "section" in the title. It can be "section3" or "section-Four", it doesn’t matter. As long as it contains the indicated string, the subsequent styles will apply.
1a:link{color: green;} 2a:visited{color: yellow;} 3a:hover{color: blue;} 4a:active{color: red;}
You can assign a different color to any link on the website, depending on its status:
:link
default color, before clicking on it.
:visited
after clicking the link.
:hover
when the cursor is over the link.
:active
when the cursor is clicking on the link.
1input {padding: 5px;} 2input:disabled { 3 background: #ddd; 4 color: #949494; 5 border: 2px solid black; 6} 7input:focus {font-size: 2em;} 8input:enabled {border: 2px solid black;}
It is very important to take enough time to style our forms. Styling is the best way to tell the user that one input is either disabled, checked, or that the cursor is over a particular input.
1#myUL li:first-child {background: blue;} 2#myUL li:nth-child(3) {background: orange;} 3#myUL li a:first-of-type {background: green;}
You can apply styles to elements based on their position.
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> element that is the first child of its parent |
:first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects <input> elements with a value within a specified range |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
:last-child | p:last-child | Selects every <p> element that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects <input> elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
:read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
:required | input:required | Selects <input> elements with a "required" attribute specified |
:root | root | Selects the document’s root element |
:target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |
🔗 Great reading about CSS Selectors: The 30 CSS selectors you must memorize