Attribute selector
Using attribute selector, you can select all elements by the name or value of a given attribute and apply styling to them.
Example 1: Below is an example of an HTML line which has ‘rel’ attribute with the value of “newfriend”
<h3 id="title" class="friend" rel="newfriend">David Walsh</h3>
Let’s see how to use attribute selector for ‘rel’ attribute in the above line.
h3[rel="newfriend"] {
color: yellow;
}
This selector is frequently used by the developers in code for ‘checkbox’ element. Read the example given below.
Example 2:
input[type="checkbox"] {
color: purple;
}
It is also frequently used for the anchor tags in the code. Read the example given below.
Example 3:
a[title] {
color: red;
}
Combinators: These are used to apply styling to the html elements by using the relationship between the selectors. Combinators allows you to mix simple selectors and apply logic between them. Let’s discuss four different combinator selectors in CSS.
- Descendant selector
- Child selector
- Adjacent sibling selector
- General sibling selector
10 CSS Selectors Every Developer Should Know
What’s the first thing for any website to create a good impression on a user? …
Yes…it’s the user interface for any website. Every developer knows how important it is to create a beautiful design for users to interact with any website. Giving styling to web pages smartly in a minimal amount of time is not an easy task if you don’t have a good knowledge of CSS and its selectors. CSS selectors target the specified elements in an HTML document and help developers apply styling to the web pages. You might know some basic CSS selectors but a bit more than basic knowledge helps in achieving your goal faster. Using the right CSS selectors minimizes the amount of code, makes it more readable, and makes the CSS simpler to maintain in the future.
There are a wide variety of CSS selectors available. Let’s discuss some important ones to simplify your work in front-end development.
Table of Content
- Element or Group Selector
- #id selector
- class selector
- Attribute selector
- Descendant selector
- Child selector
- Adjacent and general sibling selectors
- The star selector *
- Pseudo-classes and Pseudo element
- nth-of-type and nth-child