# Pure CSS Galley Carousel with Keypress?

**URL:** <https://forum.kirupa.com/t/pure-css-galley-carousel-with-keypress/641469>\
**Category:** web dev\
**Created:** [December 30, 2019, 12:36pm UTC](https://forum.kirupa.com/t/pure-css-galley-carousel-with-keypress/641469 "2019-12-30T12:36:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![4a0g1kfa54](https://avatars.discourse-cdn.com/v4/letter/4/5daacb/32.png) [@4a0g1kfa54](https://forum.kirupa.com/u/4a0g1kfa54)\
**Post date:** [December 30, 2019, 12:36pm UTC](https://forum.kirupa.com/t/pure-css-galley-carousel-with-keypress/641469/1 "2019-12-30T12:36:17Z")

</div>

Hi I have a pure css carousel lightbox which is working fine.

However, I would like to do three things:  
-when i press ESC it simulates clicking the close button;  
-when pressing left and right it simulates clicking the left/right button; and,  
-when i click off the image, the lightbox closes.  
Ideally it would be great if this can be done in pure css.

Here is a [fiddle](https://jsfiddle.net/postcolonialboy/Lxa2pgrw/3/).

(If not, willing to accept jquery :P)

Here is what I have:

```
<span class="feature_category active" id="A">
  <div class="feature_box_wrapper"><a href="#groys1">Link</a></div>
  <div class="feature_box_wrapper"><a href="#groys2">Link</a></div>
  <div class="feature_box_wrapper"><a href="#groys3">Link</a></div>
   <div class="feature_box_wrapper"><a href="#groys4">Link</a></div>
   <div class="feature_box_wrapper"><a href="#groys5">Link</a></div>
</span>

<div class="lightbox">
  <!-- Groys -->
  <div class="lightbox__slide" id="groys1">
    <a href="#_" class="btn btn--close"></a>
    <a href="#groys5" class="btn btn--left"></a>
    <a href="#groys2" class="btn btn--right"></a>
    <img src="https://picsum.photos/seed/picsum/800/450" alt="Screenshot of your site">
  </div>
  <div class="lightbox__slide" id="groys2">
    <a href="#_" class="btn btn--close"></a>
    <a href="#groys1" class="btn btn--left"></a>
    <a href="#groys3" class="btn btn--right"></a>
    <img src="https://picsum.photos/seed/picsum/800/450" alt="Screenshot of your site">
  </div>
  <div class="lightbox__slide" id="groys3">
    <a href="#_" class="btn btn--close"></a>
    <a href="#groys2" class="btn btn--left"></a>
    <a href="#groys4" class="btn btn--right"></a>
    <img src="https://picsum.photos/seed/picsum/800/450" alt="Screenshot of your site">
  </div>
  <div class="lightbox__slide" id="groys4">
    <a href="#_" class="btn btn--close"></a>
    <a href="#groys3" class="btn btn--left"></a>
    <a href="#groys5" class="btn btn--right"></a>
    <img src="https://picsum.photos/seed/picsum/800/450" alt="Screenshot of your site">
  </div>
  <div class="lightbox__slide" id="groys5">
    <a href="#_" class="btn btn--close"></a>
    <a href="#groys4" class="btn btn--left"></a>
    <a href="#groys1" class="btn btn--right"></a>
    <img src="https://picsum.photos/seed/picsum/800/450" alt="Screenshot of your site">
  </div>

  <div class="lightbox__bg"></div><!-- Close -->
</div><!-- Close Lightbox -->

```

CSS

```
.lightbox__slide img {
  position: absolute;
  z-index: 30;
  max-width: 90%;
  max-height: 80%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-name: hide;
  animation-duration: 0.5s;
  animation-iteration-count: 1;
  animation-direction: linear;
  animation-fill-mode: forwards;
}

.lightbox__slide:target .btn {
  display: block;
}

.lightbox__slide:target img {
  opacity: 0;
  animation-name: show;
  animation-duration: 0.5s;
  animation-iteration-count: 1;
  animation-direction: linear;
  animation-fill-mode: forwards;
}

.lightbox __slide:target~.lightbox__ bg {
  position: relative;
  background: white;
  opacity: 0.6;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

@-webkit-keyframes show {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

@-webkit-keyframes hide {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.btn {
  position: absolute;
  z-index: 20;
  display: none;
  transition: all 0.2s;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  text-align: center;
  line-height: 40px;
  text-decoration: none;
  color: black;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.8);
}

.btn--close {
  top: 40px;
  right: 20px;
}

.btn--close:after {
  content: '\2715';
}

.btn--left {
  top: 50%;
  left: 40px;
}

.btn--left:after {
  content: '⯇';
}

.btn--right {
  top: 50%;
  right: 20px;
}

.btn--right:after {
  content: '⯈';
}

```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 30, 2019, 5:12pm UTC](https://forum.kirupa.com/t/pure-css-galley-carousel-with-keypress/641469/2 "2019-12-30T17:12:35Z")

</div>

Unfortunately, for handling keyboard events, you’ll have to go into JavaScript territory. There are ways to hook into some of the default input elements’ keyboard behavior, but I consider those more hacks than not. This article can help you get started: [https://www.kirupa.com/html5/keyboard\_events\_in\_javascript.htm](https://www.kirupa.com/html5/keyboard_events_in_javascript.htm)

🙂
