Let's say I have some deeply nested markup that I want to target with CSS. It could be anything, but for example:
<div> <div id='someid'> <span class='someclass'> <a class='link' href='alink'>Go somewhere</a> </span> </div> <div>
Is it acceptable to write a CSS rule targeting the <a>
tag directly, like this?
a.link { font-size: large; }
Or is this considered non-standard that may fail in some browsers? Do I need to target each element in the chain like this?
div div span.someclass a.link { font-size: large; }
If you're creating a class that is specific to/only works on one certain type element, it's best to use the element in the selector as well. An example of this would be a bullet list you want to display on one line, since this class requires the HTML to be a <ul> you should specify it in the CSS as well; ul. inline .
To nest a selector, you simply separate them with a space. This will make paragraph tags inside main have one font size, and paragraph tags inside either header or footer have another font size. Descendant selectors target all elements inside the other, no matter how deeply nested it is.
To be nest-prefixed , a nesting selector must be the first simple selector in the first compound selector of the selector. If the selector is a list of selectors, every complex selector in the list must be nest-prefixed for the selector as a whole to be nest-prefixed.
What is CSS nesting? This syntax has been possible with CSS preprocessors like Sass and Less. As you can see from both code samples above, nesting is enclosing a selector inside another selector. Nesting helps you to group related styles and write CSS in a nested hierarchy.
Both are completely acceptable to use and the answer depends on your specific solution. For instance if you have other areas where you are sharing common properties that are defined by that class you'd want to keep it as general as possible. If for instance you have a navigation and the links in that area share some common elements those could be defined by a.link
Then in your nested html, you might do something like
.someclass a.link {font-size:8px}
to make that text smaller.
Here is an article that discusses how the specificity works: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
Both are perfectly valid, and which one you use depends on what you want to do.
If you are creating a generic class that you want to be able to use throughout your entire site regardless of element and where the element is, you should only use .class
. A good example for this is something like .icon
which you may want to use on links, list items, headings etc. And you want to be able to use them everywhere.
If you're creating a class that is specific to/only works on one certain type element, it's best to use the element in the selector as well. An example of this would be a bullet list you want to display on one line, since this class requires the HTML to be a <ul>
you should specify it in the CSS as well; ul.inline
. This way you can use the "inline" class name for other elements as well, without the styling affecting both.
If you're only using the class in order to select the element but it shouldn't have any generic styling you should be specific. For example, you may want the first paragraph in your #blog-post
element to be larger, then you should specify both #blog-post
and the class; #blog-post p.first
(note that these types of classes are rarely needed anymore thanks to advanced selectors like :first-of-type
, h2 + p
etc).
Saying that ".link
is the best, a.link
is second best and a long selector is bad" is just wrong. It all depends on the situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With