Although many of these features have been overlooked by developers, they can significantly improve both web development efficiency and user experience.
In this article, I'll reveal the rest of the iceberg and show you what HTML is truly capable of.
Let’s dive in!
1. The
Element
The element is one of those hidden gems in HTML5 that many developers haven't fully explored.
Without any need for frameworks like React, it serves as a container for holding HTML content that isn't rendered on the page immediately. Instead, this content can be cloned and inserted into the DOM later, usually via JavaScript.
It's ideal for scenarios where you need to render content dynamically, like in single-page applications (SPAs) or when you have components that get reused multiple times.
Here is how it works:
id="card-template">
class="card">
src="" alt="Image" class="card-img">
class="card-text">
const template = document.getElementById('card-template');
const clone = template.content.cloneNode(true);
clone.querySelector('.card-img').src = 'image.jpg';
clone.querySelector('.card-text').textContent = 'This is a dynamic card!';
document.body.appendChild(clone);
In this example, we have used the element to create a reusable card component that can be easily cloned and customized with JavaScript.
2. The
Element for Responsive Images
The
element is designed to help developers display different images based on different conditions, such as screen size or resolution.
This feature makes media queries in CSS almost obsolete.
Here's how you can implement the
element:
srcset="image-small.jpg" media="(max-width: 600px)">
srcset="image-large.jpg" media="(min-width: 601px)">
src="image-default.jpg" alt="Responsive Image">
In this example, we've defined different image sources for different screen sizes, ensuring that the appropriate image is loaded depending on the user's device.
3. The
Element for Enhanced Input Fields
The element is a great addition to HTML5, providing a way to offer predefined options within an input field.
It creates a drop-down list of suggestions that users can choose from, making forms more interactive and user-friendly.
Let's see how to use the element in a search input field:
for="search">Search:
list="suggestions" id="search" name="search">
id="suggestions">
value="HTML5">
value="CSS3">
value="JavaScript">
value="React">
4. Form Validation with HTML5 Attributes
HTML5 has introduced built-in form validation attributes such as required
, pattern
, minlength
, and maxlength
.
These attributes have allowed you to enforce input rules directly in the HTML, reducing the need for complex JavaScript validation.
Here's an example of a form with HTML5 validation:
for="username">Username:
type="text" id="username" name="username" required minlength="4" maxlength="20">
for="email">Email:
type="email" id="email" name="email" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$">
type="submit" value="Submit">
In this example, the element has displayed the sum of two numbers in real-time.
5. The
Element for Displaying Calculation Results
The element is a great tool in HTML5 for displaying the results of calculations or user actions.
It's particularly useful in interactive forms or web applications where you want to show immediate feedback without relying on JavaScript.
Here's how the element can be used:
oninput="result.value=parseInt(a.value)+parseInt(b.value)">
type="number" id="a" value="50"> +
type="number" id="b" value="100">
= name="result" for="a b">150
In this example, the element has displayed the sum of two numbers in real-time.
Made with ❤️ for modern front-end developers.
I hope this was helpful, and I’d be happy to connect and follow each other!