search
HomeWeb Front-endHTML TutorialHTML can't be written like this, did you make it?

目录  

a标签不可以嵌套交互式元素

块级元素可以包含内联元素和某些块级元素,内联元素不能包含块级元素,只能包含内联元素

p标签不能包含块级元素

不可包含块级元素的标签

li标签可以包含div以及ul,ul的子元素应该只有li

元素并排(块级和块级并列,内联和内联并列)

字符实体引发的错误

错误的使用 role 属性

行内元素强制转成块级元素,块级元素强制转成行内元素

使用 disabled=false

页面中同一个ID出现两次及以上

内嵌的 <script> 标签含有 src 属性</script>

标签错误嵌套

语法错误

* a标签不可以嵌套交互式元素[a, audio(如果设置了controls属性), button, details, embed, iframe, img(如果设置了usemap属性), input(如果type属性不为hidden状态), keygen, label, menu(如果type属性为toolbar状态),object(如果设置了usemap属性), select, textarea, video(如果设置了controls属性)]

下面这些写法浏览器是不能够正常解析的

<a href="">  <a href="">click</a> </a> 
 <a href="">  <button>click</button> </a> 
 <a href="">  <input type="text"> </a> 
 <a href="">  <textarea name="" id="" cols="10" rows="5"></textarea> </a> 
 
 <a href="">  <a href="">click</a> </a> <a href="">  
 <button>click</button> 
 </a> <a href="">  <input type="text"> </a> 
 <a href="">  
 <textarea name="" id="" cols="30" rows="10"></textarea> 
 </a>

有的虽然解析正常,但却达不到预想的目的

语义错误

页面可能正常解析,但不符合语义。这是因为浏览器自带容错机制,对于不规范的写法也能够正确的解析,各浏览器的容错机制不同,所以尽量按规范来写。

* 块级元素可以包含内联元素和某些块级元素,内联元素不能包含块级元素,只能包含内联元素

/*规范的写法*/ 
 <div>  <h2 id="jikexueyuan">jikexueyuan</h2>  <p>IT education</p> </div> 
 /*不规范的写法*/ 
 <span>  <div>wrong</div> </span>

* p标签不能包含块级元素

/*不规范的写法*/  <p>  <h1></h1> </p> 
 <p>  <div></div> </p>

* 如下的标签不可包含块级元素

h1、h2、h3、h4、h5、h6、p

* li标签可以包含div以及ul(这个是不是很牛,可以包含父级元素)

 /*规范的写法*/ 
 <li>  <ul>  <li>  </li>   <li>  </li>   <li>  <li>   </ul>  <div> </div> </li> 
 /*不规范的写法*/ 
 <ul>  <a href="">迷路的a标签</a>    <li></li>       <li></li>  <li></li> </ul>

* 元素并排(块级和块级并列,内联和内联并列)

/*规范的写法*/ 
 <div>  <h2></h2>  <p></p> </div>  <div>  <img src="/static/imghwm/default1.png"  data-src="" alt="  class="lazy"   alt="">  <a href=""></a>   <span></span> </div> 
 
 /*不规范的写法*/ <div>  <span>我是内联元素</span>  <p>我是块级元素</p> </div>

字符实体引发的错误

有些字符是 html 预留的,不能够直接书写,但是可以通过字符实体来显示。 如:  

空格 -  

大于符号 - >

& - &

......

但如果稍不注意,在如下的情况下就会引发错误:

<a href="?art&copy">Art and Copy</a>

这里的 © 会被转换为 符号,从而得不到预期的效果

正确的做法是所有的保留字符全部用实体字符替代。

 <a href="?art&copy">Art and Copy</a>

错误的使用 role 属性

role 属性使用来增强标签的语义的,但如果使用不当,反而得到负面的效果,所以使用的时候一定要注意。

<input type=radio role=progressbar>

这里的是一个 input标签,但是却通过 role 指定了进度条的语义,但是 input是不能够作为进度条的,所以这里反而模糊了语义。

行内元素强制转成块级元素,块级元素强制转成行内元素

如下做法是不推荐的:  

标签设置 inline-block 属性

标签设置 block 属性

使用 disabled=false

disabled 属性是用来禁用标签的,一般用于 input 、button等,表示不可点击。

正常的用法是:

<input type="text" name="lname" /> <input type="text" name="lname" disabled="disabled" />

错误的写法:

<input type="text" name="name" disabled="false" />

这里虽然 disabled="false" 语义也代表不禁用,但实际上是被禁用的。

页面中同一个 ID 出现两次及更多

ID重复会引起元素选择错误,从而引发 Javascript 隐藏问题,因此需要注意。

注:每一个ID会在浏览器中生成一个同名的全局变量

内嵌的 <script> 标签含有 src 属性</script>

当 <script> 标签包含 src 属性时,其包含的 Javascript 脚本是不会执行的。</script>

The above is the detailed content of HTML can't be written like this, did you make it?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the purpose of HTML tags?What is the purpose of HTML tags?Apr 28, 2025 am 12:02 AM

HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

What are self-closing tags? Give an example.What are self-closing tags? Give an example.Apr 27, 2025 am 12:04 AM

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools