HTML Tips and Tricks: 20 You Won’t See in Most Tutorials

By Marko Denic
Contents
  1. But first, what is HTML?
  2. Let’s start!
  3. 1. Lazy load images with loading=lazy
  4. 2. mailto, tel and sms links
  5. 3. Start an ordered list at any number
  6. 4. The `meter` element
  7. 5. HTML Native Search
  8. Native HTML Search
  9. 6. fieldset and legend: group form fields
  10. 7. window.opener and target=_blank
  11. 8. Open all links in a new tab with the base element
  12. 9. Favicon cache busting
  13. 10. Turn spell checking on or off
  14. 11. HTML range slider (input type=range)
  15. 12. HTML accordion with details and summary
  16. 13. Highlight text with the mark tag
  17. 14. Download links with the download attribute
  18. 15. WebP images for faster pages
  19. 16. Video thumbnail with the poster attribute
  20. 17. input type=search
  21. 18. pre tag: show preformatted text
  22. 19. Open a link in a new tab
  23. 20. Make an image clickable
  24. FAQ
  25. 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.

html
<img src='image.jpg' loading='lazy' alt='Alternative Text'>

3. Start an ordered list at any number

Use the start attribute to change the starting point for your ordered lists.

Codepen Preview

4. The `meter` element

You can use the <meter> element to display quantities. No JavaScript/CSS is needed.

Demo:

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:

Choose a plan

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.

html
<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

  1. 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).

html
<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.

html
<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.

Mark Tag implementation

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.

html
<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.

html
<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.

html
<video poster="path/to/image">

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.

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.

html
<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.