How can we group selectors?
Experience Level:
Junior
Tags:
CSS
Answer
If you have multiple selectors with the same style definition, you can save some space and merge all the styles to one. If you do this, you will get the following advantages:
- Shorter code: The resulting code will be shorter and more readable.
- Faster to download: Downloading the shorter web page will be faster and it will consume less bandwidth. Mobile users will appreciate this.
- Easier to maintain: The styles will be easier to maintain. In case some change needs to be done to the style definition, it can be done on just one place. Without the selector grouping it would be necessary to do the change on many places.
h1 {
font-weight: bold;
}
h2 {
font-weight: bold;
}
h3 {
font-weight: bold;
}
h1, h2, h3 {
font-weight: bold;
}
Related CSS job interview questions
-
What is the difference between external, embedded and inline styles?
CSS Junior -
How to write comments and what are they used for?
CSS Junior -
What is a selector and what selector types do you know?
CSS Junior -
Can you describe CSS syntax?
CSS Junior -
What problem does CSS solve?
CSS Junior