Selectors Property in CSS


In CSS, selectors are used to target the HTML elements on our web pages that we want to style. There are a wide variety of CSS selectors available, allowing for fine-grained precision when selecting elements to style.

Selector used for
#id This selects the element with id="name"
.class This selects all elements with class="test"
element.class This selects only< p > elements with class="test"
* This selects all elements
element This selects all < p > elements
element,element,.. This selects all < div > elements and all < p > elements

Source Code :

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    #id1{
      color:red;
    }
    #id2{
      color:blue;
    }
    .clr{
      color:green;
    }
  </style>
</head>
<body>
    <h1 id="id1">Heading-1</h1>
      <p class="clr">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Amet expedita sunt excepturi, quidem a ipsum magni labore nam adipisci unde maxime, culpa voluptatem consequuntur inventore temporibus necessitatibus nisi itaque numquam saepe. Quo itaque nemo obcaecati</p>
    <h1 id="id2">Heading-2</h1>
    <h2 class="clr">Sub Heading</h2>
    <p class="clr">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Amet expedita sunt excepturi, quidem a ipsum magni labore nam adipisci unde maxime, culpa voluptatem consequuntur inventore temporibus necessitatibus nisi itaque numquam saepe. Quo itaque nemo obcaecati</p>
</body>
</html>

<!--
Simple Selectors
  element
  class
  id
  universal
-->

Output

Background Attachment
Live Preview


CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects