Skip to main content

CSS only Click-handlers You Might not be using, but you should

 


You’re building a simple website, a good-looking landing page with a “See More” button. Instinctively, you reach for JavaScript to handle the button click event.

But wait — what if I told you that CSS alone could do the job?

Yes. CSS is often underestimated, but it can handle click interactions without JavaScript.

In this guide, you’ll learn how to create CSS-only click handlers using the :target pseudo-class, and explore scenarios where this approach makes perfect sense.

The :target Pseudo-Class

CSS offers several pseudo-classes that let you style elements based on different states (:hover, :focus, :checked).

But there’s one you might not have used before — :target.

The :target pseudo-class applies styles to an element when its ID matches the fragment identifier in the URL (the part after #).

This behavior is commonly seen when clicking an anchor link that jumps to a section on the same page.

Here’s a simple example :

<a href="#contact">Go to Contact</a>

<section id="contact">This is the contact section.</section>

When you click the link, the browser scrolls to the element with the id=”contact”, and that element becomes the :target. You can use this to apply specific styles to that element — such as displaying hidden content.

Building a CSS-Only “See More” Toggle
Let’s create a basic landing page with a “See More” button that reveals additional content using only HTML and CSS.

HTML Structure:

<h1>Welcome to Our Site</h1>

<p>This is a simple landing page.</p>

<a href="#more" class="see-more-btn">See More</a>

<div id="more" class="more-content">

<a href="#" class="close-btn">Close</a>

<p>Here's some more information about our product or service!</p>

</div>



CSS Styling:

.more-content {

max-height: 0;

overflow: hidden;

transition: max-height 0.5s ease-out, padding 0.3s ease-out;

background: #f0f0f0;

padding: 0 1rem;

margin-top: 1rem;

}

/* When the section is targeted, expand it */

#more:target {

max-height: 200px; /* Adjust based on content */

padding: 1rem;

}

.see-more-btn,

.close-btn {

display: inline-block;

margin-top: 1rem;

background-color: #007BFF;

color: white;

padding: 0.5rem 1rem;

text-decoration: none;

border-radius: 4px;

}

.close-btn {

background-color: #dc3545;

}

How It Works

Clicking “See More” adds #more to the URL, targeting the #more div.

The :target pseudo-class applies, and styles change to reveal the content.

Inside that div, the “Close” link points to #, removing the fragment and collapsing the content.

Benefits of This Approach
No JavaScript required: Perfect for static sites, documentation, or minimal setups.

Improved performance: Fewer scripts mean faster loading and better accessibility.

Progressive enhancement: Works even if JavaScript is disabled.

Limitations to Keep in Mind
You can’t toggle back and forth with the same link — you need separate “open” and “close” links.

It relies on the URL’s hash, which could interfere with deep linking or browser history.

More complex UI interactions (like accordions, multiple toggles, or animations based on timing) are better handled with JavaScript.

Real-World Use Cases

Toggle FAQ sections

Show/hide forms

Navigation drawers in static sites

Interactive resumes or portfolios

Final Thoughts

CSS is often seen as just a styling tool, but with features like :target, it can take on roles you might’ve reserved for JavaScript. While it’s not a replacement for full interactivity, it’s a clever, fast, and simple way to build basic interactions without writing a single line of JavaScript.

So next time you’re about to reach for JS, ask yourself — can CSS handle this? You might be surprised!

Let me know if you’d like this turned into a CodePen or GitHub demo!


Comments

Popular posts from this blog

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...

6 Essential JavaScript Concepts Every Developer Should Understand

It’s the only language I’ve used where [] == ![] it's true and where you can, typeof null and somehow get 'object' . But despite all its quirks (and there are many), there are a few core concepts that make life with JS not just easier, but saner. This isn’t some computer science flex. These are practical concepts that, once you understand them, make you write better, cleaner, and less buggy code. 1. Hoisting  Before you rage at your variables being undefined , understand this: JS hoists variable and function declarations to the top of their scope. But —  and this is important  —  only the declarations , not the assignments. Why? Because JS reads it like: This is also why let and const behave differently — they’re hoisted too, but live in the “Temporal Dead Zone” until declared. 2. Closures Closures are like little memory vaults for your functions. They allow functions to remember variables from the scope they were created in, even after that scope has gone. Why care? T...