Thereβs often a point when youβre building a website, especially a simple landing page, when you realize youβre going to have to introduce some JavaScript. CSS, however, can often do a lot more than we give it credit for. Here weβll look at creating click handlers with only CSS! π₯
Letβs say weβre building an HTML and CSS landing page and we decide we want a βSee more!β button to display a section of text. As soon as we think of a click event, usually it leads us to one word: JavaScript.
As web developers, most of us spend a lot of time with JavaScript-- and thatβs great! However, there are tons of things we can do with just CSS that we may not even know about! π
There are lots pseudo-classes in CSS that can help us style elements in different states. For example, you can style a button when itβs hovered, active, focused, etc.
One pseudo-class you might not have heard about, though, is the :target
pseudo-class.
The :target
pseudo-class is used to select an element when that elementβs ID matches part of the current URL.
A common use case for having an elementβs ID show up in the URL is if youβre using an anchor tag (<a>
) to jump to a specific spot on the current page. For example, if itβs your personal website, you might have a βContactβ button at the top of the page. On click, it could bring the user to the footer to see all your contact information.
If youβve used an <a>
tag before, youβre likely familiar with the href
attribute. If youβre trying to link to a website, like Alligator.io for example, youβd create the link in your HTML like this:
<a href='https://alligator.io'>
Click me! 🐊
</a>
However, if you want your user to stay on the current page and jump down to the footer, for example, all you have to do is set an ID on the footer and use that ID for the href
value in the <a>
tag.
<a href='#footer'>
Go to the footer!
</a>
When the user clicks on this footer link, the page will jump to the footer and their current URL will get updated to look like:
https://www.imawebsite.com/#footer
The footer elementβs ID is now part of the current URL. In other words, itβs the target! π€
Now that we know how to create a target with HTML, how do we use that target to create a click handler without any JavaScript? Thankfully, it takes just a little CSS! π
Using our βSee more!β button example from above, letβs start by creating a link to see more text:
<a href='#seeMore'>
See more!
</a>
Our section of text we want to see doesnβt exist yet, so letβs create that too.
<a href='#seeMore'>
See more!
</a>
<section id='seeMore'>
<p>
Here's some more info that you couldn't see before. I can only be seen after you click the "See more!" button.
</p>
</section>
When you click the βSee more!β button, the URL will update to look like this:
https://www.imawebsite.com/#seeMore
The problem we have now is that the #seeMore
section is visible to the user even though itβs not supposed to be yet! π
Since this is all the HTML weβll need so far, letβs add our CSS to manage showing the text block on click.
First, letβs hide the text that shouldnβt show yet.
<style>
#seeMore {
display: none;
}
</style>
Now the text in the #seeMore
section doesnβt show on load or when you click the βSee more!β button. This is where the :target
styling comes in. Letβs use the :target
pseudo-class to update the styling when the βSee more!β button gets clicked.
<style>
#seeMore {
display: none;
}
#seeMore:target {
display: block;
}
</style>
It is literally as simple as that! On load, our text section will not show. As soon as you click the βSee more!β button, it will add #seeMore
to the URL and the #seeMore
section becomes the target. Once #seeMore
becomes the target, it will have its :target
styling applied, which displays the text. π₯³
If an element wasnβt visible to begin with, you will probably want the option to hide it again.
Luckily, we can do that with just one more line of HTML (no CSS!) πͺ
Using the same example as above, letβs expand the HTML to include a βHide textβ button.
<a href='#seeMore'>See more!</a>
<section id='seeMore'>
<p>
Here's some more info that you couldn't see before. I can only be seen after you click the "See more!" button.
</p>
<a href='#'>Hide text</a>
</section>
Notice that thereβs a new <a>
tag in the text section. Since itβs in the element that only shows when the user clicks βSee more!β, the βHide textβ button will only show if the hidden text becomes visible. That is, the user doesnβt need to see the button to hide text unless thereβs text to hide.
The href
value on the βHide textβ button is β#β. This is because we want to update the URL to no longer include #seeMore
. When the βHide textβ button is clicked, it will update the URL to look like this:
https://www.imawebsite.com/#
With the URL updated, #seeMore
is no longer the target, and the #seeMore:target
styling no longer gets applied. The block of text (including the βHide textβ button) will, therefore, go back to having the display: none;
styling applied.
In short, update the URL and the text that was originally not displayed goes back to not being displayed. We officially have a way to toggle the text! β¨
If youβre not sure when you would actually use the :target
pseudo-class, here are some examples of how you could:
The browser support for the :target
pseudo-class is fantastic and you basically donβt need to worry about it unless youβre supporting IE8. As always, though, check Can I Use to be sure. π
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the βreport an issueβ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!