CSS Id and Class


I addition to setting a style for HTML elements CSS allows you to specify your own selectors called "id" and "class"

Id Selector:

  • It's used to specify a style for single and unique HTML elements
  • It's using the id attribute of the HTML elements and is defined with a hashtag "#"
  • Do not start an Id name with a number it will not work in the browser.

<style>

    #paragraph{

        background-color: #000;

        color: red;

        text-align: center;

      }

</style>

<p id ="paragraph"> How are you?</p>

       

 

Class Selector:

The class selector is used to specify a style for a group of elements.  Unlike the id sector, the class selector is most often used for several elements.

  • It's allowing you to set a particular style for many HTML elements with the same class.
  • The class selector uses the HTML class attribute and is defined with a preceded by a period ("."). Like. paragraph
  • Do NOT start a Class name with a number because some browser does not support it.

<style>

    .paragraph{

        background-color: #000;

        color: red;

        text-align: center;

      }

</style>

<p class ="paragraph"> How are you?</p>

 


Leave a comment
No Cmomments yet