Html tips to make your code semantic

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. Google will give you a lot of articles on semantics. Why do we need semantic tags? I think that each tag in Html has its own specific meaning, and semantics means that we use appropriate tags in appropriate places to make it easier for humans and machines (machines can be understood as browsers and search engines) to understand at a glance. If my explanation is not clear enough, please Google it.

How to use the right label at the right place?

This is a simple logic of understanding. For example, h1~h6 tags are used for titles; ul is used for unordered lists; ol is used for lists with content; dl is used to define lists; em and strong tags are used for emphasis... To put it bluntly, each English interpretation of an Html tag determines its semantics (later in this article, I will put a comparison table of English interpretations of commonly used Html tags for reference).

What is clear to both humans and machines?

The best way to check if an Html page is semantic is to remove the CSS link from the page to see if the page structure is in order and if the page is still readable. Why can we say that? Everyone knows that browsers have default styles (it is recommended to use Chrome's Web Developer Tools for Chrome).   plugin, or Firefox Web Developer   plugin), for example, h1~h6, there will be default styles of bold/decreasing font size, top and bottom margins, ul, ol, dl have default bullets, strong has a bold style by default... Therefore, for the same page, a semantically well-formed Html can still perform well even if the page CSS is removed.

Another point is that good semantic coding is more friendly to search engines. Search spiders don't recognize your CSS, but they can recognize Html tags.

Here is a simple example:

未語義化語義化后的效果差異

Copy code
The code is as follows:

<!--Unsemanticized-->
<div id="header">
<div class="h1">Mr.Think's Blog</div>
<div class="h2">Focus on Web front-end technology, love Php, and advocate a simple life.</div>
</div>
<!--After semanticization-->
<div id="header">
<h1>Mr.Think's Blog</h1>
<h2>Focus on Web front-end technology, love Php, and advocate a simple life.</h2>
</div>

Through the simple examples above and the effect diagram without any CSS definition, you should understand the difference between the two. If you are learning Html5, its header, footer, sidebar, article and other elements are all newly added semantic tags.
Semanticization of Html coding is a step towards high-quality front-end development. It means better compliance with Web standards, and it also allows your page to remain in order after removing the style. For more detailed introduction to semanticization, you can Google it yourself or read Adam's "The Way to Practice Web Front-end Development"   Chapter 3.
Appendix: Chinese-English comparison table of tag semantics

Tag Name English spelling Chinese Translation
a anchor anchor
abbr abbreviation Abbreviations
acronym acronym Abbreviations
address address address
b bold Bold
big big Get bigger
blockquote block quotation Block referenced by
br break Line Break
caption caption title
center center Center
dd definition description Definition Description
del delete delete
div division Separation
dl definition list Definition List
dt definition term Defining Terms
em emphasized Aggravation
fieldset fieldset Domain Set
font font Font
h1~h6 header1~header6 Title 1~Title 6
hr horizontal rule Level
i italic Italic
ins inserted insert
legend legend icon
li list item List Items
ol ordered list Sorting List
p paragraph paragraph
pre preformatted Predefined formats
s strikethrough Strikethrough
small small Smaller
span span scope
strong strong Aggravation
sub subscripted Table below
sup superscripted Superscript
u underlined Underline
ul unordered list Unsorted list
var variable variable

Let me add the following

1. What is HTML semantics?

Choosing appropriate HTML tags makes it easier for developers to read and write more elegant code while allowing browser crawlers and machines to parse it well.

2. Why semanticization?

In order to make the page look good when it is naked, even without CSS, the page can also present a good content structure and code structure;
User experience: for example, title and alt are used to explain nouns or image information, and label tags are used flexibly;
Good for SEO: Establishing good communication with search engines helps crawlers to capture more valid information: crawlers rely on tags to determine the context and weight of each keyword;
Facilitates parsing by other devices (such as screen readers, blind readers, and mobile devices) to render web pages in a meaningful way;
Facilitating team development and maintenance, and making semantics more readable are important trends in the next step for web pages. Teams that follow W3C standards all follow this standard to reduce differentiation.

3. What should you pay attention to when writing HTML code?

1. Use as few semantically meaningless tags as possible, such as div and span;
2. When the semantics are not obvious, if you can use either div or p, try to use p, because p has upper and lower spacing by default, which is beneficial for compatibility with special terminals;
3. Do not use pure style tags and tags not supported by the specification, such as: b, font, u, center, strike, menu, etc. Use CSS settings instead.
4. Text that needs to be emphasized can be included in strong or em tags (browser default styles, don't use them if you can specify them with CSS). The default style of strong is bold (don't use b), and em is italic (don't use i);
5. When using a table, use caption for the title, thead for the header, tbody for the main body, and tfoot for the footer. The table header and general cells should be distinguished, the table header uses th and the cells use td;
6. The form fields should be wrapped with fieldset tags, and the purpose of the form should be explained with legend tags;
7. The description text corresponding to each input tag needs to use the label tag, and by setting the id attribute for the input and setting for=someld in the label tag, the description text can be associated with the corresponding input.
8. Do not omit the attributes of certain tags. The alt attribute of the <img> tag is used as replacement text when the image cannot be displayed normally. The title attribute of the <a> tag can be used as explanatory information and is displayed as a prompt message when the mouse hovers over it.

An interview question from Yahoo:

<P> What I write is not HTML, but loneliness. <br><br> I said:<br>Don't be obsessed with me, I'm just a legend

The problems include:

1. The difference between html and xhtml standards is that xhtml has a strict structure, tags must be closed, single tags need to be closed by adding / at the end, and tags must be lowercase;
2. Separation of structure and style. Do not use <br/> to control the style. Design the structure reasonably. Add p to each paragraph for line breaks. The style needs to be implemented using CSS.
3. Use tags reasonably and pay attention to the semantics of tags. Abbreviations can be marked with <abbr>, "I" can be marked with <cite>, and quoted speech content can be marked with <q>;

//Code reference is as follows
<p>What I write is not <abbr title="Hypertext Markup Language">HTML</abbr>, but loneliness. </p>
<p><cite>I</cite> said: <q>Don't be obsessed with me, I'm just a legend</q></p>

The above is the detailed content of Html techniques to semanticize your code. For more information about Html semanticization, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Node quickly builds the backend implementation steps

>>:  MySQL statement summary

Recommend

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

Vue implements pull-down to load more

Developers familiar with Element-UI may have had ...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

Vue implements an example of pulling down and scrolling to load data

Table of contents Step 1: Installation Step 2: Ci...

Ubuntu installation cuda10.1 driver implementation steps

1. Download cuda10.1: NVIDIA official website lin...

Getting Started Tutorial for Beginners⑧: Easily Create an Article Site

In my last post I talked about how to make a web p...

Detailed explanation of JS homology strategy and CSRF

Table of contents Overview Same Origin Policy (SO...

Tutorial on building nextcloud personal network disk with Docker

Table of contents 1. Introduction 2. Deployment E...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

A set of code based on Vue-cli supports multiple projects

Table of contents Application Scenario Ideas Proj...

Detailed installation and configuration tutorial of mysql5.7 on CentOS

Install Make sure your user has permission to ins...

MySQL table name case selection

Table of contents 1. Parameters that determine ca...

IE8 Beta 1 has two areas that require your attention

<br />Related articles: Web skills: Multiple...

Detailed explanation of MySQL 30 military rules

1. Basic Specifications (1) InnoDB storage engine...