Difference between "." and "#" selector in CSS

CSSWeb DevelopmentFront End Technology

'.' selector

'.' selector is a class level selector. The dot operator is used to create a style class which can then be applied on multiple html elements.

'#' selector

'#' selector is an element level selector. Hash operator is used to applying style on a particular element whose id is the same as used in '#' selector

Example

Following the example, shows usage of '.' as well as '#' selector on three div elements.

<!DOCTYPE html>
<html>
<head>
   <title>Selector Example>/title>
   <style>
      .blackBorder {
         border: 2px solid black;
      }
      #redDiv {
         border: 2px solid red;
      }
   </style>
</head>
<body>
   <div> No Border Box </div>
   <div class="blackBorder"> Black Border Box </div>
   <div id="redDiv"> Red Border Box </div>
   <div> No Border Box </div>
   <div class="blackBorder"> Black Border Box </div>
</body>
</html>
raja
Published on 13-Jan-2020 06:22:29
Advertisements