×

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 Color

CSS Colors: The CSS color property is used for setting the HTML element’s color. The color of the fonts and the color of the background can be set-up by using...

5 minutes 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 Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute 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 Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.