h15 HTML CSS IDs

 

HTML "id" is an attribute used to uniquely identify an element within a web page. It serves as a label for that element and enables JavaScript and CSS to target it specifically. This identification helps in applying custom styles, making interactive features, and navigating within the webpage with precision. "id" values must be unique within the document.

Defining ID Attribute

In HTML and CSS, you can define an "id" attribute to uniquely identify an element and apply specific styles or JavaScript interactions. To define an "id" in HTML, simply add it to an element using the following format −

Syntax

<element id="your_id_name">Content</element>

 

Example

<p id="my-paragraph">This is a paragraph with an ID.</p>

 

In CSS, you can target this ID by using a hash symbol (#) followed by the ID name to apply styles −

Syntax

#your_id_name {
   property: value;
}

 

For the previous HTML example −

#my-paragraph {
   color: blue;
   font-size: 16px;
}

 

This allows you to uniquely style or interact with specific elements on your web page.

 

ID attribute in HTML

IDs are unique identifiers in HTML, used to select specific elements. They are valuable for styling and JavaScript manipulation. Below are two methods, each with an example of using IDs.

Using IDs for Styling (CSS)

In this method, we use IDs to style specific HTML elements using CSS. We define a unique ID, "special-text," and apply styles like color and font-weight to the element with that ID. It ensures that only the element with the specified ID receives these styles.

复制代码
<!DOCTYPE html>
<html>
<head>
   <style>
      #special-text {
         color: blue;
         font-weight: bold;
      }
   </style>
</head>
<body>
   <p>This is <span id="special-text">special</span> text. </p>
   <p>Regular text.</p>
</body>
</html>
复制代码

 

In this example, we apply CSS styles to the element with the ID "special-text."

Using IDs for JavaScript Interaction

In the below program, an ID called "special-text" is used to enable JavaScript interaction with a specific HTML element. The JavaScript function "changeText" is defined to change the content of the element when clicked.

复制代码
<!DOCTYPE html>
<html>
<head>
   <script>
      function changeText() {
         var element = document.getElementById("special-text");
         element.innerHTML = "new text";
      }
   </script>
</head>
<body>
   <p>This is <span id="special-text" onclick="changeText()">special</span> text. </p>
   <p>Regular text.</p>
</body>
</html>
复制代码

 

 

ID Attribute Rules in HTML

Make sure that the ID attribute follows the below rules or not −

  • In the ID attribute at least one character should be there, the starting letter should be a character (a-z) or (A-Z), and the rest of the letters of any type can written even special characters.

  • The ID attribute does not contain any spaces.

  • Within the document every ID must be unique.

Example

The following example shows the values of the ID attribute −

<div id = "#123"> .... </div>
<tag id = "#%ADF@"> .... </tag>
<h1 id = " _H"> .... </h1>
<div id = "@"> .... </tag>
<tag id = " {}"> .... </tag>

Before understanding whether these values for the ID attribute are valid or not, try to understand the class attribute which is explained in the class chapter, and the difference between class and ID attribute, it helps in understanding about ID attribute in detail.

 

Valid and Invalid ID attributes

Certain ID Attributes are valid in HTML 5, but not in Cascading Style Sheets. In such cases, it is recommended to go with simple output rather than styled output because certain values that we use for ID may be invalid in CSS.

The following example demonstrates the use of simple ID attributes using CSS −

Example

复制代码
<!DOCTYPE html>
<html>
<head>
   <title>Simple Id Attributes</title>
   <style>
      #@TP {
         color: #070770;
         text-align: center;
         font-size: 30px;
         font-weight: bold;
      }
      #@TP1 {
         text-align: center;
         font-size: 25px;
      }
   </style>
</head>
<body>
   <div id="@TP">TutorialsPoint Website</div>
   <div id="@TP1"> Html5 Tutorials, CSS Tutorials, JavaScript Tutorials </div>
</body>
</html>
复制代码

 

When we execute the above code, two div elements will be displayed, one with ID attribute (TutorialsPoint Website), and the other one with other ID attribute (Html5 Tutorials, CSS Tutorials, JavaScript Tutorials).

Now we will change the value IDs in the above example. ID’s values are valid in HTML 5 and in CSS. Therefore, we can get a styled output because of valid ID’s. Following is the example which demonstrate the functioning of valid ID’s −

复制代码
<!DOCTYPE html>
<html>
<head>
   <title>Simple Id Attributes</title>
   <style>
      #TP1 {
         color: #070770;
         text-align: center;
         font-size: 30px;
         font-weight: bold;
      }
      #TP2 {
         text-align: center;
         font-size: 25px;
      }
   </style>
</head>
<body>
   <div id="TP1">TutorialsPoint Website</div>
   <div id="TP2">Html5 Tutorials, CSS Tutorials, JavaScript Tutorials</div>
</body>
</html>
复制代码

 

When we execute the above program, two div elements with ID attributes are displayed.

Consider another example which demonstrates "Invalid ID’s not supported in JavaScript" notion.

复制代码
<!DOCTYPE html>
<html>
<body>
   <h1 id="1TP">TutorialsPoint WEBSITE Content</h1>
   <button onclick="Value()"> Display </button>
   <style>
      #.1TP {
         color: blue;
      }
   </style>
   <script>
      function Value() {
         document.getElementById("1TP").innerHTML = "TutorialsPoint";
      }
   </script>
</body>
</html>
复制代码

 

Consider another example which demonstrates Valid ID’s that are supported by JavaScript and HTML.

复制代码
<!DOCTYPE html>
<html>
<body>
   <h1 id="TP">TutorialsPoint WEBSITE Content</h1>
   <button onclick="displayResult()"> Display </button>
   <style>
      #TP {
         color: blue;
      }
   </style>
   <script>
      function displayResult() {
         document.getElementById("TP").innerHTML = "TutorialsPoint";
      }
   </script>
</body>
</html>
复制代码

 

When we run the above program, an ID attribute will be displayed along with the button labeled as display.

When ID value is taken only in characters, then JavaScript and HTML supports the ID and When Clicked on the display button the below page will open.

 

posted @   emanlee  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2014-05-18 Windows Myeclipse 10 安装 Perl 插件
2010-05-18 14个优化网站性能提高网站访问速度技巧
2009-05-18 如何查看sql server版本号
2009-05-18 BlogEngine.NET Extensions
2009-05-18 BlogEngine.NET Widgets
2008-05-18 C语言程序设计 练习题参考答案 第八章 文件(2)
2008-05-18 C语言程序设计 练习题参考答案 第八章 文件(1)
点击右上角即可分享
微信分享提示