# html tips
前言
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScript.
# The loading=lazy attribute
Performance tip. You can use the
loading=lazyattribute to defer the loading of the image until the user scrolls to them.
<img src="image.jpg" loading="lazy" alt="Alternative Text" />
# Email, call, and SMS links
<a href="mailto:{email}?subject={subject}&body={content}">
Send us an email
</a>
<a href="tel:{phone}">
Call us
</a>
<a href="sms:{phone}?body={content}">
Send us a message
</a>
2
3
4
5
6
7
8
9
10
11
# Ordered lists start attribute
Use the
startattribute to change the starting point for your ordered lists.
<ol start="66">
<li>html</li>
<li>useful</li>
<li>tips</li>
</ol>
2
3
4
5
# The meter element
You can use the
<meter>element to display quantities. No JavaScript/CSS needed.
<label for="value1">Low</label>
<meter
id="value1"
min="0"
max="100"
low="30"
high="75"
optimum="80"
value="25"
></meter>
2
3
4
5
6
7
8
9
10
# HTML Native Search
div class="wrapper">
<h1>
Native HTML Search
</h1>
<input list="items">
<datalist id="items">
<option value="Marko Denic">
<option value="FreeCodeCamp">
<option value="FreeCodeTools">
<option value="Web Development">
<option value="Web Developer">
</datalist>
</div>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Fieldset Element
You can use the
<fieldset>element to group several controls as well as labels (<label>) within a web form.
<form>
<fieldset>
<legend>Choose your favorite language</legend>
<input type="radio" id="javascript" name="language" />
<label for="javascript">JavaScript</label><br />
<input type="radio" id="python" name="language" />
<label for="python">Python</label><br />
<input type="radio" id="java" name="language" />
<label for="java">Java</label>
</fieldset>
</form>
2
3
4
5
6
7
8
9
10
11
12
13
14
# Window.opener
Pages opened with
target="_blank"allow the new page to access the original’s window.opener. This can have security and performance implications. Includerel="noopener"orrel="noreferrer"to prevent this.
<a href="https://markodenic.com/" target="_blank" rel="noopener">
Marko's website
</a>
2
3
# Base Element
If you want to open all links in the document in a new tab, you can use
<base>element:
<head>
<base target="_blank" />
</head>
<!-- This link will open in a new tab. -->
<div class="wrapper">
This link will be opened in a new tab:
<a href="https://freecodetools.org/">
Free Code Tools
</a>
<p>
Read more: <br /><a
href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base"
>
MDN Documentation
</a>
</p>
</div>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Favicon cache busting
To refresh your website’s favicon you can force browsers to download a new version by adding
?v=2to the filename.This is especially helpful in production to make sure the users get the new version.
<link rel="icon" href="/favicon.ico?v=2" />
# The spellcheck attribute
Use the
spellcheckattribute to define whether the element may be checked for spelling errors.
<label for="input1">spellcheck="true"</label>
<input type="text" id="input1" spellcheck="true" />
<label for="input2">spellcheck="false"</label>
<input type="text" id="input2" spellcheck="false" />
2
3
4
5
# Native HTML sliders
You can use
<input type="range">to create sliders.
<label for="volume">Volume: </label>
<input type="range" id="volume" name="volume" min="0" max="20" />
<label for="result">Your choice: </label>
<input type="number" id="result" name="result" />
2
3
4
5
# HTML Accordion
You can use the
detailselement to create a native HTML accordion.
<div class="wrapper">
<details>
<summary>
Click me to see more details
</summary>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ut eum
perferendis eius. Adipisci velit et similique earum quas illo odio rerum
optio, quis, expedita assumenda enim dicta aliquam porro maxime minima sed
a ullam, aspernatur corporis.
</p>
</details>
</div>
2
3
4
5
6
7
8
9
10
11
12
13
14
# mark tag
You can use the
<mark>tag to highlight text.
# download attribute
You can use the
downloadattribute in your links to download the file instead of navigating to it.
<a href="path/to/file" download>
Download
</a>
2
3
# Performance tip
Use the
.webpimage format to make images smaller and boost the performance of your website.
<picture>
<!-- load .webp image if supported -->
<source srcset="logo.webp" type="image/webp" />
<!--
Fallback if `.webp` images or <picture> tag
not supported by the browser.
-->
<img src="logo.png" alt="logo" />
</picture>
2
3
4
5
6
7
8
9
10
# Video thumbnail
Use the
posterattribute to specify an image to be shown while the video is downloading, or until the user hits the play button.
<video poster="path/to/image"></video>
# input type="search"
Use the
type="search"for your search inputs and you get the “clear” button for free.