CSS | Class Selector

The .class selector is used to select all elements which belong to a particular class attribute. To select the elements with a particular class, use (.) character with specifying class name. Class name is mostly used to set the CSS property to given class.

Syntax:

.class {
    // CSS property
} 

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <style>
            .geeks {
                color:green;
            
            .gfg {
                background-color: yellow;
                font-style:italic;
                color:green;
            }
          
        </style>
    </head>
      
    <body style = "text-align:center">
          
        <h1 class = "geeks">
            GeeksforGeeks
        </h1>
          
        <h2>.class Selector</h2>
          
        <div class = "gfg">
            <p>GeeksforGeeks: A computer science portal</p>
        </div>
    </body>
</html>                    

chevron_right


Output:

Example 2: This example describes the space separated class name.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>class selector</title>
        <style>
            .geeks {
                color:green;
            
            .gfg {
                background-color:yellow;
              
        </style>
    </head>
      
    <body style = "text-align:center">
          
        <h1 class = "geeks">
            GeeksforGeeks
        </h1>
          
        <h2 class = "gfg">.class Selector</h2>
          
        <p class = "geeks gfg">
            GeeksforGeeks: A computer science portal
        </p>
    </body>
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by .class selector are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.