How To Define a id In HTML

admin_img Posted By Bajarangi soft , Posted On 29-09-2020

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 style sheet. It is also used by JavaScript to access and manipulate the element with the specific id,The syntax for id is: write a hash character (#) character, followed by an id name. Then, define the CSS properties within curly braces {}.

Html id result image

1.Create a HTML FIle Using Id Attribute

<!DOCTYPE html>
<html>
<head>
    <style>
        #myHeader {
            background-color: lightblue;
            color: black;
            padding: 40px;
            text-align: center;
            font-size: 40px;
        }
        h2{
            color:red;
            font: 30px;
        }
        p{
            color:red;
            font-size: 30px;
        }
    </style>
</head>
<body>

<h2>The id Attribute</h2>
<p>Use CSS to style an element with the id "myHeader":</p>

<h1 id="myHeader">Bajarangi soft</h1>

</body>
</html>
2.Differences Between  the Id and Classes 
<!DOCTYPE html>
<html>
<head>
    <style>
        #myHeader {
            background-color: lightblue;
            color: black;
            padding: 40px;
            text-align: center;
            font-size: 40px;
        }
        h2{
            color:red;
            font: 30px;
        }
        p{
            color:red;
            font-size: 30px;
        }

        /* Style all elements with the class name "city" */
        .city {
            background-color: tomato;
            color: white;
            padding: 10px;
        }

    </style>
</head>
<body>

<h2>The id Attribute</h2>
<p>Use CSS to style an element with the id "myHeader":</p>

<h1 id="myHeader">Bajarangi soft</h1>

<!-- Multiple elements with same class -->
<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>
3.Html Bookmarks with Id and Links
<!DOCTYPE html>
<html>
<body>

<p><a href="#C4">Jump to Chapter 4</a></p>
<p><a href="#C10">Jump to Chapter 10</a></p>

<h2>Chapter 1</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 2</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 3</h2>
<<p>This chapter explains web designing</p>

<h2 id="C4">Chapter 4</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 5</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 6</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 7</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 8</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 9</h2>
<p>This chapter explains web designing</p>

<h2 id="C10">Chapter 10</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 11</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 12</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 13</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 14</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 15</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 16</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 17</h2>
<p>This chapter explains web designing</p>
<h2>Chapter 18</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 19</h2>
<p>This chapter explains web designing</p>

<h2>Chapter 20</h2>
<p>This chapter explains web designing</p>>

<h2>Chapter 21</h2>
<p>This chapter explains web designing</p>
</body>
</html>
4.Using Id Attribute in Javascript
<!DOCTYPE html>
<html>
<body>

<h2>Using The id Attribute in JavaScript</h2>
<p>JavaScript can access an element with a specified id by using the getElementById() method:</p>

<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>

<script>
    function displayResult() {
        document.getElementById("myHeader").innerHTML = "Have a nice day!";
    }
</script>

</body>
</html>

Related Post