首页 > 其他分享 >HTML 15 - CSS IDs

HTML 15 - CSS IDs

时间:2024-05-18 16:10:46浏览次数:24  
标签:15 text JavaScript IDs HTML attribute ID CSS

 

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.

 

标签:15,text,JavaScript,IDs,HTML,attribute,ID,CSS
From: https://www.cnblogs.com/emanlee/p/18190425

相关文章

  • 代码随想录算法训练营第十一天 | 20.有效的括号 1047.删除字符串中的所有相邻 重复项
    20.有效的括号题目链接文章讲解视频讲解思路:遍历字符串,如果栈不为空,则进行匹配   如果匹配则出栈,否则入栈   如果栈为空,直接入栈   遍历结束后栈为空则说明全部匹配,否则没有全部匹配classSolution{public:boolisValid(strings){stack<cha......
  • 152. 乘积最大子数组
    给你一个整数数组nums,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。测试用例的答案是一个32-位整数。示例1:输入:nums=[2,3,-2,4]输出:6解释:子数组[2,3]有最大乘积6。示例2:输入:nums=[-2,0,-1]输......
  • HTML 16 - Images
    HTMLImagesprovidesavisualcontentforwebpages,enhancinguserexperiencesandconveyinginformation.Theycanbephotographs,graphics,icons,orillustrations.ToinsertanimageonHTMLdocumentwecanuse<img>tag.ReasontouseImagesHTM......
  • HTML 14 - CSS Classes
    InHTML,aclassisanattributethatcanbeappliedtooneormoreelementsandisusedtostyleandcategorizeelementsbasedoncommoncharacteristicsorpurpose.Classesallowsmultipleelementstosharethesamestylingrules.Byassigningthesamec......
  • HTML 13 - Style Sheet
     CSS,orCascadingStyleSheets,isatoolthatdefineshowwebdocumentslookonscreensorinprint.Sinceitsintroductionin1994,theW3Chasencouragedtheuseofstylesheetsforwebdesign.CSSletsyoucontrolthepresentationofyourcontent,w......
  • HTML 12 - Meta Tags
     HTMLletsyouspecifymetadata,whichisadditionalimportantinformationaboutadocument,inavarietyofways.TheMETAelementscanbeusedtoincludename/valuepairsdescribingpropertiesoftheHTMLdocument,suchasauthor,expirydate,alisto......
  • HTML 11 - Phrase Tags
     Thephrasetagshavebeendesignedforspecificpurposes,thoughtheyaredisplayedinasimilarwayasotherbasictagslike<b>,<i>,<pre>,and<tt>,asyouhaveseeninthepreviouschapter.Thischapterwilltakeyouthrough......
  • HTML 10 - Comments
    HTMLCommentsareusedtocommentinHTMLcodes,sothedevelopercanunderstandthepurposeofthatcodesectionanditishelpfullfordebuggingalso.Ifwearefacingsomeissuebecouseofanyparticularelementwecahcheckitbycommentingoutthate......
  • 代码随想录算法训练营第第11天 | 20. 有效的括号 、1047. 删除字符串中的所有相邻重
    今天的题主要是关于栈的,比较简单,一次性过20.有效的括号讲完了栈实现队列,队列实现栈,接下来就是栈的经典应用了。大家先自己思考一下有哪些不匹配的场景,在看视频我讲的都有哪些场景,落实到代码其实就容易很多了。题目链接/文章讲解/视频讲解:https://programmercarl.com/0020.......
  • HTML 09 - Quotations
     QuotationsinHTMLallowyoutoincludeandformatquotedtextwithinyourwebcontent.HTMLprovidestagssuchas<blockquote>,<q>,<cite>,<address>,<bdo>and<abbr>tostructureandstylequotes.Thesetagshelp......