DEV Community

Share Point Anchor
Share Point Anchor

Posted on • Originally published at sharepointanchor.com on

CSS id and class

CSS id and class selectors are often used to style web page elements. The id (unique identifier) selector uses the id attribute of an HTML element to select a specific element. To specify the id, you can use # symbol followed by the element. For instance, #design.

The class selector is used when the same style must be applied to multiple HTML elements. It is specified with ( . ) character followed by class name. For example, .info

Difference Between id and class

id class
The id is a unique identifier Classes are not unique
Each element can have only one id The same class can be used on multiple HTML elements
A specific style must be applied to a single HTML element The Same style must be applied to multiple HTML elements

Example for the id Selector:


<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>

      #blue {

        color: #1c87c9;

      }
    </style>
  </head>
  <body>

    <p>The first paragraph.</p>

    <p id="blue">The second paragraph.</p>

    <p>The third paragraph.</p>

  </body>
</html>

Enter fullscreen mode Exit fullscreen mode

In the above code, we have assigned blue as id selector to the second paragraph and declared a style using the color property inside the head section.

Result:

After executing the above code, you will get the output as shown in the below image.

Result of id selector

Example for the id Selector:


<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>

      .blue {

        color: #1c87c9;
      }
    </style>
  </head>
  <body>

    <h2 class="blue">This is some heading.</h2>

    <p>The second paragraph.</p>

    <p class="blue">The third paragraph.</p>

  </body>
</html>

Enter fullscreen mode Exit fullscreen mode

In this code, the class selector is used twice in the heading and paragraph. We have assigned blue as a class selector and declared its style using color property inside the head section.

Result:

By running the above code, you will see the result as given in the below image.

Result of class selector

The post CSS id and class appeared first on Share Point Anchor.

Dynatrace image

Highlights from KubeCon Europe 2025

From platform engineering to groundbreaking advancements in security and AI, discover the KubeCon Europe 2025 insights that are shaping the future of cloud native observability.

Learn more

Top comments (0)

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay