Grouping and Nesting CSS Selectors
Grouping and Nesting CSS Selectors
CSS Selectors
When you start to write bigger and
bigger HTML files, and your CSS styles
start to become longer and longer, it
might be worth looking into if you can
shorten and simplify them a bit
using grouping and nesting.
Grouping
The easiest way to identify where you might be able to group selectors in one
line is to see where you have repetition in your styles.
If you had those paragraphs tags first nested inside a div tag, neither styles
would apply because they wouldn't be direct children of main or header.
Adjacent Selector
There will sometimes be cases where you want to target an element based on whether or not it came right
after another element.
Let's say you wanted the first paragraph after every h1 tag to be in a larger font size:
You can use an adjacent selector to say "hey, I want to style only
the paragraph tag right after my header":
Now your first paragraph will be in a larger font, but the following
paragraphs will be in their usual font size.
END