Tutorial Details
- Topic: CSS selectors
- Difficulty: Intermediate
Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Nettuts+. This tutorial
was first published in November, 2010.
So you learned the base id
, class
,
and descendant
selectors – and then called it a day? If so, you’re
missing out on an enormous level of flexibility. While many of the selectors mentioned in this article are part of the CSS3 spec, and are, consequently, only available in modern browsers, you owe it to yourself to commit these to memory.
1. *
- * {
- margin: 0;
- padding: 0;
- }
Let’s knock the obvious ones out, for the beginners, before we move onto the more advanced selectors.
The star symbol will target every single element on the page. Many developers will use this trick to zero out the margin
s
and padding
. While this is certainly fine for quick tests, I’d advise
you to never use this in production code. It adds too much weight on the browser, and is unnecessary.
The *
can also be used with child selectors.
- #container * {
- border: 1px solid black;
- }
This will target every single element that is a child of the #container
div
.
Again, try not to use this technique very much, if ever.
View Demo
Compatibility
- IE6+
- Firefox
- Chrome
- Safari
- Opera
2. #X
- #container {
- width: 960px;
- margin: auto;
- }
Prefixing the hash symbol to a selector allows us to target by id
.
This is easily the most common usage, however be cautious when using id
selectors.
Ask yourself: do I absolutely need to apply an
id
to this element
in order to target it?
id
selectors are rigid and don’t allow for reuse. If possible, first
try to use a tag name, one of the new HTML5 elements, or even a pseudo-class.
View Demo
Compatibility
- IE6+
- Firefox
- Chrome
- Safari
- Opera
3. .X
- .error {
- color: red;
- }
This is a class
selector. The difference between id
s
and class
es is that, with the latter, you can target multiple elements.
Use class
es when you want your styling to apply to a group of elements.
Alternatively, use id
s to find a needle-in-a-haystack, and style
only that specific element.
View
Demo
Compatibility
- IE6+
- Firefox
- Chrome
- Safari
- Opera
4. X Y
- li a {
- text-decoration: none;
- }
The next most comment selector is the descendant
selector. When you
need to be more specific with your selectors, you use these. For example, what if, rather than targeting all anchor tags, you only need to target
the anchors which are within an unordered list? This is specifically when you’d use a descendant selector.
Pro-tip – If your selector looks like
X
, you’re doing it wrong. Always ask yourself if it’s absolutely necessary to apply all of that weight.
Y Z A B.error
View
Demo
Compatibility
- IE6+
- Firefox
- Chrome
- Safari
- Opera
5. X
- a { color: red; }
- ul { margin-left: 0; }
What if you want to target all elements on a page, according to their type
,
rather than an id
orclass
name?
Keep it simple, and use a type selector. If you need to target all unordered lists, use ul
.
{}
View
Demo
Compatibility
- IE6+
- Firefox
- Chrome
- Safari
- Opera