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

Summary of various incorrect usages of H5

php中世界最好的语言
Release: 2018-03-27 14:13:03
Original
3670 people have browsed it

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 id="wrapper">
    <p id="header">
        <h1>My super duper page</h1>
        Header content  </p>
    <p id="main">
        Page content    </p>
    <p id="secondary">
        Secondary content   </p>
    <p id="footer">
        Footer content  </p></p>
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 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)

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

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

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

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>My best blog post</h1>
    </hgroup>
    <p>by Rich Clark</p></header>
Copy after login

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

<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 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>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 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>
Copy after login

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

<header>
    <h1>My company name</h1>
    <!-- More stuff in here --></header>
Copy after login

Figure也不仅仅只是图片

另一个常见的关于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在pe into HTML5的语义化一章中作出了解释。

六、form属性的错误使用

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

布尔属性

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

  • autofocus

  • autocomplete

  • required

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

<!-- 请不要复制这段代码! 这是错的! --><input type="email" name="email" required="true" /><!--
 另一个错误的例子 --><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

相信看了本文案例你已经掌握了方法,更多精彩请关注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!

Related labels:
source:php.cn
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