dev.rean.me
html

HTML Tips and Tricks with Examples

Best HTML5 tips and tricks with simple English explanations, bad code vs good code examples, semantic tags, forms, accessibility, and performance.

2026-08-28 ยท 6 min read

Share:

HTML Tips & Tricks โ€” Best Practice with Examples

Hello friend! Today we share best HTML5 tips and tricks. These tips help you write clean code, make website load faster, and improve Google SEO ranking!

If you want to learn more about HTML, check our step-by-step tutorials:

We explain in simple way with Bad Code โŒ vs Good Code โœ…. Easy to understand!


1. Use Semantic HTML (Not <div> Everywhere)

Do not use <div> for everything! HTML5 has semantic tags like <header>, <nav>, <main>, <article>, <section>, <footer>. It help search engine understand your website!

โŒ Bad (Div Soup):

<div class="header">
  <div class="nav">Nav link</div>
</div>
<div class="main">
  <div class="article">Content here</div>
</div>
<div class="footer">Copyright 2026</div>

โœ… Good (Semantic Tags):

<header>
  <nav>Nav link</nav>
</header>
<main>
  <article>Content here</article>
</main>
<footer>Copyright 2026</footer>

๐Ÿ’ก Tip: Semantic tags are super important for Google SEO ranking and screen readers!


2. Always Add alt Attribute to <img>

If image fail to load, alt text will show up. Also Google Image Search use alt text to understand image content!

โŒ Bad:

<img src="student-exam.jpg">

โœ… Good:

<img src="student-exam.jpg" alt="Student Exam Results Table in School">

๐Ÿ’ก Tip: If image is only for background design, use empty alt="".


3. Use <button> Instead of Clickable <div>

If user can click element to perform action, use <button> tag! Do not use <div onClick="...">. Keyboard user cannot press Enter or Space on a <div>!

โŒ Bad:

<div class="btn" onclick="submitForm()">Submit</div>

โœ… Good:

<button type="button" onclick="submitForm()">Submit</button>

๐Ÿ’ก Tip: <button> automatically supports keyboard tab focus, Space, and Enter key!


4. Always Pair <label> with <input>

Connect <label> to <input> using for attribute (or wrap input inside label). When user click label text, the input box automatically focuses!

โŒ Bad:

<span>Email:</span>
<input type="email" id="email">

โœ… Good:

<label for="user-email">Email:</label>
<input type="email" id="user-email">

5. Use Specific HTML5 Input Types

Do not use type="text" for everything! HTML5 has special input types like email, number, date, tel, url. Mobile phone will show correct soft keyboard automatically!

โŒ Bad:

<input type="text" placeholder="Enter age">
<input type="text" placeholder="Enter email">

โœ… Good:

<input type="number" placeholder="Enter age" min="1" max="100">
<input type="email" placeholder="Enter email">
<input type="date">

6. Use Native Autocomplete Datalist

You can create search dropdown autocomplete in native HTML without writing JavaScript or installing plugins!

โœ… Good Example:

<label for="country">Choose Country:</label>
<input list="countries" id="country" name="country">

<datalist id="countries">
  <option value="Cambodia">
  <option value="Japan">
  <option value="Thailand">
  <option value="USA">
</datalist>

๐Ÿ’ก Tip: Want step-by-step tutorial with video? Read: โ†’ Dropdown List with Auto Complete in HTML


7. Group Form Inputs with <fieldset> and <legend>

When you have radio buttons or checkboxes, group them with <fieldset> and <legend>. It makes form structure very clear!

โœ… Good Example:

<fieldset>
  <legend>Select Gender:</legend>
  <input type="radio" id="male" name="gender" value="male">
  <label for="male">Male</label>
  
  <input type="radio" id="female" name="gender" value="female">
  <label for="female">Female</label>
</fieldset>

๐Ÿ’ก Tip: Read our full lesson on fieldsets: โ†’ How to Use Fieldsets with Radios and Checkbox in HTML


8. Add loading="lazy" to Images

Lazy loading images helps webpage load super fast! Browser only downloads images when user scrolls down to them.

โŒ Bad (Download all images at once):

<img src="large-photo.jpg" alt="Photo">

โœ… Good (Lazy Load):

<img src="large-photo.jpg" alt="Photo" loading="lazy">

9. Security Rule for target="_blank"

When opening external links in a new tab, always add rel="noopener noreferrer". Otherwise, external page can hijack your site using JavaScript window.opener!

โŒ Bad (Security Risk):

<a href="https://example.com" target="_blank">Visit Site</a>

โœ… Good (Safe):

<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Site</a>

10. Native Accordion with <details> and <summary>

You can make collapsible accordion (Expand / Hide text) without writing any JavaScript!

โœ… Good Example:

<details>
  <summary>What is HTML?</summary>
  <p>HTML is HyperText Markup Language used to create webpage structure.</p>
</details>

๐Ÿ’ก Tip: User can click summary text to expand or collapse details. 100% native HTML!


If you want user to download PDF or file when clicking link instead of opening in browser tab, add download attribute!

โŒ Bad (Open in browser):

<a href="/files/book.pdf">Download Book</a>

โœ… Good (Force Download):

<a href="/files/book.pdf" download="HTML-Book.pdf">Download Book</a>

12. Always Set lang Attribute on <html> Tag

Tell browser and search engines what language your page uses. Very good for screen readers and automatic page translation!

โŒ Bad:

<html>

โœ… Good:

<html lang="en">

13. Responsive Meta Viewport Tag

Always include viewport meta tag inside <head>. Without this tag, mobile phones will render website zoomed out like desktop screen!

โœ… Good:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

14. Use Description List <dl> for Terms & Definitions

For dictionary, FAQ, key-value data, or metadata, use Description List <dl>, <dt> (term), and <dd> (definition)!

โœ… Good Example:

<dl>
  <dt>HTML</dt>
  <dd>HyperText Markup Language</dd>
  <dt>CSS</dt>
  <dd>Cascading Style Sheets</dd>
</dl>

๐Ÿ’ก Tip: Want to learn all list types (<ul>, <ol>, <dl>)? Read: โ†’ List in HTML โ€” ol, ul, dl Explained


Summary โ€” Quick HTML Checklist

TipTag / AttributeBenefit
1. Semantic Tags<header>, <main>, <footer>Better Google SEO
2. Image Altalt="description"Accessibility & SEO
3. Button Tag<button>Keyboard accessible
4. Label Connection<label for="id">Click text to focus input
5. Input Typestype="email|number|date"Mobile keyboard helper
6. Autocomplete<datalist>Native search dropdown
7. Form Grouping<fieldset> + <legend>Structured form inputs
8. Lazy Loadloading="lazy"Faster page load speed
9. Safe External Linkrel="noopener noreferrer"Security protection
10. Accordion<details> + <summary>Collapsible text without JS

๐Ÿ“š Read More HTML Tutorials

Check out our full collection of HTML tutorials:

Happy coding HTML! ๐ŸŽ‰

โ† Back to html
Share: