HTML - ID, Class Attribute

While styling of HTML, attribute id & class plays main role.
The HTML class attribute is used to specify a class for an HTML element.
The HTML id attribute is used to specify a unique id for an HTML element.

Using id attribute

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document.
The id attribute is used to point to a specific style declaration in a styling element.

While defining style for id attribute use # to indicate this css is for id.


        #p1 {
            background-color: yellow;
        }
        
        <p id="p1" >Text line with id="p1"</p>

Text line with id="p1"

Text line with no specific id




Using class attribute

The class attribute used to specify a class for an HTML element
Multiple HTML elements can share the same class.

While defining style for class attribute use . to indicate this css is for class.


        .p2 {
            color: #3f51b5;
            font-size: 2rem;
        }
        
        <p class="p2" >Text line with class="p2"</p>

Text line with class="p2"

Heading 3 text with class="p2"