Classes and ID of HTML

Classes of HTML

An HTML classes of attributes have to use to defines equals style for element with the sames classes be names.

Using The class Attribute

All HTML elements are with the same class attributes will get the same styles.
Here we has three div element that points to the same class names:

Example:

<!DOCTYPE html>
    <html>
    <head>
    <style>
    .cities {
      background-color: black;
      color: white;
      margin: 20px;
      padding: 20px;
    }
    </style>
    </head>
    <body>
 
    <div class="cities">
      <h2>Utter Pardesh</h2>
      <p>UP is the capital of Lucknow.</p>
    </div>
 
    <div class="cities">
      <h2>Bihar</h2>
      <p>Bihar is the capital of Patana.</p>
    </div>
 
    <div class="cities">
      <h2>Tokyo</h2>
      <p>Tokyo is the capital of Japan.</p>
    </div>
 
    </body>
    </html>
          

Result:

Utter Pardesh

UP is the capital of Lucknow.

Bihar

Bihar is the capital of Patana.

Tokyo

Tokyo is the capital of Japan.

Select Element With a Specific Class

Its CSS, to selects an elements with a specific classes, write a periods(.) characters, followed by the names of the classes:

Example

Uses of CSS to styles all element with the class name “city”:

 
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    .city {
      background-color: tomato;
      color: white;
      padding: 10px;
    }
    </style>
    </head>
    <body>
 
    <h2>The class Attribute</h2>
    <p>Use CSS to style elements with
     the class name "city":</p>
 
    <h2 class="city">London</h2>
    <p>London is the capital of England.</p>
 
    <h2 class="city">Paris</h2>
    <p>Paris is the capital of France.</p>
 
    <h2 class="city">Tokyo</h2>
    <p>Tokyo is the capital of Japan.</p>
 
    </body>
    </html>
          

Result:

The class Attribute

Uses CSS to styles an element with the class name “city”:

London

London is the capital of England.

Paris

Paris is the capital of France.

Tokyo

Tokyo is the capital of Japan.

Multiple Classes

An HTML element can have be more than one classes names, each classes name value must be separated by a spaces.

Example

Style element with the class name “city”, also styles element with the class names “main”:

 
    <!DOCTYPE html>
    <html>
    <style>
    .city {
      background-color: tomato;
      color: white;
      padding: 10px;
    } 
 
    .main {
      text-align: center;
    }
    </style>
    <body>
 
    <h2>Multiple Classes</h2>
    <p>All three headers have the class name 
    "city". In addition, London also have the class
     name "main", which center-aligns the text.</p>
 
    <h2 class="city main">London</h2>
    <h2 class="city">Paris</h2>
    <h2 class="city">Tokyo</h2>
 
    </body>
    </html>
 
         

Result:

Multiple Classes

All three headers have the class name “city”. In additions, Londons also have the classes name ‘main’, which center-align the texts.

London

Paris

Tokyo

Different Tag Can Share Same Class

Different tag, likes h2 and p, can have the same classes names and thereby share the same styles:

Example

 
    <!DOCTYPE html>
    <html>
    <style>
    .city {
      background-color: tomato;
      color: white;
      padding: 10px;
    } 
    </style>
    <body>
      <b>Result:</b>
    <h2>Same Class, Different Tag</h2>
    <p>Even if the two elements do not have 
    the same tags name, they can have to the same 
    class name, and get the same styling:</p>
 
    <h2 class="city">Paris</h2>
    <p class="city">Paris is the capital of France.</p>
 
    </body>
    </html>
         

Result:

Same Class, Different Tag

Even if have two elements do not have the same tags names, they can have to the same classes name, and gets the same stylings:

Paris

Paris is the capital of France.

ID of HTML

An HTML id’s attributes are used to specify a unique id’s for an HTML elements .

Using The id Attribute

The id’s attributes are used by CSS or JavaScripts to performing of the certains tasks for an elements with the of specifics id’s values.

In using CSS, to selects an elements with a specific id’s , id selected by (#), followed by the id’s of the elements:

Example

Using CSS to styles an elements with the id “Header”:

<!DOCTYPE html>
    <html>
    <head>
    <style>
    #myHeader {
      background-color: lightblue;
      color: black;
      padding: 40px;
      text-align: center;
    } 
    </style>
    </head>
    <body>
 
    <h2>The id Attribute</h2>
    <p>Using CSS to styles an elements with
     the id "Header":</p>
 
    <h1 id="Header"> Header</h1>
 
    </body>
    </html>
 
          

Result:

The id Attribute

Using CSS to styles an elements with the id “Header”:

Header

Difference Between Class and ID

An HTML elements can only have one unique id’s that belong to that single elements, while a classes name can be used by multiples element:

Example

 
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    /* Style the element with the id "myHeader" */
    #Header {
      background-color: lightblue;
      color: black;
      padding: 40px;
      text-align: center;
    }
 
    /* Style all elements with the class name "city" */
    .city {
      background-color: tomato;
      color: white;
      padding: 10px;
    } 
    </style>
    </head>
    <body>
 
    <h2>Difference Between Class and ID</h2>
    <p>An HTML pages can only have one unique id's
     applied to one specifics elements, while a classes 
    name can be applied to multiples element.</p>
 
    <!-- A unique element -->
    <h1 id="Header">My Cities</h1>
 
    <!-- Multiple similar elements -->
    <h2 class="city">London</h2>
    <p>London is the capital of England.</p>
 
    <h2 class="city">Paris</h2>
    <p>Paris is the capital of France.</p>
 
    <h2 class="city">Tokyo</h2>
    <p>Tokyo is the capital of Japan.</p>
 
    </body>
    </html>
          

Result:

Difference Between Class and ID

An HTML pages will only have to one unique id’s applied to one specifics elements, while a classes names will be applied to multiples elements.

My Cities

India

India is the capital of Dehli.

U.P

U.P is the capital of Lucknow.

Bihar

Bihar is the capital of Patna.

Leave a Reply

Your email address will not be published. Required fields are marked *