Site icon Buy CSS Snippets

What is a CSS selector? | Different Types of CSS Selectors?

What is CSS Selectors?

CSS Selectors are very important. The content is selected by CSS Selectors, for which we want to write CSS Rules. Selectors are a part of CSS Rule Syntax. Which can be an HTML Element, Element Attribute. You have already read about CSS Selectors in CSS Syntax Lesson. Now let’s know about its types

CSS Selectors are very important for a web page. Through CSS Selectors, we select the content for which we want to write CSS. CSS Selectors are used to “find” (or select) those HTML elements that we want to style.

For example, if you want to do CSS style in <h2> tag, then you can use selector to give color or size of <h2> etc.

The elements selected by the selectors are referred to as the subjects of the selectors.

In simple words, CSS Selectors are used to finding or selecting the HTML elements that you want to style.

What is a CSS selector? | Different Types of CSS Selectors?

CSS syntax has 3 parts-

selector { property: value }

We can divide CSS Selectors into five categories | Different Types of CSS Selectors

CSS Element Selector

The one who is our Element Selector selects HTML elements based on their name.

Here, all <h1> elements will be center-aligned, with a black text color

<style>
 
h1{  
    text-align: center;  
    color: black;  
}

</style> 

CSS Id Selector

Id selector uses the id attribute of the HTML element to select a specific element. ID is always different in every web page means unique so id selector is used to select unique elements. To select any specific element, write the (#) character with it, followed by the name of the element’s id.

<style>
 
#para1 {  
    text-align: center;  
    color: blue;  
}

</style>

CSS Class Selector

The class selector selects HTML elements with a specific class attribute. It is used with a period character. (full stop) after the class name.

.left {  
    text-align: left;  
    color: blue;  
}  
</style>

CSS Universal Selector

The universal selector (*) selects all the HTML elements of a web page.

<style>  
* {  
   color: mediumvioletred;  
   font-size: 20px;  
}   
</style>

CSS Grouping Selector

Grouping Selector is used to selecting all the elements with the same style definitions. We use the Grouping selector to reduce the code. Commas are used to separate each selector in a grouping.

<style>

h1, h2, p {  
    text-align: center;  
    color: green;  
}

</style>

All CSS Simple Selectors

SelectorExampledescription
element.classp.txtSelects only <p class=”txt”> elements
.class.leftSelects all elements with class=”left”
#id#para1Selects the element with id=”para1″
**Selects all elements

FAQ-

What is the meaning of CSS Selectors?

CSS Selectors are used to finding or select the HTML elements that you want to style.

Example of the CSS Selectors?

<style>
p{ } /* Element Selector */
#id{ } /* Id Selector */
.class{ } /* Class Selector */
</style>
Exit mobile version