• <id>

    L' id attribut, spécifie un identifiant unique pour un élément HTML (la valeur doit être unique dans le document HTML).

    La valeur identifiant peut être utilisé par CSS et JavaScript pour effectuer certaines tâches pour un élément unique avec l'identifiant spécifié.

    Dans CSS, pour sélectionner un élément avec un identifiant spécifique, écrire un dièse (#) caractère, suivi de l'ID de l'élément:

    ""

    Exemple simplifié

     <style>

    #myHeader {

        background-color: lightblue;
        color: black;
        padding: 40px;
        text-align: center;

    </style>
    <h1 id="myHeader">My Header</h1>

     Exemple 

    <!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>Use CSS to style an element with the id "myHeader":</p>

    <h1 id="myHeader">My Header</h1>

    </body>
    </html>

    Résultat Exemple

    The id Attribute

    Use CSS to style an element with the id "myHeader":

    My Header

    Astuce: L'attribut id peut être utilisé sur tout élément HTML.

    Remarque: La valeur id est sensible à la casse.

    Remarque: La valeur id doit contenir au moins un caractère, et  ne doit pas contenir des espaces (espaces, onglets, etc.).

     

    Différence entre la classe et l'ID

    Un élément HTML ne peut avoir qu'un identifiant unique qui appartient à cet élément unique, alors qu'un nom de classe peut être utilisé par plusieurs éléments:

    Exemple simplifié   

    <style>
    /* Style the element with the id "myHeader" */
    #myHeader {
        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>

    <!-- A unique element -->
    <h1 id="myHeader">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>

    Exemple  

    <!DOCTYPE html>
    <html>
    <head><style>
    /* Style the element with the id "myHeader" */
    #myHeader {
        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 page can only have one unique id applied to one specific element, while a class name can be applied to multiple elements.</p>

    <!-- A unique element -->
    <h1 id="myHeader">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>

    Résultat de l'exemple

    Difference Between Class and ID

    An HTML page can only have one unique id applied to one specific element, while a class name can be applied to multiple elements.

    My Cities

    London

    London is the capital of England.

    Paris

    Paris is the capital of France.

    Tokyo

    Tokyo is the capital of Japan.