Home > Web Front-end > H5 Tutorial > body text

Common incorrect usage in html5

王林
Release: 2021-01-13 10:47:53
forward
2051 people have browsed it

Common incorrect usage in html5

Six common incorrect usages in html5:

1. Do not use section as a substitute for div

(Learning video sharing : html video tutorial)

One of the most common mistakes people make when using tags is to arbitrarily equate HTML5's

with
- specifically , that is, used directly as a replacement (for styles). In XHTML or HTML4, we often see code like this:

<!-- HTML 4-style code --><div id="wrapper">
    <div id="header">
        <h1>My super duper page</h1>
        Header content  </div>
    <div id="main">
        Page content    </div>
    <div id="secondary">
        Secondary content   </div>
    <div id="footer">
        Footer content  </div></div>
Copy after login

But now in HTML5, it will be like this:
Please do not copy these codes! This is wrong!

<section id="wrapper">
    <header>
        <h1>My super duper page</h1>
        <!-- Header content -->
    </header>
    <section id="main">
        <!-- Page content -->
    </section>
    <section id="secondary">
        <!-- Secondary content -->
    </section>
    <footer>
        <!-- Footer content -->
    </footer></section>
Copy after login

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 divs.
Based on the above ideas, the following are examples of correct use of HTML5 and some ARIA roles features (note that according to your own design, you may also need to add div)

<body><header>
    <h1>My super duper page</h1>
    <!-- Header content --></header><div role="main">
    <!-- Page content --></div><aside role="complementary">
    <!-- Secondary content --></aside><footer>
    <!-- Footer content --></footer></body>
Copy after login

If you are still not sure about using Which element, then I suggest you refer to HTML5 sectioning content element flowchart
2. Only use header and hgroup 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 auxiliary text, which is often used As the header of the section. When the header has multiple layers of structure, such as sub-headers, subtitles, various logos, etc., use hgroup to combine h1-h6 elements as the header of the section.
Abuse of header

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

    <header>
        <h1>My best blog post</h1>
    </header>
    <!-- Article content --></article>
Copy after login

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 extra code. Simply write it like this: Wrong use of

<article>
    <h1>My best blog post</h1>
    <!-- Article content --></article>
Copy after login


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

If there is only one sub-header, hgroup will work fine by itself. . . Isn’t this nonsense?
The first question is usually like this:
Please do not copy this code! No hgroup is needed here –>

    <hgroup>
        <h1>My best blog post</h1>
    </hgroup>
    <p>by Rich Clark</p></header>
Copy after login

In this example, just remove the hgroup. Let the heading run smoothly.

<header>
    <h1>My best blog post</h1>
    <p>by Rich Clark</p></header>
Copy after login

The second question is another unnecessary example:
Please do not copy this code! No header is needed here –>

    <hgroup>
        <h1>My company</h1>
        <h2>Established 1893</h2>
    </hgroup></header>
Copy after login

If header is the only child element It's hgroup, so what else needs to be done with the header? If there are no other elements in the header (such as multiple hgroups), just remove the header directly. (Web front-end learning exchange group: 328058344 No chatting, no entry unless you are interested!)

<hgroup>
    <h1>My company</h1>
    <h2>Established 1893</h2></hgroup>
Copy after login

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

With the introduction of HTML5, 30 With new elements (as of the time of the original publication), 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.​ ​
The key word is "main" navigation. Of course we could spend the whole day telling each other what "primary" means. And I personally define it like this:

  • Main navigation

  • Site search

  • two Level navigation (slightly controversial)

  • In-page navigation (such as a very long article)

  • Since there is no absolute right or wrong, so According to an informal vote and my own explanation, in the following situations, whether you put it or not, I will not put it anyway

  • Paging Control

  • Social links (although some social links are also primary navigation, such as "About" and "Favorites")

  • Tags for blog posts

  • Blog Article classification

  • Three-level navigation

  • 过长的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>
Copy after login
<!-- 请不要复制这段代码!这也是错的 --><header>
    <figure> ![My company](/img/mylogo.png) </figure></header>
Copy after login

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

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

这是个常见的问题,但并不是一个错误,我认为我们应该通过最佳实践来避免这种风格。
在HTML5中,script和style元素不再需要type属性。然而这些很可能会被你的CMS自动加上,所以要移除也不是那么的轻松。但如果你是手工编码或者你完全可以控制你的模板的话,那真的没有什么理由再去包含type属性。所有的浏览器都认为脚本是javascript而样式是css样式,你没必要再多此一举了。

<!-- 请不要复制这段代码!它太冗余了! --><link type="text/css" rel="stylesheet" href="css/styles.css" /><script type="text/javascript" src="js/scripts" /></script>
Copy after login

其实只需要这样写:

<link rel="stylesheet" href="css/styles.css" /><script src="js/scripts" /></script>
Copy after login

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

六、form属性的错误使用

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

  • autofocus

  • autocomplete

  • required

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

<!-- 请不要复制这段代码! 这是错的! --><input type="email" name="email" required="true" /><!--
Copy after login
 另一个错误的例子 --><input type="email" name="email" required="1" />
Copy after login

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

<!-- 请不要复制这段代码! 这是错的! --><input type="email" name="email" required="false" />
Copy after login

解析器仍然会将required属性视为有效并执行相应的行为,尽管你试着告诉它不要去执行了。这显然不是你想要的。
有三种有效的方式去使用布尔属性。(后两种只在xthml中有效)

  • required

  • required=””

  • required=”required”

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

<input type="email" name="email" required />
Copy after login

相关推荐:html5教程

The above is the detailed content of Common incorrect usage in html5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!