DEV Community

Cover image for HTML Tags That'll Make Your Life Easier (No, Really)
Varshith V Hegde
Varshith V Hegde Subscriber

Posted on

HTML Tags That'll Make Your Life Easier (No, Really)

Look, I get it. HTML can feel boring sometimes. You throw in your divs and spans, maybe toss in a form here and there, and call it a day. But there are some genuinely cool HTML elements that most of us completely ignore, and honestly? They make things so much simpler.

Let me show you six HTML tags that actually solve real problems. No fancy jargon, just practical stuff you can start using today.

1. The <template> Element - Your HTML Blueprint

Okay, so you need to add stuff to your page dynamically. Maybe it's a new comment, a product card, whatever. You've probably been doing one of these:

The old way (don't do this):

// Using innerHTML - looks easy but it's dangerous
element.innerHTML = '<div class="card"><h3>' + userInput + '</h3></div>';
// If userInput has <script> tags? Yeah, you're in trouble.
Enter fullscreen mode Exit fullscreen mode

Or maybe you went the "safe but annoying" route:

// createElement - safe but tedious
const div = document.createElement('div');
div.className = 'card';
const h3 = document.createElement('h3');
h3.textContent = userInput;
div.appendChild(h3);
// This gets old real fast with complex HTML
Enter fullscreen mode Exit fullscreen mode

The better way with <template>:

<template id="card-template">
  <div class="card">
    <h3 class="card-title"></h3>
    <p class="card-description"></p>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode
// Clone it and use it
const template = document.getElementById('card-template');
const clone = template.content.cloneNode(true);

// Fill in your data
clone.querySelector('.card-title').textContent = userInput;
clone.querySelector('.card-description').textContent = description;

// Add it to the page
document.body.appendChild(clone);
Enter fullscreen mode Exit fullscreen mode

Why this rocks: The HTML stays in HTML where it belongs, it's safer than innerHTML, and it's faster because the browser only has to layout the new element instead of re-parsing everything.

2. Input Types You Didn't Know You Had

Stop reinventing the wheel with custom date pickers and validation libraries. The humble <input> tag is way more powerful than you think.

Email and phone validation built right in:

<input type="email" placeholder="your@email.com">
<input type="tel" placeholder="555-1234">
Enter fullscreen mode Exit fullscreen mode

On mobile, these automatically show the right keyboard. Plus, you get free validation styling:

input:invalid {
  border-color: red;
}
Enter fullscreen mode Exit fullscreen mode

Want pattern matching without JavaScript?

<input 
  type="text" 
  pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
  placeholder="123-456-7890"
>
Enter fullscreen mode Exit fullscreen mode

Date and time pickers that just work:

<input type="date">
<input type="datetime-local">
<input type="month">
<input type="week">
<input type="time">
Enter fullscreen mode Exit fullscreen mode

No library needed. They work across all modern browsers and look native on each platform.

Other handy types:

<input type="search">  <!-- Gets a little X to clear on some browsers -->
<input type="range" min="0" max="100">  <!-- Slider! -->
<input type="color">  <!-- Color picker! -->
Enter fullscreen mode Exit fullscreen mode

Autocomplete with <datalist>:

<input list="cities" placeholder="Choose a city">
<datalist id="cities">
  <option value="New York">
  <option value="Los Angeles">
  <option value="Chicago">
  <option value="Houston">
</datalist>
Enter fullscreen mode Exit fullscreen mode

Type a few letters and get suggestions. Zero JavaScript required.

3. The inert Attribute - Focus Control Made Easy

Ever had a modal popup where users could still tab to stuff behind it? Super annoying, right?

Before (the hard way):
You'd have to manually track all focusable elements, disable them, remember their state, re-enable them later... ugh.

After (the easy way):

<div class="page-content" inert>
  <!-- Everything in here becomes unclickable and unfocusable -->
  <button>Can't click me now</button>
  <input type="text">  <!-- Can't focus this either -->
</div>

<div class="modal">
  <!-- Only stuff in here is interactive -->
  <button>Close</button>
</div>
Enter fullscreen mode Exit fullscreen mode

That's it. One attribute. The browser handles everything, including making it work properly with screen readers.

4. The <dialog> Element - Modals That Don't Suck

Speaking of modals, stop building them from scratch with divs and a prayer.

Here's how simple it can be:

<dialog id="my-dialog">
  <h2>Hello!</h2>
  <p>This is a proper dialog.</p>
  <button onclick="this.closest('dialog').close()">Close</button>
</dialog>

<button onclick="document.getElementById('my-dialog').showModal()">
  Open Dialog
</button>
Enter fullscreen mode Exit fullscreen mode
const dialog = document.getElementById('my-dialog');

// Show it as a modal (with backdrop, makes everything else inert)
dialog.showModal();

// Or show it without the modal behavior
dialog.show();

// Close it
dialog.close();
Enter fullscreen mode Exit fullscreen mode

Control how it closes:

<!-- Only closes via JavaScript -->
<dialog closedby="none">...</dialog>

<!-- Closes with Escape key or JavaScript -->
<dialog closedby="closerequest">...</dialog>

<!-- Closes with Escape, clicking outside, or JavaScript -->
<dialog closedby="any">...</dialog>
Enter fullscreen mode Exit fullscreen mode

The backdrop (that dark overlay) comes free, everything else on the page becomes inert automatically, and it's accessible by default.

5. The <picture> Element - Responsive Images Done Right

Stop loading massive desktop images on phones. Your users on data plans will thank you.

Basic responsive images:

<picture>
  <!-- Small screens get the mobile version -->
  <source media="(max-width: 500px)" srcset="small-image.jpg">

  <!-- Medium screens get the tablet version -->
  <source media="(max-width: 1000px)" srcset="medium-image.jpg">

  <!-- Fallback for everything else -->
  <img src="large-image.jpg" alt="Description">
</picture>
Enter fullscreen mode Exit fullscreen mode

Handle high-DPI displays:

<picture>
  <source 
    media="(max-width: 500px)" 
    srcset="mobile.jpg 1x, mobile@2x.jpg 2x, mobile@3x.jpg 3x"
  >
  <img src="desktop.jpg" alt="Description">
</picture>
Enter fullscreen mode Exit fullscreen mode

Different crops for different orientations:

<picture>
  <!-- Portrait orientation gets a vertical crop -->
  <source 
    media="(orientation: portrait)" 
    srcset="portrait-crop.jpg"
  >

  <!-- Landscape gets a horizontal crop -->
  <img src="landscape-crop.jpg" alt="Description">
</picture>
Enter fullscreen mode Exit fullscreen mode

The browser picks the first matching source and only downloads that one image. Smart, right?

6. The <output> Element - Show Your Results

This one's simple but makes your forms way more accessible.

Instead of this:

<span id="total">$0.00</span>
Enter fullscreen mode Exit fullscreen mode

Do this:

<label>
  Price: 
  <input type="number" id="price" value="0">
</label>

<label>
  Quantity: 
  <input type="number" id="quantity" value="0">
</label>

<output for="price quantity" id="total">$0.00</output>
Enter fullscreen mode Exit fullscreen mode
function updateTotal() {
  const price = document.getElementById('price').value;
  const quantity = document.getElementById('quantity').value;
  document.getElementById('total').textContent = 
    '$' + (price * quantity).toFixed(2);
}
Enter fullscreen mode Exit fullscreen mode

Why bother? Screen readers understand that this is a calculated result related to those specific inputs. They can announce the change without moving focus away from what the user is doing. It's just better UX.

Wrapping Up

HTML keeps getting better, but a lot of us are still writing it like it's 2010. These tags aren't exotic or experimental anymore. They work, they're supported, and they solve real problems.

Next time you reach for a JavaScript library or start building something from scratch, take a minute to check if HTML already has what you need. You might be surprised.

Now go forth and use some proper HTML tags. Your future self (and your users) will appreciate it.

Top comments (0)