博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
html5语义化标记元素_语义HTML5元素介绍
阅读量:2521 次
发布时间:2019-05-11

本文共 9852 字,大约阅读时间需要 32 分钟。

html5语义化标记元素

Semantic HTML elements are those that clearly describe their meaning in a human- and machine-readable way.

语义HTML元素是以人类和机器可读的方式清楚地描述其含义的元素。

Elements such as <header>, <footer> and <article> are all considered semantic because they accurately describe the purpose of the element and the type of content that is inside them.

诸如<header><footer><article>元素都被认为是语义的,因为它们准确地描述了元素的用途以及它们内部内容的类型。

什么是语义元素? (What are Semantic Elements?)

HTML was originally created as a markup language to describe documents on the early internet. As the internet grew and was adopted by more people, its needs changed.

HTML最初是作为一种标记语言创建的,用于描述早期Internet上的文档。 随着互联网的发展和越来越多的人采用互联网,其需求发生了变化。

Where the internet was originally intended for sharing scientific documents, now people wanted to share other things as well. Very quickly, people started wanting to make the web look nicer.

互联网最初旨在​​共享科学文档,而现在人们也希望共享其他内容。 很快,人们开始希望使网络看起来更好。

Because the web was not initially built to be designed, programmers used different hacks to get things laid out in different ways. Rather than using the <table></table> to describe information using a table, programmers would use them to position other elements on a page.

因为最初并不是为设计网络而设计的,所以程序员使用不同的技巧来以不同的方式布置事物。 程序员不是使用<table></table>来使用<table></table>来描述信息,而是会使用它们在页面上放置其他元素。

As the use of visually designed layouts progressed, programmers started to use a generic “non-semantic” tag like <div>. They would often give these elements a class or id attribute to describe their purpose. For example, instead of <header> this was often written as <div class="header">.

随着视觉设计布局的使用的发展,程序员开始使用通用的“非语义”标记,例如<div> 。 他们通常会给这些元素一个classid属性来描述它们的目的。 例如,它通常代替<header>编写为<div class="header">

As HTML5 is still relatively new, this use of non-semantic elements is still very common on websites today.

由于HTML5仍然是相对较新的,因此在当今的网站上,非语义元素的这种使用仍然非常普遍。

新语义元素列表 (List of new semantic elements)

The semantic elements added in HTML5 are:

HTML5中添加的语义元素是:

  • <article>

    <article>

  • <aside>

    <aside>

  • <details>

    <details>

  • <figcaption>

    <figcaption>

  • <figure>

    <figure>

  • <footer>

    <footer>

  • <header>

    <header>

  • <main>

    <main>

  • <mark>

    <mark>

  • <nav>

    <nav>

  • <section>

    <section>

  • <summary>

    <summary>

  • <time>

    <time>

Elements such as <header>, <nav>, <section>, <article>, <aside>, and <footer> act more or less like <div> elements. They group other elements together into page sections. However where a <div> tag could contain any type of information, it is easy to identify what sort of information would go in a semantic <header> region.

诸如<header><nav><section><article><aside><footer>类的元素或多或少类似于<div>元素。 他们将其他元素组合到页面部分中。 但是,在<div>标记可以包含任何类型的信息的地方,很容易确定语义<header>区域中将包含哪种信息。

An example of semantic element layout by w3schools

w3schools的语义元素布局示例

为什么要使用语义元素? (Why use semantic elements?)

To look at the benefits of semantic elements, here are two pieces of HTML code. This first block of code uses semantic elements:

为了查看语义元素的好处,这里有两段HTML代码。 第一部分代码使用语义元素:

Whilst this second block of code uses non-semantic elements:

尽管第二个代码块使用非语义元素:

First, it is much easier to read. This is probably the first thing you will notice when looking at the first block of code using semantic elements. This is a small example, but as a programmer you can be reading through hundreds or thousands of lines of code. The easier it is to read and understand that code, the easier it makes your job.

首先,它更容易阅读 。 当使用语义元素查看第一段代码时,这可能是您会注意到的第一件事。 这是一个小例子,但是作为程序员,您可以阅读成百上千的代码行。 越容易阅读和理解该代码,就越容易完成工作。

It has greater accessibility. You are not the only one that finds semantic elements easier to understand. Search engines and assistive technologies (like screen readers for users with a sight impairment) are also able to better understand the context and content of your website, meaning a better experience for your users.

它具有更大的可访问性 。 您不是唯一发现语义元素更容易理解的人。 搜索引擎和辅助技术(例如视力障碍用户的屏幕阅读器)也可以更好地了解您网站的上下文和内容,从而为用户带来更好的体验。

Overall, semantic elements also lead to more consistent code. When creating a header using non-semantic elements, different programmers might write this as <div class="header">, <div id="header">, <div class="head">, or simply <div>. There are so many ways that you can create a header element, and they all depend on the personal preference of the programmer. By creating a standard semantic element, it makes it easier for everyone.

总体而言,语义元素还会导致代码更加一致 。 使用非语义元素创建标头时,不同的程序员可能将其写为<div class="header"><div id="header"><div class="head">或简称为<div> 。 创建标题元素的方法有很多,它们都取决于程序员的个人喜好。 通过创建标准语义元素,它使每个人都更容易。

Since October 2014, HTML4 got upgraded to HTML5, along with some new “semantic” elements. To this day, some of us might still be confused as to why so many different elements that doesn’t seem to show any major changes.

自2014年10月起,HTML4和一些新的“语义”元素已升级为HTML5。 时至今日,我们中的某些人可能仍对为什么这么多不同的元素似乎没有显示出任何重大变化感到困惑。

<section><article> (<section> and <article>)

“What’s the difference?”, you may ask. Both these elements are used for sectioning a content, and yes, they can definitely be used interchangeably. It’s a matter of in which situation. HTML4 offered only one type of container element, which is <div>. While this is still used in HTML5, HTML5 provided us with <section> and <article> in a way to replace <div>.

您可能会问:“有什么区别?”。 这两个元素都用于分割内容,是的,它们可以互换使用。 这取决于哪种情况。 HTML4仅提供一种容器元素,即<div> 。 尽管在HTML5中仍使用此功能,但HTML5为我们提供了<section><article>来代替<div>

The <section> and <article> elements are conceptually similar and interchangeable. To decide which of these you should choose, take note of the following:

<section><article>元素在概念上相似并且可以互换。 要决定应选择哪种方式,请注意以下几点:

  1. An article is intended to be independently distributable or reusable.

    文章旨在独立分发或可重复使用。
  2. A section is a thematic grouping of content.

    一节是内容的主题分组。

Top Stories

News

Story 1
Story 2
Story 3

Sport

Story 1
Story 2
Story 3

<header><hgroup> (<header> and <hgroup>)

The <header> element is generally found at the top of a document, a section, or an article and usually contains the main heading and some navigation and search tools.

<header>元素通常位于文档,节或文章的顶部,通常包含主要标题以及一些导航和搜索工具。

Company A

The <hgroup> element should be used where you want a main heading with one or more subheadings.

<hgroup>元素应用于需要一个或多个子标题的主标题的地方。

Heading 1

Subheading 1

Subheading 2

REMEMBER, that the <header> element can contain any content, but the <hgroup> element can only contain other headers, that is <h1> to <h6> and including <hgroup>.

请记住, <header>元素可以包含任何内容,但是<hgroup>元素只能包含其他标题,即<h1><h6>并包括<hgroup>

<aside> (<aside>)

The <aside> element is intended for content that is not part of the flow of the text in which it appears, however still related in some way. This of <aside> as a sidebar to your main content.

<aside>元素用于不属于其出现的文本流的内容,但仍以某种方式相关。 <aside>作为主要内容的侧边栏。

Before HTML5, our menus were created with <ul>’s and <li>’s. Now, together with these, we can separate our menu items with a <nav>, for navigation between your pages. You can have any number of <nav> elements on a page, for example, its common to have global navigation across the top (in the <header>) and local navigation in a sidebar (in an <aside> element).

在HTML5之前,我们的菜单是使用<ul><li>的。 现在,连同这些,我们可以使用<nav>分隔菜单项,以在页面之间进行导航。 您可以在页面上具有任意数量的<nav>元素,例如,在顶部进行全局导航(在<header> )和在侧栏中进行局部导航(在<aside>元素中)是常见的做法。

If there is a <header> there must be a <footer>. A <footer> is generally found at the bottom of a document, a section, or an article. Just like the <header> the content is generally metainformation, such as author details, legal information, and/or links to related information. It is also valid to include <section> elements within a footer.

如果有一个<header>则必须有一个<footer> 。 通常在文档,节或文章的底部找到<footer> 。 就像<header> ,内容通常是元信息,例如作者详细信息,法律信息和/或相关信息的链接。 在页脚中包含<section>元素也是有效的。

©Company A

<small> (<small>)

The <small> element often appears within a <footer> or <aside> element which would usually contain copyright information or legal disclaimers, and other such fine print. However, this is not intended to make the text smaller. It is just describing its content, not prescribing presentation.

<small>元素通常出现在<footer><aside>元素内,该元素通常包含版权信息或法律免责声明以及其他此类精美文字。 但是,这并不是要使文本变小。 它仅是描述其内容,而不是规定演示。

©Company A Date

<time> (<time>)

The <time> element allows an unambiguous ISO 8601 date to be attached to a human-readable version of that date.

<time>元素允许将明确的ISO 8601日期附加到该日期的人类可读版本。

Why bother with <time>? While humans can read time that can disambiguate through context in the normal way, the computers can read the ISO 8601 date and see the date, time, and the time zone.

为什么要困扰<time> ? 虽然人类可以正常方式读取可以通过上下文进行歧义的时间,但计算机可以读取ISO 8601日期并查看日期,时间和时区。

<figure><figcaption> (<figure> and <figcaption>)

<figure> is for wrapping your image content around it, and <figcaption> is to caption your image.

<figure>用于将图像内容包装在其周围,而<figcaption>用于为图像添加标题。

Shadow of Mordor
Cover art for Middle-earth: Shadow of Mordor

详细了解新HTML5元素: (Learn more about the new HTML5 elements:)

  • provides simple and clear descriptions of many of the news elements and how/where they should be used.

    提供了许多新闻元素及其使用方式/位置的简单明了描述。

  • also provides a great reference for all HTML elements and goes deeper into each.

    还为所有HTML元素提供了很好的参考,并且对每个元素都有更深入的介绍。

翻译自:

html5语义化标记元素

转载地址:http://emrwd.baihongyu.com/

你可能感兴趣的文章
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_3_练习_使用递归计算阶乘...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_4_字节输出流写入数据到文件...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_5_综合案例_文件搜索...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_6_字节输出流写多个字节的方法...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_03 过滤器_2_FileNameFilter过滤器的使用和Lambda表达式...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_8_字节输入流_InputStream类&FileInputStream...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_3_字节输出流_OutputStream类&FileOutputStream...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_10_字节输入流一次读取一个字节的原理...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_5_文件存储的原理和记事本打开文本显示原理...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_12_练习_文件复制...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_7_字节输出流的续写和换行...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_1_字符输入流_Reader类&FileRead...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_9_字节输入流读取字节数据...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_3_字符输出流_Writer类&FileWriter类...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_11_字节输入流一次读取多个字节...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_5_flush方法和close方法的区别...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_13_使用字节流读取中文的问题...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_7_字符输出流的续写和换行...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_2_字符输入流读取字符数据...
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_9_JDK7和JDK9流中异常的处理...
查看>>