search
HomeWeb Front-endH5 TutorialSummary of various incorrect usages of H5

This time I will bring you a summary of the various incorrect uses of H5. What are the various incorrect uses of H5? Here are practical cases, let’s take a look.

1. Do not use section as a substitute for p

One of the most common mistakes people make when using tags is to use HTML5 randomly. The

is equivalent to

- specifically, used directly as a replacement (for styling purposes). In XHTML or HTML4, we often see code like this:

<!-- HTML 4-style code --><p>
    </p><p>
        </p><h1 id="My-super-duper-page">My super duper page</h1>
        Header content  

    

        Page content    

    

        Secondary content   

    

        Footer content  

But now in HTML5, it will be like this:

Please do not copy these codes! This is wrong!

<section>
    <header>
        <h1 id="My-super-duper-page">My super duper page</h1>
        <!-- Header content -->
    </header>
    <section>
        <!-- Page content -->
    </section>
    <section>
        <!-- Secondary content -->
    </section>
    <footer>
        <!-- Footer content -->
    </footer></section>

This usage is incorrect: **

is not a style container. The **section element represents the semantic part of the content used to help build a document summary. It should contain a header. If you're looking for an element that acts as a page container (like HTML or XHTML style), then consider writing the style directly to the body element, as Kroc Camen suggests. If you still need additional style containers, stick with p.

Based on the above ideas, the following are examples of correct use of HTML5 and some ARIA roles features (note that you may also need to add p according to your own design)

<header>
    <h1 id="My-super-duper-page">My super duper page</h1>
    <!-- Header content --></header><p>
    <!-- Page content --></p><aside>
    <!-- Secondary content --></aside><footer>
    <!-- Footer content --></footer>

2. Use header and hgroup only when needed

It is of course meaningless to write tags that do not need to be written. Unfortunately, I often see headers and hgroups misused for no purpose. You can read the two articles about header and hgroup elements for a detailed understanding. I briefly summarize the content as follows:

  • The header element represents a set of introductory or navigational properties auxiliary text, often used as the header of a section

  • When the header has multiple layers of structure, such as sub-headers, subtitles, various logo texts, etc., use hgroup to separate h1 -h6 elements are combined as the header of the section

  • Abuse of header

Since header can be used multiple times in a document, it may cause This coding style is welcomed:
Please do not copy this code! The header is not needed here ->

    <header>
        <h1 id="My-best-blog-post">My best blog post</h1>
    </header>
    <!-- Article content -->

If your header element contains only one header element, then discard the header element. Since the article element already guarantees that the header will appear in the document summary, and the header cannot contain multiple elements (as defined above), why write redundant code. Simply write it like this: Incorrect use of

<article>
    <h1 id="My-best-blog-post">My best blog post</h1>
    <!-- Article content --></article>

On the topic of headers, I often see the incorrect use of hgroup. Sometimes hgroups and headers should not be used at the same time:

  • If there is only one sub-header

  • if hgroup itself will work just fine. . . Isn’t this nonsense?

The first question is usually like this:

请不要复制这段代码!此处不需要hgroup –>
    <hgroup>
        <h1 id="My-best-blog-post">My best blog post</h1>
    </hgroup>
    <p>by Rich Clark</p>

In this example, just remove the hgroup and let the heading run.

<header>
    <h1 id="My-best-blog-post">My best blog post</h1>
    <p>by Rich Clark</p></header>

The second question is another unnecessary example:

Please do not copy this code! No header is needed here –>

    <hgroup>
        <h1 id="My-company">My company</h1>
        <h2 id="Established">Established 1893</h2>
    </hgroup>

If header is the only one The sub-element of is hgroup, so why does the header still need to be used? If there are no other elements in the header (such as multiple hgroups), just remove the header directly.

<hgroup>
    <h1 id="My-company">My company</h1>
    <h2 id="Established">Established 1893</h2></hgroup>

3. Don’t put all list-style links in nav

With the introduction of 30 new elements in HTML5 (as of When the original article was published), our choices in constructing semantic and structured tags have also become somewhat careless. That said, we shouldn't abuse hyper-semantic elements. Unfortunately, nav is an example of such an abuse. The specification of the nav element is described as follows:
The nav element represents a block in the page that links to other pages or other parts of this page; a block that contains navigation connections.

Note: Not all links on the page need to be placed in the nav element - this element is intended to be used as the main navigation block. To give a specific example, there are often many links in the footer, such as terms of service, home page, copyright statement page, etc. The footer element itself is sufficient to handle these situations. Although the nav element can also be used here, we usually think it is unnecessary.

关键的词语是“主要的”导航。当然我们可以互相喷上一整天什么叫做“主要的”。而我个人是这样定义的:

  • 主要的导航

  • 站内搜索

  • 二级导航(略有争议)

  • 页面内导航(比如很长的文章)

  • 既然并没有绝对的对错,所以根据一个非正式投票以及我自己的解释,以下的情况,不管你放不放,我反正不放在中:

  • 分页控制

  • 社交链接(虽然有些社交链接也是主要导航,比如“关于”“收藏”)

  • 博客文章的标签

  • 博客文章的分类

  • 三级导航

  • 过长的footer

  • 如果你不确定是否要将一系列的链接放在nav中,问你自己:“它是主要的导航吗?”为了帮助你回答这个问题,考虑以下首要原则:

  • 如果使用section和hx也同样合适,那么不要用nav — Hixie on IRC

为了方便访问,你会在某个“快捷跳转”中给这个nav标签加一个链接吗?

如果这些问题的答案是“不”,那就跟鞠个躬,然后独自离开吧。

四、figure元素的常见错误

figure以及figcaption的正确使用,确实是难以驾驭。让我们来看看一些常见的错误,

不是所有的图片都是figure

上文中,我曾告诉各位不要写不必要的代码。这个错误也是同样的道理。我看到很多网站把所有的图片都写作figure。看在图片的份上请不要给它加额外的标签了。你只是让你自己蛋疼,而并不能使你的页面内容更清晰。
规范中将figure描述为“一些流动的内容,有时候会有包含于自身的标题说明。一般在文档流中会作为独立的单元引用。”这正是figure的美妙之处——它可以从主内容页移动到sidebar中,而不影响文档流。
这些问题也包含在之前提到的HTML5 element flowchart中。

如果纯粹只是为了呈现的图,也不在文档其他地方引用,那就绝对不是

。其他视情况而定,但一开始可以问自己:“这个图片是否必须和上下文有关?”如果不是,那可能也不是(也许是个)。继续:“我可以把它移动到附录中吗?”如果两个问题都符合,则它可能是。
Logo并不是figure

进一步的说,logo也不适用于figure。下面是我常见的一些代码片段:

<!-- 请不要复制这段代码!这是错的 --><header>
    <h1>
        <figure> ![My company](/img/mylogo.png) </figure>
        My company name </h1></header>
<!-- 请不要复制这段代码!这也是错的 --><header>
    <figure> ![My company](/img/mylogo.png) </figure></header>

没什么好说的了。这就是很普通的错误。我们可以为logo是否应该是H1标签而互相喷到牛都放完回家了,但这里不是我们讨论的焦点。真正的问题在于figure元素的滥用。figure只应该被引用在文档中,或者被section元素围绕。我想你的logo并不太可能以这样的方式引用吧。很简单,请勿使用figure。你只需要这样做:

<header>
    <h1 id="My-company-name">My company name</h1>
    <!-- More stuff in here --></header>

Figure也不仅仅只是图片

另一个常见的关于figure的误解是它只被图片使用。figure可以是视频,音频,图表,一段引用文字,表格,一段代码,一段散文,以及任何它们或者其他的组合。不要把figure局限于图片。web标准的职责是精确的用标签描述内容。

五、不要使用不必要的type属性

这是个常见的问题,但并不是一个错误,我认为我们应该通过最佳实践来避免这种风格。
 

在HTML5中,script和style元素不再需要type属性。然而这些很可能会被你的CMS自动加上,所以要移除也不是那么的轻松。但如果你是手工编码或者你完全可以控制你的模板的话,那真的没有什么理由再去包含type属性。所有的浏览器都认为脚本是javascript而样式是css样式,你没必要再多此一举了。

<!-- 请不要复制这段代码!它太冗余了! --><link><script></script>

其实只需要这样写:

<link><script></script>

甚至指定字符集的代码都可以省略掉。Mark Pilgrim在pe into HTML5的语义化一章中作出了解释。

六、form属性的错误使用

HTML5引入了一些form的新属性,以下是一些使用上的注意事项

布尔属性

一些多媒体元素和其他元素也具有布尔属性。这里所说的规则也同样适用。
有一些新的form属性是布尔型的,意味着它们只要出现在标签中,就保证了相应的行为已经设置。这些属性包括:

  • autofocus

  • autocomplete

  • required

坦白的说,我很少看到这样的。以required为例,常见的是下面这种:

<!-- 请不要复制这段代码! 这是错的! --><input><!--
 另一个错误的例子 --><input>

严格来说,这并没有大碍。浏览器的HTML解析器只要看到required属性出现在标签中,那么它的功能就会被应用。但是如果你反过来写equired=”false”呢?

<!-- 请不要复制这段代码! 这是错的! --><input>

解析器仍然会将required属性视为有效并执行相应的行为,尽管你试着告诉它不要去执行了。这显然不是你想要的。

有三种有效的方式去使用布尔属性。(后两种只在xthml中有效)

  • required

  • required=””

  • required=”required”

上述例子的正确写法应该是:

<input>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

H5读取文件并上传到服务器的方法

select下拉框的右边怎么增加提示图标

The above is the detailed content of Summary of various incorrect usages of H5. 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
html5的div一行可以放两个吗html5的div一行可以放两个吗Apr 25, 2022 pm 05:32 PM

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别是什么html5中列表和表格的区别是什么Apr 28, 2022 pm 01:58 PM

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

html5怎么让头和尾固定不动html5怎么让头和尾固定不动Apr 25, 2022 pm 02:30 PM

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有哪些html5中不支持的标签有哪些Mar 17, 2022 pm 05:43 PM

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了哪个列表标签html5废弃了哪个列表标签Jun 01, 2022 pm 06:32 PM

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是什么意思html5是什么意思Apr 26, 2021 pm 03:02 PM

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

Html5怎么取消td边框Html5怎么取消td边框May 18, 2022 pm 06:57 PM

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft