Fe-interview: [html] 第11天 你对标签语义化的理解是什么?

Created on 26 Apr 2019  ·  16Comments  ·  Source: haizlin/fe-interview

第11天 你对标签语义化的理解是什么?

html

Most helpful comment

见名知意,方便多人认识,且命名统一,简洁,易于重构代码

All 16 comments

见名知意,方便多人认识,且命名统一,简洁,易于重构代码

也方便SEO

更方便后期维护修改吧,并且也能让代码意图更明了

正确的标签做正确的事,有助于SEO,也方便读代码,了解代码的意图

也方便SEO
对SEO的影响大吗

标签语义化的初衷是让正确的标签做正确的事情,但对于人来说,标签的语义除了在 tag 上体现,还可以从 id, class 上体现出来。

而每个人对于标签的理解都会存在不同,因此强制规定哪个标签怎么用就不是很好。我的理解标签语义化更多的是为 SEO 进行服务。让爬虫更方便地找到想要的信息。

有利于SEO
方便维护
有利于浏览器识别

简明的表示页面结构,便于开发人员及浏览器识别。

一、什么是HTML语义化标签

语义化的标签,旨在让标签有自己的含义。

\

一行文字\


\一行文字\
1
2
如上代码,p 标签与 span 标签都区别之一就是,p 标签的含义是:段落。而 span 标签责没有独特的含义。

二、语义化标签的优势

代码结构清晰,方便阅读,有利于团队合作开发。
方便其他设备解析(如屏幕阅读器、盲人阅读器、移动设备)以语义的方式来渲染网页。
有利于搜索引擎优化(SEO)。

三、常见的语义化标签

因此我们在写页面结构时,应尽量使用有 语义的HTML 标签
  • \:页面主体内容。</li> <li>\<hn>:h1\~h6,分级标题,\<h1> 与 \<title> 协调有利于搜索引擎优化。</li> <li>\<ul>:无序列表。</li> <li>\<li>:有序列表。</li> <li>\<header>:页眉通常包括网站标志、主导航、全站链接以及搜索框。</li> <li>\<nav>:标记导航,仅对文档中重要的链接群使用。</li> <li>\<main>:页面主要内容,一个页面只能使用一次。如果是web应用,则包围其主要功能。</li> <li>\<article>:定义外部的内容,其中的内容独立于文档的其余部分。</li> <li>\<section>:定义文档中的节(section、区段)。比如章节、页眉、页脚或文档中的其他部分。</li> <li>\<aside>:定义其所处内容之外的内容。如侧栏、文章的一组链接、广告、友情链接、相关产品列表等。</li> <li>\<footer>:页脚,只有当父级是body时,才是整个页面的页脚。</li> <li>\<small>:呈现小号字体效果,指定细则,输入免责声明、注解、署名、版权。</li> <li>\<strong>:和 em 标签一样,用于强调文本,但它强调的程度更强一些。</li> <li>\<em>:将其中的文本表示为强调的内容,表现为斜体。</li> <li>\<mark>:使用黄色突出显示部分文本。</li> <li>\<figure>:规定独立的流内容(图像、图表、照片、代码等等)(默认有40px左右margin)。</li> <li>\<figcaption>:定义 figure 元素的标题,应该被置于 figure 元素的第一个或最后一个子元素的位置。</li> <li>\<cite>:表示所包含的文本对某个参考文献的引用,比如书籍或者杂志的标题。</li> <li>\<blockquoto>:定义块引用,块引用拥有它们自己的空间。</li> <li>\<q>:短的引述(跨浏览器问题,尽量避免使用)。</li> <li>\<time>:datetime属性遵循特定格式,如果忽略此属性,文本内容必须是合法的日期或者时间格式。</li> <li>\<abbr>:简称或缩写。</li> <li>\<dfn>:定义术语元素,与定义必须紧挨着,可以在描述列表dl元素中使用。</li> <li>\<address>:作者、相关人士或组织的联系信息(电子邮件地址、指向联系信息页的链接)。</li> <li>\<del>:移除的内容。</li> <li>\<ins>:添加的内容。</li> <li>\<code>:标记代码。</li> <li>\<meter>:定义已知范围或分数值内的标量测量。(Internet Explorer 不支持 meter 标签)</li> <li>\<progress>:定义运行中的进度(进程)。</li> </ul> <p>原文链接:<a rel="nofollow noopener" target="_blank" href="https://blog.csdn.net/eeeecw/article/details/80591511">https://blog.csdn.net/eeeecw/article/details/80591511</a></p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars3.githubusercontent.com/u/29722665?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="diandianzd picture"> <strong>diandianzd</strong> <span class="text-muted ml-1">on 16 Oct 2019</span> </div> <div class="col text-right"> 👍<span class="ml-2 mr-3">3</span> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p><strong>什么是语义化标签?</strong><br /> 语义是我们说话表达的意思,多数的语义实际上都是由文字来承载的。语义类标签则是纯文字的补充,比如标题、自然段、章节、列表,这些内容都是纯文字无法表达的,我们需要依靠语义标签代为表达。<br /> <strong>语义化标签的好处?</strong><br /> 代码结构清晰,方便阅读,有利于团队合作开发。<br /> 方便其他设备解析(如屏幕阅读器、盲人阅读器、移动设备)以语义的方式来渲染网页,如根据文章可以自动生成目录等等。<br /> 有利于搜索引擎优化(SEO),可以让搜索引擎爬虫更好地获取到更多有效信息,有效提升网页的搜索量<br /> <strong>语义化标签的应用场景?</strong><br /> 1 作为自然语言的延伸(补充说明、消除歧义等)<br /> ruby现象(使用ruby、rt、rp标签)<br /> em标签表示句子中的重音(强调)<br /> 2作为标题摘要<br /> 在 hgroup 中的 h1-h6 被视为同一标题的不同组成部分,形成副标题。<br /> section 的嵌套会使得其中的 h1-h6 下降一级,因此,在 HTML5 以后, section 和 h1 就足以形成文档的树形结构。<br /> 3作为整体结构<br /> 让浏览器很好地支持“阅读视图功能”,一个典型的body结构:</p> <pre><code class="prettyprint"><body> <header>……</header> <article> <header>……</header> <section>……</section> <section>……</section> <section>……</section> <footer>……</footer> </article> <article> …… </article> <article> …… </article> <footer> <address></address> </footer> </body> </code></pre> <p>类似报纸的多文章结构适合用 article 来组织:</p> <pre><code class="prettyprint"><body> <header>……</header> <article> <header>……</header> <section>……</section> <section>……</section> <section>……</section> <footer>……</footer> </article> <article> …… </article> <article> …… </article> <footer> <address></address> </footer> </body> </code></pre> <p><a rel="nofollow noopener" target="_blank" href="https://time.geekbang.org/column/article/78158">https://time.geekbang.org/column/article/78158</a></p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/40456021?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="abueavan picture"> <strong>abueavan</strong> <span class="text-muted ml-1">on 2 Nov 2019</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="mb-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3731713875" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <ol> <li>由于语义化更具有可读性,便于团队开发维护</li> <li>在没有css的情况下,页面也可以呈现出也很好的内容结构和代码结构</li> <li>关于SEO,搜索引擎能更好的理解页面中各部分的关系,可更快更准确的搜索到信息</li> </ol> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars3.githubusercontent.com/u/55520781?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="luckyMuChen picture"> <strong>luckyMuChen</strong> <span class="text-muted ml-1">on 18 Feb 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <ol> <li>开发人员看的舒服理解的容易,维护方便</li> <li>搜索引擎更容易检索</li> <li>对阅读障碍人员更友好,(特定的阅读器+语义化)</li> </ol> <p>我都是以某一个人、物为主体这样去记忆,好像也还行,大概还是能记住</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars3.githubusercontent.com/u/22734489?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="larry0442 picture"> <strong>larry0442</strong> <span class="text-muted ml-1">on 7 Apr 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>Semantic HTML elements 是作为代码编写人员与机器解读的中介</p> <p>对于人来说,语义标签(Semantic HTML elements)常与内容有关<br /> <article><br /> <header><br /> <table></p> <p>非语义标签(non Semantic HTML elements)跟内容无关<br /> <div><br /> <id><br /> <class></p> <p>对机器来说,标签是给机器内核语法解析成语法树(Syntax tree, AST)</p> <p>dom tree + cssom tree= render tree </p> <p><img loading="lazy" src="https://user-images.githubusercontent.com/49817672/80424511-785e0400-8914-11ea-9d5a-90cd6e9d7a72.png" alt="render-tree-construction" /></p> <p><img loading="lazy" src="https://user-images.githubusercontent.com/49817672/80425109-98da8e00-8915-11ea-8132-9d4b4a57139b.png" alt="image" /></p> <p>reference:<br /> <a rel="nofollow noopener" target="_blank" href="https://www.cnblogs.com/RachelChen/p/5456182.html">https://www.cnblogs.com/RachelChen/p/5456182.html</a><br /> <a rel="nofollow noopener" target="_blank" href="https://www.html5rocks.com/zh/tutorials/internals/howbrowserswork/#Parsing_general">https://www.html5rocks.com/zh/tutorials/internals/howbrowserswork/#Parsing_general</a></p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/49817672?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="gousern picture"> <strong>gousern</strong> <span class="text-muted ml-1">on 28 Apr 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <ul> <li>使代码更容易维护<br /> 使每一个标签一眼就能看出大概的意图,作用</li> <li>更利于SEO</li> <li>方便其他设备解析</li> </ul> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars0.githubusercontent.com/u/22475160?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="blueRoach picture"> <strong>blueRoach</strong> <span class="text-muted ml-1">on 27 May 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>让页面的内容结构化,结构更清晰,便于对浏览器、搜索引擎解析;<br /> 即使在没有样式CSS情况下也以一种文档格式显示,并且是容易阅读的;<br /> 利于SEO,便于阅读维护理解</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/55782090?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="giggleCYT picture"> <strong>giggleCYT</strong> <span class="text-muted ml-1">on 3 Jun 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>有利于SEO<br /> 方便维护<br /> 有利于浏览器识别</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars0.githubusercontent.com/u/3272892?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="MrZ2019 picture"> <strong>MrZ2019</strong> <span class="text-muted ml-1">on 7 Sep 2020</span> </div> <div class="col text-right"> </div> </div> </div> </div> </div> <div class="col-12"> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown text-center helpful"> <div class="title">Was this page helpful?</div> <div class="mt-1" onMouseLeave="rating(437842491, 0);"> <i class="fas fa-star inactive" id="star-1" onMouseOver="rating(437842491, 1);" onclick="rate(437842491, 1);"></i> <i class="fas fa-star inactive" id="star-2" onMouseOver="rating(437842491, 2);" onclick="rate(437842491, 2);"></i> <i class="fas fa-star inactive" id="star-3" onMouseOver="rating(437842491, 3);" onclick="rate(437842491, 3);"></i> <i class="fas fa-star inactive" id="star-4" onMouseOver="rating(437842491, 4);" onclick="rate(437842491, 4);"></i> <i class="fas fa-star inactive" id="star-5" onMouseOver="rating(437842491, 5);" onclick="rate(437842491, 5);"></i> </div> <div class="description text-small"><span id="rating-val">0</span> / 5 - <span id="rating-count">0</span> ratings</div> </div> </div> <div class="mb-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3452512275" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> </div> <div class="col-12 col-lg-4"> <div id="ph-above-related"></div> <div class="card card-custom issue-box"> <div class="card-body pt-3 pb-5"> <h2 class="mb-4">Related issues</h2> <div> <strong> <a href="/fe-interview/456644990/vue-watchzen-yao-shen-du-jian-ting-dui-xiang-bian-hua">[vue] watch怎么深度监听对象变化</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/5070345?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="haizhilin2013 picture"> <strong class="pr-1" dir="ltr">haizhilin2013</strong>  ·  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/fe-interview/458872437/vue-vueyao-zuo-quan-xian-guan-li-gai-zen-yao-zuo-ru-guo-kong">[vue] vue要做权限管理该怎么做?如果控制到按钮级别的权限怎么做?</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/5070345?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="haizhilin2013 picture"> <strong class="pr-1" dir="ltr">haizhilin2013</strong>  ·  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/fe-interview/459510232/vue-propyan-zheng-de-typelei-xing-you-na-ji-chong">[vue] prop验证的type类型有哪几种?</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/5070345?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="haizhilin2013 picture"> <strong class="pr-1" dir="ltr">haizhilin2013</strong>  ·  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/fe-interview/458698515/vue-zai-vuexiang-mu-zhong-ru-he-yin-ru-di-san-fang-ku-bi-ru">[vue] 在vue项目中如何引入第三方库(比如jQuery)?有哪些方法可以做到?</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/5070345?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="haizhilin2013 picture"> <strong class="pr-1" dir="ltr">haizhilin2013</strong>  ·  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/fe-interview/458692394/vue-ni-you-feng-zhuang-guo-axiosma-zhu-yao-shi-feng-zhuang">[vue] 你有封装过axios吗?主要是封装哪方面的?</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/5070345?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="haizhilin2013 picture"> <strong class="pr-1" dir="ltr">haizhilin2013</strong>  ·  <span class="px-1" dir="ltr">3</span><span>Comments</span> </div> </div> </div> <div class="sticky-top pt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div id="ph-below-related-2" class="mt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <div class="col-12 col-lg-4"> </div> </div> <div class="skyscraper-container"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="7879185320" data-ad-format="vertical" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="mt-5 spacer"></div> <footer class="mt-5 pb-2 py-4 text-center mt-auto"> <div class="container"> <a class="navbar-brand logo mr-5" href="/"> <img src="/assets/img/logo.svg" width="40" height="40" alt="bleepingcoder logo"> bleeping<strong>coder</strong> </a> <div class="mt-4"> bleepingcoder.com uses publicly licensed GitHub information to provide developers around the world with solutions to their problems. We are not affiliated with GitHub, Inc. or with any developers who use GitHub for their projects. We do not host any of the videos or images on our servers. All rights belong to their respective owners. </div> <div> Source for this page: <a href="https://www.github.com/haizlin/fe-interview/issues/31" rel="nofollow noreferrer" target="_blank">Source</a> </div> </div> <hr class="mb-5 mt-5"> <div class="container"> <div class="row"> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular programming languages</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/python" dir="ltr">Python</a> </li> <li class="mb-2"> <a href="/javascript" dir="ltr">JavaScript</a> </li> <li class="mb-2"> <a href="/typescript" dir="ltr">TypeScript</a> </li> <li class="mb-2"> <a href="/cpp" dir="ltr">C++</a> </li> <li class="mb-2"> <a href="/csharp" dir="ltr">C#</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/microsoft/vscode" dir="ltr">vscode</a> </li> <li class="mb-2"> <a href="/numpy/numpy" dir="ltr">numpy</a> </li> <li class="mb-2"> <a href="/ant-design/ant-design" dir="ltr">ant-design</a> </li> <li class="mb-2"> <a href="/mui-org/material-ui" dir="ltr">material-ui</a> </li> <li class="mb-2"> <a href="/vercel/next-js" dir="ltr">next.js</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-0"> <strong>More GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/rust-lang/rust" dir="ltr">rust</a> </li> <li class="mb-2"> <a href="/moment/moment" dir="ltr">moment</a> </li> <li class="mb-2"> <a href="/yarnpkg/yarn" dir="ltr">yarn</a> </li> <li class="mb-2"> <a href="/mozilla/pdf-js" dir="ltr">pdf.js</a> </li> <li class="mb-2"> <a href="/JuliaLang/julia" dir="ltr">julia</a> </li> </ul> </div> </div> </div> <hr class="mb-5 mt-5"> <div class="container text-muted"> © 2025 bleepingcoder.com - <a href="/bleeps" rel="nofollow">Contact</a><br /> By using our site, you acknowledge that you have read and understand our <a href="/cookies" rel="nofollow">Cookie Policy</a> and <a href="/privacy" rel="nofollow">Privacy Policy</a>. </div> </footer> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script> <script async src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <!--<script defer type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5fb2db66acbd74b2"></script>--> <script type="text/javascript" src="/assets/js/main.js"></script> <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script></body> </html>