HTML Tips and Tricks: 20 You Won’t See in Most Tutorials
Contents
- But first, what is HTML?
- Let’s start!
- 1. Lazy load images with loading=lazy
- 2. mailto, tel and sms links
- 3. Start an ordered list at any number
- 4. The `meter` element
- 5. HTML Native Search
- Native HTML Search
- 6. fieldset and legend: group form fields
- 7. window.opener and target=_blank
- 8. Open all links in a new tab with the base element
- 9. Favicon cache busting
- 10. Turn spell checking on or off
- 11. HTML range slider (input type=range)
- 12. HTML accordion with details and summary
- 13. Highlight text with the mark tag
- 14. Download links with the download attribute
- 15. WebP images for faster pages
- 16. Video thumbnail with the poster attribute
- 17. input type=search
- 18. pre tag: show preformatted text
- 19. Open a link in a new tab
- 20. Make an image clickable
- FAQ
- More HTML
In this article, I will share with you some very useful HTML tips. Enjoy!
* I recently published a 50-page eBook with lots of HTML tips, examples, and illustrations. It’s available here: HTML eBook.
But first, what is HTML?
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.
Let’s start!
1. Lazy load images with loading=lazy
Performance tip. You can use the loading=lazy attribute to defer the loading of the image until the user scrolls to them.
<img src='image.jpg' loading='lazy' alt='Alternative Text'>
2. mailto, tel and sms links
Link to an email address with mailto:, start a phone call with tel: and open a text message with sms:. On phones they open the right app directly. A mailto: link can also pre-fill the subject and body, like mailto:hello@example.com?subject=Hi&body=Hello.
<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>
3. Start an ordered list at any number
Use the start attribute to change the starting point for your ordered lists.
4. The `meter` element
You can use the <meter> element to display quantities. No JavaScript/CSS is needed.
5. HTML Native Search
Demo:
Native HTML Search
6. fieldset and legend: group form fields
You can use the <fieldset> element to group several controls as well as labels (<label>) within a web form.
Screen readers announce the legend with every field inside, which makes groups of radio buttons and checkboxes much clearer. More form tricks in HTML input types.
Demo:
7. window.opener and target=_blank
Pages opened with target="_blank" allow the new page to access the original’s window.opener. This can have security and performance implications. Include rel="noopener" or rel="noreferrer" to prevent this.
Modern browsers now treat target="_blank" as rel="noopener" by default, but adding rel="noopener noreferrer" yourself is still good practice for older browsers.
<a href="https://markodenic.com/" target="_blank" rel="noopener">
Marko's website
</a>
8. Open all links in a new tab with the base element
- If you want to open all links in the document in a new tab, you can use
<base>element:
To open just one link in a new tab, add target="_blank" to that link instead (see tip 19 below).
<head>
<!-- Every link in the document now opens in a new tab -->
<base target="_blank">
</head>
9. Favicon cache busting
To refresh your website’s favicon you can force browsers to download a new version by adding ?v=2 to 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" />
10. Turn spell checking on or off
Use the spellcheck attribute to define whether the element may be checked for spelling errors.
Demo:
11. HTML range slider (input type=range)
You can use <input type="range"> to create sliders.
Add min, max and step to control the range, and recolor it with the CSS accent-color property. See all the other HTML input types.
Demo:
12. HTML accordion with details and summary
You can use the details element to create a native HTML accordion.
Demo:
What is the details element?
A native disclosure widget: click the summary to show or hide the content.
Does it need JavaScript?
No. It works with HTML alone and is keyboard accessible by default.
Can only one stay open?
Yes. Give the details elements the same name attribute.
13. Highlight text with the mark tag
You can use the <mark> tag to highlight text.
14. Download links with the download attribute
You can use the download attribute in your links to download the file instead of navigating to it.
<a href='path/to/file' download>
Download
</a>
15. WebP images for faster pages
Use the .webp image 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>
16. Video thumbnail with the poster attribute
Use the poster attribute 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">
17. input type=search
Use the type="search" for your search inputs and you get the “clear” button for free.
Mobile keyboards also show a search key. More in HTML input types.
18. pre tag: show preformatted text
Use the <pre> tag to present preformatted text exactly as written in the HTML file:
Demo:
Spaces and
line breaks
stay exactly
as written.
19. Open a link in a new tab
Add target="_blank" to the link. Include rel="noopener noreferrer" so the new page can’t access yours through window.opener (tip 7). Use it sparingly: opening new tabs unexpectedly can annoy readers.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Opens in a new tab</a>
20. Make an image clickable
Wrap the image in a link. The image’s alt text becomes the link text for screen readers and search engines, so describe where the link goes.
<a href="/pricing/">n <img src="plans.png" alt="See pricing plans">n</a>
To be continued…
If you liked this article, you might light this one as well: HTML input types.
FAQ
Do I need JavaScript for these HTML tricks?
No. Lazy loading, the accordion, range sliders, spell checking, download links and the rest all work with plain HTML.
How do I open a link in a new tab in HTML?
Add target="_blank" and rel="noopener noreferrer" to the link, as in tip 19. To open every link on the page in a new tab, use the base element (tip 8).
What is a mailto link?
A link with href="mailto:you@example.com" opens the visitor’s email app with your address filled in. Tip 2 shows how to pre-fill the subject and body too.
More HTML
Read HTML input types, web performance tips, the HTML snippets and the companion CSS tips and tricks. Want all of this as a PDF? Get the free Modern HTML & CSS ebook.