Common pitfalls of "go" HTML5 tags

Source: Internet
Author: User

The recent group of HTML5 label Learning, the method is that everyone selects a few tags, their own first to learn, and then to explain to you. This process is still very rewarding. But now HTML5 is still in the draft stage, and some of the new labeling elements are often explained, Even the label to move out is also very frequent (such as Hgroup), while the existing large Web portal in the use of HTML5 also have no good examples can be consulted, so that everyone's learning process more groping. Here is an article I saw in the html5doctor above, at present everybody ignorant stage, Perhaps it would be easier to read the master's explanation. As a result of Caishuxueqian, many do not understand the place may just do a literal translation, the wrong place also ask everyone to advise.


The following is attached to the original address: avoiding common HTML5 Mistakes Author: Richard Clark, where you have questions, you can check English.

In this article, I will share with you the small errors and bad practices of the HTML5 build page, and let us avoid these errors in future work.


Don't define a style with <Section> as a simple container

One of the mistakes we often see is to arbitrarily replace the <div> tag with a <section> tag, especially as a <div> that surrounds the container with <section>. In XHTML or HTML4, we will see code similar to the following:

    1. <!--HTML 4-style code---
    2. <div id= "wrapper" >
    3. <div id= "Header" >
    4. <!--Header Content--
    5. </div>
    6. <div id= "Main" >
    7. <!--Page Content--
    8. </div>
    9. <div id= "Secondary" >
    10. <!--secondary Content--
    11. </div>
    12. <div id= "Footer" >
    13. <!--Footer Content--
    14. </div>
    15. </div>

Now I see the following code look:

    1. <!--Don ' t copy this code! It ' s wrong! -->
    2. <section id= "wrapper"
    3.   
    4.    
    5.     <!--Header content-->
    6. &NBSP;&NB Sp;
    7.   <section id= "main"
    8.     <!--page content -->
    9.   </section>
    10.   <section id= "secondary";
    11.     <!--secondary content-->
    12.   </section>
    13.    <footer>
    14.     <!--footer content-->
    15.   </footer
    16. </section>

Visually, the above example is wrong:<section> not a container .<section> element is a semantic section that helps to build a document outline. It should contain a caption. If you are looking for an element that can contain pages (whether HTML or XHTML), it is common practice to define a style directly on the <body> tag, as Kroc Camen describes, if you need additional elements to define the style, use <DIV> As Dr Mike explains, Div is not extinct, and if there are no other more appropriate, Div may be the most appropriate choice for you.


Remember this, here we have revised the example above, by using two new characters. (Whether you need extra <div> depends on your design.) )

    1. <body>
    2. <!--Header Content--
    3. <div role= "Main" >
    4. <!--Page Content--
    5. </div>
    6. <aside role= "Complementary" >
    7. <!--secondary Content--
    8. </aside>
    9. <footer>
    10. <!--Footer Content--
    11. </footer>
    12. </body>

If you're still unsure which element is more suitable, I suggest you check out the HTML5 sectioning content element flowchart to keep you moving forward.


Use

It is unreasonable to write some unwanted phenomena when using tags. Unfortunately, it is often found that people use

    • The
    • The


Over-use

You certainly know that in a document you can use multiple

    1. <!--Don ' t copy this code! No need for headers here--
    2. <article>
    3. <!--article Content--
    4. </article>

If your

    1. <article>
    2. <!--article Content--
    3. </article>

In this topic of the title, you often see error cases using

    • There's only one headline,
    • Or

The first scenario looks like this:

    1. <!--Don ' t copy this code! No Need for Hgroup
    2. <p>by Rich clark</p>

In this case, the

    1. <p>by Rich clark</p>

The second case also contains tags that they do not need.

    1. <!--Don ' t copy this code! No need for headers here--

Why do we need an extra

Do not place all of the link lists on the <nav> tab

Of the 30 new elements added to HTML5 (as we write this article), when we build more semantically structured tags, our choices become too rich. That is to say, we are going to abuse these super-semantic tags that we now offer to us .<nav> is a very tragic example. The description in the specification is this:

The
nav element represents a, section of a page, links to other pages, or to parts within the page:a sections with Navigation links. Note:not all groups of the links on a page need to being in a nav element-the element are primarily intended for sections that consist of major navigation blocks. In particular, it's common for footers to has a short list of links to various pages of a site, such as the terms of ser Vice, the home page, and a copyright page. The footer element alone is sufficient for such cases; While a NAV element can being used in such cases, it is usually unnecessary.
WHATWG HTML Spec

The key word in this is "important" navigation. We may have different definitions of "important", but my understanding is:

    • Main navigation
    • Website search
    • Second-level navigation (this can be controversial)
    • Links within the page (e.g. a long article)

Although there is no right or wrong point, but according to my understanding and a poll made me feel that under these circumstances, I will not use the <nav> tag:

    • Page
    • Social links (although some social links are also the main link, like about me and taste flavours)
    • Tags for blog posts
    • Category List of blog posts
    • Third level navigation
    • Large footer

If you are not sure whether to use <nav>, then ask the following questions: "Is the person a primary link?" "You can answer the question you just made based on several factors:

    • If you can solve your problem with <section> and title tags, don't use <nav>–hixie on IRC
    • Are you adding a quick jump link to increase accessibility?

If the above answer is "no", it may not be appropriate to use <nav>.


Errors in <figure> elements

<figure> and often <figcaption> with it, it is difficult to grasp the label, the following is often seen some small errors.


Not all pictures are figures (note: More difficult to understand, image= pictures, figure= graphics)

Before, I once said don't write those unwanted tags. This error is also the same. I often see that every picture on a website has a <figure> tag. These additional tags will not bring you any benefits, but also add to your own intensity and make your content harder to understand.


The interpretation of <figure> in the specification is as follows: "Some stream content, which can have a title, self-contained, and usually as a unit independent of the inner document flow. "There is a perfect representation that it can be removed from the main content-for example, in the sidebar, without affecting the flow of documents."


It is not necessary to use <FIGURE> if it is just a picture of the class and does not have anything to do with the other content in the document. " Does this picture need to explain the content of the context? "If the answer is no, it may not be <figure> (probably <aside>)," Can I move it to the appendix? If the answer to both questions is yes, it could be <figure>.


Your logo is not a <figure>

Extend the above to your logo as well. Here are two sets of code snippets I've found that are regular:

    1. <!--Don ' t copy this code! It ' s wrong! -->
    2.   
    3.     <figure>
    4.       
    5.     </figure>
    6.     My company name
    7.   
    8. <!--Don ' t copy this code! It ' s wrong! -->
    9.   <figure>
    10.    
    11.   </figure>

There is no need to say anything, this is a very obvious mistake, maybe you think we are talking about the logo in the H1 tag, but we do not discuss this issue here. What confuses us is the <figure> element. <figure> tags are used only when there is a context to explain or be surrounded by <section>. What I'm saying here is that your logo may rarely be used in this case. It's simple, so don't do it, you just need to look like this.

    1. <!--more stuff on here--

Figure can only be used on the label of misunderstanding

Another misconception about <figure> is that we generally think that it can only be used on images. In fact this is not the case, it can be used in <video><audio>, an icon (such as <svg>,), a reference, a table, a piece of code, a piece of prose, or any combination of those related. Do not confine your <figure> tags to the image alone. The task of our web-maker is to use labels to describe content more accurately.


Here is a more in-depth explanation <figure> article I wrote about <figure> it's worth reading.


Do not use those unnecessary type attributes

This may be our most common problem, they are not real mistakes, but I think our best practice is to try to avoid this pattern.


In HTML5, we do not need to add the type attribute to <script> and <script>, if it is painful to remove these from the CMS default content, Then you can delete them when you manually encode them or you can completely control your template when you write them down. Because all browsers will parse <script> into JavaScript and <css> tags are CSS, you no longer need that type attribute:

    1. <!--Don ' t copy this code! It ' s attribute overload! -
    2. <link type= "Text/css" rel= "stylesheet" href= "Css/styles.css"/>
    3. <script type= "Text/javascript" src= "Js/scripts.js" ></script>

Now we can write the following:

    1. <link rel= "stylesheet" href= "Css/styles.css"/>
    2. <script src= "Js/scripts.js" ></script>

You can also omit the encoding settings. Mark Pilgrim An explanation in the semantic chapter of dive into HTML5.


Do not include the wrong form properties

You may find that


Boolean property

Several of the newly introduced tag properties are Boolean, and their tag behavior is basically close. These properties include:

    • Autofocus
    • AutoComplete
    • Required

Frankly, the following situation is not common to me, but we take the example from required as follows:

    1. <!--Don ' t copy this code! It ' s wrong! -
    2. <input type= "Email" name= "email" required= "true"/>
    3. <!--Another bad example--
    4. <input type= "Email" name= "email" required= "1"/>

Basically, this piece of code doesn't hurt. The client's parsing of the HTML will take effect when it encounters the required tag attribute. But what happens when we change the code and enter Required= "false"?

    1. <!--Don ' t copy this code! It ' s wrong! -
    2. <input type= "Email" name= "email" required= "false"/>

The required attribute is still encountered when parsing, although the behavior you wish to join is false, it will still be parsed into true.


There are three reasonable ways to get the Boolean value into effect. (The second and third option is only required when you write XHTML parsing)


Our example above can be written as follows:

    1. <input type= "Email" name= "email" required/>
Article from: http://www.w3cfuns.com/article-5598818-1-1.html

Common pitfalls of "go" HTML5 tags

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.