Can I have an onclick effect in CSS?


I have an image element that I want to change on click.

<img id="btnLeft">

This works:

#btnLeft:hover {

But what I need is:

#btnLeft:onclick {

But, it doesn't work, obviously. Is it possible at all to have onclick behavior in CSS (i.e. without using JavaScript)?

2/6/2020 7:16:50 PM

Accepted Answer

The closest you'll get is :active:

#btnLeft:active {
    width: 70px;
    height: 74px;

However this will only apply the style when the mouse button is held down. The only way to apply a style and keep it applied onclick is to use a bit of JavaScript.

11/29/2012 4:29:28 PM

You can use pseudo class :target to mimic on click event, let me give you an example.

#something {
  display: none;

#something:target {
  display: block;
<a href="#something">Show</a>
<div id="something">Bingo!</div>

Here's how it looks like:

One thing to note, this is only limited to hyperlink, so if you need to use on other than hyperlink, such as a button, you might want to hack it a little bit, such as styling a hyperlink to look like a button.


If you give the element a tabindex then you can use the :focus pseudo class to simulate a click.


<img id="btnLeft" tabindex="0" src="" />




Edit: Answered before OP clarified what he wanted. The following is for an onclick similar to javascripts onclick, not the :active pseudo class.

This can only be achieved with either Javascript or the Checkbox Hack

The checkbox hack essentially gets you to click on a label, that "checks" a checkbox, allowing you to style the label as you wish.

The demo


TylerH made a really good answer, I just had to give that last button a visual update.

.btn {
    border-radius: 5px;
    padding: 10px 30px;
    box-shadow: 1px 1px 1px #000;
    background-image: linear-gradient(to bottom, #eee, #ddd);

.btn:hover {
    background-image: linear-gradient(to top, #adf, #8bf);

.btn:active {
    margin: 1px 1px 0;
    box-shadow: -1px -1px 1px #000;

#btnControl {
    display: block;
    visibility: hidden;
<input type="checkbox" id="btnControl"/>
<label class="btn" for="btnControl">Click me!</label>


How about a pure CSS solution without being (that) hacky?

enter image description here

.page {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background-color: #121519;
  color: whitesmoke;

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;

.arrow {
  cursor: pointer;
  transition: filter 0.3s ease 0.3s;

.arrow:active {
  filter: drop-shadow(0 0 0 steelblue);
  transition: filter 0s;
<body class="page">
  <div class="controls">
    <div class="arrow">
      <img src="" />

@TylerH has a great response but its a pretty complex solution. I have a solution for those of you that just want a simple "onclick" effect with pure css without a bunch of extra elements.

We will simply use css transitions. You could probably do similar with animations.

The trick is to change the delay for the transition so that it will last when the user clicks.

.arrowDownContainer.clicked {
  filter: drop-shadow(0px 0px 0px steelblue);
  transition: filter 0s;

Here I add the "clicked" class as well so that javascript can also provide the effect if it needs to. I use 0px drop-shadow filter because it will highlight the given transparent graphic blue this way for my case.

I have a filter at 0s here so that it wont take effect. When the effect is released I can then add the transition with a delay so that it will provide a nice "clicked" effect.

.arrowDownContainer {
  cursor: pointer;
  position: absolute;
  bottom: 0px;
  top: 490px;
  left: 108px;
  height: 222px;
  width: 495px;
  z-index: 3;
  transition: filter 0.3s ease 0.3s;

This allows me to set it up so that when the user clicks the button, it highlights blue then fades out slowly (you could, of course, use other effects as well).

While you are limited here in the sense that the animation to highlight is instant, it does still provide the desired effect. You could likely use this trick with animation to produce a smoother overall transition.

enter image description here

enter image description here