<article>
, <div>
or <section>
tags. Each post can have attributes or classes that can be used to identify its characteristics, such as a category or tag. For example, an HTML blog post might look like this:<article class="post category-news tag-technology">
<h2>New Tech Innovations in 2024</h2>
<p>Some interesting tech news...</p>
</article>
The filtering mechanism is based on a comparison between the filter criteria and the attributes or classes of the blog posts. When a user selects a filter option, JavaScript can be used to manipulate the CSS properties of the posts to show or hide them accordingly. For example, if the user selects a “news” category filter, the JavaScript code can add a CSS class to all non - news posts that sets their display
property to none
.
First, we need to create a proper HTML structure for the blog posts and the filter controls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog with Filters</title>
</head>
<body>
<!-- Filter controls -->
<div id="filters">
<button data - filter="all">All</button>
<button data - filter="news">News</button>
<button data - filter="reviews">Reviews</button>
</div>
<!-- Blog posts -->
<div id="blog - posts">
<article class="post news">
<h2>Latest Tech News</h2>
<p>Some tech news here...</p>
</article>
<article class="post reviews">
<h2>Product Review: XYZ</h2>
<p>Our thoughts on the XYZ product...</p>
</article>
</div>
<script>
const filterButtons = document.querySelectorAll('#filters button');
const blogPosts = document.querySelectorAll('.post');
filterButtons.forEach(button => {
button.addEventListener('click', function () {
const filterValue = this.dataset.filter;
blogPosts.forEach(post => {
if (filterValue === 'all' || post.classList.contains(filterValue)) {
post.style.display = 'block';
} else {
post.style.display = 'none';
}
});
});
});
</script>
</body>
</html>
In this example, we have a set of filter buttons with data - filter
attributes. When a button is clicked, the JavaScript code loops through all the blog posts and sets their display
property to block
if they match the filter criteria or none
if they don’t.
CSS can be used to style the filter buttons and the blog posts. Here is an example of CSS code to style the filter buttons and blog posts:
#filters button {
background-color: #007BFF;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
cursor: pointer;
border-radius: 5px;
}
#filters button:hover {
background-color: #0056b3;
}
.post {
border: 1px solid #ccc;
padding: 15px;
margin: 10px;
border-radius: 5px;
}
One of the most common practices is category - based filtering. For example, a blog might have categories like “news”, “reviews”, “tutorials”. The HTML for blog posts can include a class for each category:
<article class="post category - news">
<h2>Breaking Tech News</h2>
<p>Some breaking news content...</p>
</article>
<article class="post category - reviews">
<h2>Product Review: ABC</h2>
<p>Our review of the ABC product...</p>
</article>
The filter buttons can be set up to filter based on these categories. The JavaScript code can then check the class of each post and show or hide them accordingly.
Tags are another popular way to filter blog posts. A single blog post can have multiple tags. For example:
<article class="post tag - tech tag - startup">
<h2>Startup Tech Innovations</h2>
<p>How startups are changing the tech world...</p>
</article>
The filter buttons can be created for each tag, and the filtering logic can be adjusted to show posts that contain the selected tag.
Date - based filtering allows users to view blog posts within a specific time frame. For this, the blog post HTML can include a data - date
attribute with the publication date. The JavaScript code can then compare the dates and show or hide posts based on the selected date range.
<article>
, <section>
and <nav>
for blog posts and filter controls. This helps screen readers understand the structure and makes the content more accessible.styles.css
file for all your CSS styles and a script.js
file for all your JavaScript functionality.CSS and HTML blog post filters are powerful tools that enhance the user experience on blogs by allowing users to quickly find the content they are interested in. By understanding the fundamental concepts, using proper usage methods, and following common and best practices, you can create an efficient and user - friendly filtering system. Whether it’s category - based, tag - based, or date - based filtering, these techniques can be customized to meet the specific needs of your blog.