×

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set of CSS rules. They choose elements of HTML based on its class, type, id, and attribute.

Here are some selector types in CSS which are as follows:

  • Element Selector
  • Id Selector
  • Class Selector
  • Universal Selector
  • Group Selector

Element Selector

The element selector of CSS is used to select the element of HTML by the name. The example of element selector is as follows:

<!DOCTYPE>
 <html>
 <head>
 <title> Element Selector of CSS </title>
 <style>
 p {
 color: grey;
 text-align: center; 
 }
 </style>
 </head>
 <body>
 <p> This is an example of Element Selector </p>
 <p> hellow! </p> 
 </body>
 </html> 

Output:

CSS Selector

Id Selector

This selector of CSS selects the HTML element’s attribute id to choose a particular element. The id is always considered special inside the page. Thus, it is selected to choose a unique and single element. The id is defined with a hash character (#), pursued by the element’s id.

The example of id selector is as follows which is defined with id “p”:

<!DOCTYPE>
 <html>
 <head>
 <title> ID Selector of CSS </title>
 <style>
 #p { 
 color: blue;
 text-align: center;
 }
 </style>
 </head>
 <body>
 <p id="p"> ID Selector </p> 
 <p> This is not troubled via the style </p>
 </body>
 </html> 

Output:

CSS Selector

Class Selector

These selectors choose the elements of HTML along with a particular attribute of class. A class selector can be used with the period character (Full stop). It is pursued by the name of the class. Usually, a class must not be begin with the number.

The example of the class selector is shown below:

In this, a class is represented by “class1”:

<!DOCTYPE>
 <html>
 <head>
 <title> Class Selector of CSS </title>
 <style>
 .class1 { 
 color: red;
 text-align: center;
 }
 </style>
 </head>
 <body> 
 <h1 class="class1"> Class Selector </h1>
 <h2 class="class1"> This is an example of Class Selector </h2>
 </body>
 </html> 

Output:

CSS Selector

Specific Element Selector: To define a specific element of troubled HTML, we can use the name of the element with the class selector.

Following is an example for selecting a specific element:

<!DOCTYPE>
 <html>
 <head>
 <title> Specific element Selector of CSS </title>
 <style>        
 h1.class1 {  
 color: brown; 
 text-align: center;
 }
 </style>
 </head>
 <body> 
 <h1 class="class1"> Specific Element Selector </h1>
 <p class="class1"> This is an example for selecting a specific element </p>
 </body>
 </html> 

Output:

CSS Selector
  • Various elements of HTML can also be referred to as one or more classes.

Example:

In the following example, element <p> is written according to the class=”class1” and to the class=”large”:

<!DOCTYPE>
 <html>
 <head>
 <title> One or More class Selector</title>
 <style>            
 p.class1 { 
 color: blue;
 text-align: center;
 }
 p.large {
 font-size: 200%;
 }
 </style> 
 </head>
 <body>
 <h1 class="class1"> One or More Class Selector </h1>
 <p class="class1"> This is an example for selecting one or more elements </p>
 <p class="class1 large">This is a paragraph which will be blue, center-aligned, and written in a large font-size. </p> 
 </body>
 </html> 

Output:

CSS Selector

Universal Selector

CSS has a unique kind of selector i.e., universal selector. It is applied on the elements as the wildcard character. These selectors select every element which is displayed over the page.

The example of universal selector is as written as below:

<!DOCTYPE>
 <html>
 <head>
 <title> Universal Selector</title>
 <style>             
 * {
 color: red; 
 font-size: 20px;
 }
 </style>
 </head>
 <body>
 <h1> Universal Selector </h1>
 <p> This is an example for selecting all the elements available on the page </p> 
 <p id="class1">This is a paragraph which will be blue, center-aligned, and written in a large font-size. </p>
 <p>hello!</p>
 </body>
 </html> 

Output:

CSS Selector

Group Selector

Various group selectors of CSS are applied to select every element including similar style definitions. A lengthy code can be reduced by the use of the group selectors. Commas can be used to isolate all the selectors in the grouping.

Take a look on the following code i.e., written without the use of group selector:

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

We need to describe the properties of CSS for every element. These properties are grouped in the following ways:

h1,h2,p {
 color: red;
 text-align: center;
 }                  
 Let’s take a complete example of group selector which is as follows:
 <!DOCTYPE>
 <html>
 <head> 
 <title> Group Selector of CSS </title>
 <style>            
 h1,h2,p {
 color: red; 
 text-align: center;
 }
 </style> 
 </head>
 <body>
 <h1> Group Selector </h1>
 <h2> This is an example of Group Selector </h2>
 <p>This is not affected</p> 
 </body>
 </html> 

Output:

CSS Selector

Related Topics

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.