For working professionals
For fresh graduates
More
CSS Tutorial: A Comprehensive …
1. CSS Tutorials
2. CSS Media Queries
3. CSS Selectors
Now Reading
4. CSS Navigation Bar
5. CSS Transition
6. CSS Text
7. How to Center a Div in CSS
8. CSS Images
9. CSS Float
10. CSS Form
11. CSS Inline Block
12. CSS Examples
13. CSS Dropdown
14. CSS Flexbox
15. Responsive Web Design
16. CSS Cheat Sheet
17. CSS Variables
18. CSS Grid Layout
19. CSS Animation
20. CSS Frameworks
21. CSS Positioning
22. CSS Comment
23. CSS Gradients
A CSS selector is the first part of the CSS rule. This tool does not select specific elements or a group of elements for applying the CSS property; rather, it is a versatile tool that empowers you to control the style of the webpage. The CSS selectors select aspects known as the "Subject of the selector". CSS selectors in HTML target the HTML elements that need styling. The CSS selector specification defines selectors separately. Like other CSS properties, they need to be browser-friendly.
A selector is not just a tool but the backbone that empowers you to style the webpage precisely as you envision. In the regular webpage application you develop, understanding and implementing CSS selectors can significantly enhance your page's look and aesthetic experience, giving you the power to create visually appealing designs.
When you need to define the same rules for different elements, you can streamline your code by combining them into a single list. This is a CSS selector list, a powerful tool for efficient styling.
For example, in the CSS style below, the rules for p1 and h1 are the same. Hence, combine them into one single list.
Example:
p1 {
color: pink;
}
h1 {
color: pink; }
After combining the selector list, the above example can be implemented as below:
p1, h1 {
color: pink;
}
Let's dive into the world of CSS selectors. There are various types, each with its unique functionality and use cases. In this article, we'll explore the major ones, equipping you with the knowledge to style your web pages precisely.
A CSS Type selector is also called a CSS Name selector or CSS Element selector because it selects a particular name/element from the CSS.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p>This is to learn CSS selectors</p>
<h1> Welcome to Selectors </h1>
</body>
</html>
In the example above, highlight will apply the CSS selector, which is a type selector.
Output of above code:
A CSS ID selector selects the element using the ID name defined for the type. Each type can have a different ID.
CSS ID selector syntax:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p>This is to learn CSS selectors</p>
<h1> Welcome to Selectors </h1>
</body>
</html>
In the example above, the CSS selector, an ID selector, will be applied only to the ID named para1. Hence, the selector is not used in another paragraph. See the output of the first section below to learn the difference.
Output of above code:
A CSS class selector selects the element using the specific class attribute.
CSS class selector example:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p>This is to learn CSS selectors</p>
<h1 class="center"> Welcome to Selectors </h1>
</body>
</html>
In the example above, the CSS selector, a class selector, will be applied only to the class attribute.
Output of above code:
In the same example above, let's make some changes in the code to select only particular class elements and see how the output changes.
<!DOCTYPE html>
<html>
<head>
<style>
h1.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p class=”center”>This is to learn CSS selectors</p>
<h1 class="center"> Welcome to Selectors </h1>
</body>
</html>
Output of above code:
A CSS universal selector selects the whole elements in the document
CSS universal selector example:
<!DOCTYPE html>
<html>
<head>
<style>
* {
color: blue;
font-size: 20px;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p class=”center”>This is to learn CSS selectors</p>
<h1 class="center"> Welcome to Selectors </h1>
</body>
</html>
In the example above, all HTML elements have the universal CSS selector applied to them.
Output of above code:
A CSS Group Selector selects the same style of elements and groups them.
CSS group selector example:
<!DOCTYPE html>
<html>
<head>
<style>
p,h1 {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<p id="para1">Hello Example</p>
<p class=”center”>This is to learn CSS selectors</p>
<h1 class="center"> Welcome to Selectors </h1>
</body>
</html>
In the example above, the CSS selector groups p and h1 applies the same style.
Output of above code:
The CSS combinator combines (or) explains the relationship between two selectors. If we want to combine any two selectors, we can use this CSS Combinator. There are four types of CSS combinators available.
Example of a general sibling selector:
<!DOCTYPE html>
<html>
<head>
<style>
div ~p {
color: blue;
font-size: 20px;
}
</style>
</head>
<body>
<div>
<p id="para1">Hello Example</p>
<p class=”center”>This is to learn CSS selectors</p>
<h1 class="center"> Welcome to Selectors </h1>
</div>
</body>
</html>
Other types of selectors are explained below with examples.
This type of selector selects the elements in a particular state. It starts with the colon symbol (:)
Some examples of pseudo-states are hover and focus. These are some states of the behavior of HTML elements.
Example:
h1 :hover {
background-color: pink }
Pseudo-elements behave in the same way. It starts with a double colon (::)
You can combine the Pseudo class and Pseudo elements.
Example:
p::firstline {
background-color : pink }
CSS selectors are a flexible tool that empowers you to control the style of the website. They choose components referred to as the "Subject of the selector". Understanding and imposing CSS selectors can substantially beautify your web page's appearance and aesthetic revel in, supplying you with the strength to create visually appealing designs. CSS selectors are faster and easier to examine and put into effect than XPath. CSS selector lists are an effective tool for green styling. There are various kinds of CSS selectors, each with its specific functionality and use cases, inclusive of type, magnificence, ID, and attribute selectors.
1. What are CSS selectors?
CSS selectors are part of CSS rules that help to style particular elements or complete web pages.
2. What are the types of CSS selectors?
There are different types of CSS selectors. Type, ID, Class, Group, and universal selector are significant types.
3. What is the universal selector?
The universal selector applies the style to the whole document and all HTML page elements.
4. How specific should selectors be?
You must adequately define which type of CSS you are going to implement. Based on the implementation, the respective CSS selector style will apply.
5. Can I combine multiple selectors?
Yes, you can combine multiple selectors in CSS to target particular elements.
6. Are CSS selectors faster than XPath?
Yes, CSS selectors are faster than XPath, which has more consistency.
7. How do you select two selectors in CSS?
You can use CSS Combinator to select more than one selector. Various types of combinations can be made. This is explained in the above CSS Combinator section.
8. Where is the CSS selector?
To find a CSS selector on a webpage, right-click and select 'Inspect' on the desired element. This will open the browser's developer tools.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.