HTML ID
In HTML we have an attribute called id attribute. It is like an identity card which is unique and can't be of the same name as others.
The id attribute is used to uniquely select an element in an HTML document.
It is used to add CSS property to the element as well as used for performing javascript tasks.
We can add CSS properties to the HTML elements using the id attribute.
Steps to use id attribute for adding CSS properties:
- create a <style> tag
<head> tag. - write name of your id preceding with a Hash sign ( # ).
- Make curly braces. Define the CSS properties inside curly braces.
- Create an id attribute inside the tag you want to define those CSS properties and give it the name of your class within a double quote.
<!DOCTYPE html>
<html>
<head>
<style>
#id-1 {
color: red;
background-color: orange;
padding: 20px;
font-size: 20px;
}
</style>
</head>
<body>
<h2>This is not given any id.</h2>
<p id="id-1">This paragraph has an id named "id-1".</p>
</body>
</html>
Try It
bookmark on a webpage using id
Bookmarks of a webpage allow the user to jump to a certain section of a long webpage. Instead of scrolling manually, you can just click on the bookmark and you will directly jump to that section of the webpage.
To create a bookmark, create a link ( which will be clicked ) and create a bookmark ( the section where to jump ) on the webpage.
Creating a bookmark: - Choose the element where you have to jump. give that element an id attribute and an id name.
<h4 id="bookmark1">Jump here section 25</h4>
Create a link to this bookmark: - Create an anchor tag ( <a> ) with href attribute bookmark name starting with a hash ( # ) sign.
<a href="#bookmark1">Go to section 25</a>
A working example.
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<h2>Webpage example jump to section 25.</h2>
<a href="#bookmark1">jump to section 25.</a>
<p>Section 1.</p>
<p>Section 2.</p>
<p>Section 3.</p>
.
.
.
<p id="bookmark1">Section 25.</p>
</body>
</html><!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<h2>Webpage example jump to section 25.</h2>
<a href="#bookmark1">jump to section 25.</a>
<p>Section 1.</p>
<p>Section 2.</p>
<p>Section 3.</p>
.
.
.
<p id="bookmark1">Section 25.</p>
</body>
</html>
Try It
Difference between class and id
- Once a class is defined it can be used as many times you want to use in the HTML document but an id can be used only once in an HTML document as id indicates unique identity.
- From the CSS point of view there is no difference between Class and id. They both can be used to add CSS property to the element.
- Classes are not unique but ids are unique.
- Classes do not have any browser ability but id have a special ability. When you have URL like http://yourwebsite.com#id1 then the browser will locate the id having name "id1" in the webpage and will scroll to "id1".
- An element can have both a class as well as an id.
<!DOCTYPE html>
<html>
<head>
<style>
.my-class {
color: red;
background-color: orange;
padding: 20px;
font-size: 20px;
}
#my-id {
color: blue;
background-color: lightpink;
padding: 25px;
font-size: 25px;
}
</style>
</head>
<body>
<h2>Class used multiple times but id is used only once.</h2>
<p id="my-id">I have id named "my-id".</p>
<p class="my-class">I have class named "my-class"</p>
<p class="my-class">I have class named "my-class"</p>
<p class="my-class">I have class named "my-class"</p>
<p class="my-class">I have class named "my-class"</p>
</body>
</html>
Try It
Selecting and id element by javascript
An id can be used to select and modify any element using javascript
We can select the id using document.getElementById("id-name") and call a function over any event targeting that id.
<!DOCTYPE html>
<html>
<head>
<script>
function change() {
document.getElementById("my-id").innerHTML = "Welcome to TutorialsTonight";
document.getElementById("my-id").style.fontSize = "30px";
document.getElementById("my-id").style.color = "red";
}
</script>
</head>
<body>
<h2>Selecting and changing element by id using javascript.</h2>
<button onclick="change()">modify id elemet</button>
<p id="my-id">Hello world!</p>
</body>
</html>
Try It