5+ JavaScript Close Button Examples

This post contains a total of 5+ Hand-Picked JavaScript Close Button Examples with Source Code. All the Close Buttons are made using JavaScript & Styled using CSS.

You can use the source code of these examples for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Pekka Wallenius

Made by Pekka Wallenius. Three dots to close button using JavaScript. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>  
<style>
body {
  background: black;
}

.container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 45px;
  height: 45px;
  cursor: pointer;
}

.dot {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 9px;
  height: 9px;
  background: white;
  border-radius: 50%;
  transition: all 0.35s ease;
  -webkit-backface-visibility: hidden;
          backface-visibility: hidden;
}
.container.hover .dot {
  background: transparent;
}
.dot::before, .dot::after {
  display: block;
  content: "";
  position: absolute;
  bottom: 0;
  background: white;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  transition: all 0.35s ease;
}
.container.hover .dot::before, .container.hover .dot::after {
  height: 45px;
  border-radius: 0;
}
.dot::before {
  left: -18px;
}
.container.hover .dot::before {
  transform: rotate(45deg);
  transform-origin: right bottom;
  border-radius: 9px;
}
.dot::after {
  right: -18px;
}
.container.hover .dot::after {
  transform: rotate(-45deg);
  transform-origin: left bottom;
  border-radius: 9px;
}
</style>
</head>
<body>
  <div class="container">
  <div class="dot"></div>
</div>
  <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
      <script>
$('.container').click('on', function () {
  $(this).toggleClass('hover');
});
    </script>
</body>
</html>

2. By VΓ­ctor Silva

Made by VΓ­ctor Silva. Open/Close Menu Button. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>
  <style>
body{
  background:#E3355C;
}

#menu-open-close{
  width: 30px;
  height: 30px;
  margin: 100px auto;
  position: relative;
  border-top: 3px solid #fff;
  border-color:#fff;
  display: block;
  -webkit-transition: all 0.4s;
  -moz-transition: all 0.4s;
  -o-transition: all 0.4s;
  transition: all 0.4s;
  top: 0px;
}
  
#menu-open-close:after, #menu-open-close:before {
  content: '';
  position: absolute;
  width: 100%;
  height: 0px;
  border-top: 3px solid #fff;
  left: 0px;
  -webkit-transition: all 0.4s;
  -moz-transition: all 0.4s;
  -o-transition: all 0.4s;
  transition: all 0.4s;
}
    
#menu-open-close:after {
  top: 6px;
}
  
#menu-open-close:before {
  top: 15px;
}
  
#menu-open-close.open {
  border: 3px solid #fff;
  border-radius: 50%;
  top: -8px;
  -webkit-transform: rotate(135deg);
  -moz-transform: rotate(135deg);
  -ms-transform: rotate(135deg);
  -o-transform: rotate(135deg);
  transform: rotate(135deg);
}
    
#menu-open-close.open:before,#menu-open-close.open:after{
  top: 14px;
  left: 3px;
  width: 80%; 
}
    
#menu-open-close.open:after {
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  
}
  
#menu-open-close.open:before {
  -webkit-transform: rotate(-90deg);
  -moz-transform: rotate(-90deg);
  -ms-transform: rotate(-90deg);
  -o-transform: rotate(-90deg);
  transform: rotate(-90deg);
}
</style>
</head>
<body>
  <a href="#" id="menu-open-close"></a>
    <script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-1b93190375e9ccc259df3a57c1abc0e64599724ae30d7ea4c6877eb615f89387.js"></script>

  <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
      <script id="rendered-js" >
$(function () {
  setInterval(function () {
    $('#menu-open-close').toggleClass('open');
  }, 1000);
});
    </script>
</body>
</html>

3. By Caleb Duren

Made by Caleb Duren. Js Close button with Hover Effects. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

<style>
@import url(https://fonts.googleapis.com/css?family=Roboto:400,);
@-webkit-keyframes slideDown {
  0% {
    -webkit-transform: translateY(0);
  }
  10% {
    -webkit-transform: translateY(-8px);
  }
  40% {
    -webkit-transform: translateY(100px);
  }
  50% {
    -webkit-transform: translateY(-8px);
  }
  65% {
    -webkit-transform: translateY(4px);
  }
  85% {
    -webkit-transform: translateY(-2px);
  }
  100% {
    -webkit-transform: translateY(0);
  }
}
@keyframes slideDown {
  0% {
    transform: translateY(0);
  }
  10% {
    transform: translateY(-8px);
  }
  40% {
    transform: translateY(100px);
  }
  50% {
    transform: translateY(-8px);
  }
  65% {
    transform: translateY(4px);
  }
  85% {
    transform: translateY(-2px);
  }
  100% {
    transform: translateY(0);
  }
}
* {
  box-sizing: border-box;
}

body {
  display: table;
  height: 100%;
  width: 100%;
  font-family: Roboto, Helvetica Neue, sans-serif;
}

.text {
  font-size: 18px;
  text-transform: uppercase;
  color: #000;
  font-weight: 400;
  margin: 0 auto 30px;
  text-align: center;
}

.vertical-align {
  display: table-cell;
  vertical-align: middle;
  height: 100vh;
}

.close-button-container {
  width: 30px;
  position: relative;
  margin: 0 auto;
}

.close-button {
  position: absolute;
  margin: 0 auto;
  border: 2px solid #1a1a1a;
  background: transparent;
  border-radius: 20px;
  padding: 15px 15px 15px;
  cursor: pointer;
  height: 30px;
  width: 30px;
  transition: all 500ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.close-button:hover {
  border: 2px solid #333333;
  transition: all 250ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.close-button:after, .close-button:before {
  position: absolute;
  top: 3px;
  right: 3px;
  bottom: 0;
  left: 50%;
  transform: rotate(45deg) translateX(0) translateY(0);
  height: 1.5em;
  width: 0.15em;
  background: #1a1a1a;
  content: "";
  display: block;
  margin: 0 0 0 -1px;
  transition: all 500ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.close-button:hover:after, .close-button:hover:before {
  background: #333333;
}
.close-button:hover:after {
  transform: rotate(-315deg) translateX(7px) translateY(1px);
  height: 0.9em;
  width: 0.15em;
  transition: all 250ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.close-button:hover:before {
  transform: rotate(-405deg) translateX(-7px) translateY(1px);
  height: 0.9em;
  width: 0.15em;
  transition: all 250ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.close-button:before {
  transform: rotate(-45deg) translateX(0) translateY(0);
}

.slide-down {
  -webkit-animation-name: slideDown;
  animation-name: slideDown;
  -webkit-animation-duration: 1200ms;
  animation-duration: 1200ms;
  -webkit-animation-iteration-count: 1;
  animation-iteration-count: 1;
  -webkit-animation-timing-function: ease-in-out;
  animation-timing-function: ease-in-out;
}
</style>
</head>
<body>
  <body>
  <div class='vertical-align'>
    <div class='text'>Mouse over, mouse out, and click</div>
    <div class='close-button-container'>
      <div class='close-button'></div>
    </div>
  </div>
</body>
  <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
      <script>
$('.close-button').click(function () {
  var _this = this;
  $(_this).addClass('slide-down');
  setTimeout(function () {
    $(_this).removeClass('slide-down');
  }, 1250);
});
    </script>
</body>
</html>

4. By Mark

Made by Mark. Hamburger Menu Button to Close Button. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  background-color: #000;
  background: linear-gradient(#1f1f21, #000);
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.main {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.sidebar-menu-btn {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 40px;
  padding: 9px;
  margin: auto;
  background-color: #000;
  border: 2px solid #ffff7c;
  border-radius: 50%;
  box-shadow: 0px 0px 25px 5px rgba(255, 255, 124, 0.25);
}
.sidebar-menu-btn__line {
  display: block;
  width: 20px;
  height: 1px;
  background-color: #ffff7c;
  transition: all 0.5s ease;
}
.sidebar-menu-btn.active .sidebar-menu-btn__line:nth-of-type(1) {
  transform: rotateZ(-45deg) translate(-4px, 5px);
}
.sidebar-menu-btn.active .sidebar-menu-btn__line:nth-of-type(2) {
  transform: scale(0, 1);
}
.sidebar-menu-btn.active .sidebar-menu-btn__line:nth-of-type(3) {
  transform: rotateZ(45deg) translate(-4px, -4px);
}
</style>
</head>
<body>
  <div class='main'>

    <a class='sidebar-menu-btn'>
        <span class='sidebar-menu-btn__line'></span>
        <span class='sidebar-menu-btn__line'></span>
        <span class='sidebar-menu-btn__line'></span>
    </a>

</div>
      <script>
let btn = document.querySelector(".sidebar-menu-btn");
btn.onclick = function () {
  btn.classList.toggle("active");
};

// Animate on load
setTimeout(function () {
  btn.classList.add("active");
}, 1000);
    </script>
</body>
</html>

5. By Murphy Randle

Made by Murphy Randle. A simple JavaScript close button with rotating animation. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

<style>
* {
  box-sizing: border-box;
}

html {
  background-color: #C9353B;
}

.open-close-button {
  display: inline-block;
  width: 2em;
  height: 2em;
  border: 0.1em solid #f4d6d7;
  margin: 2em 45%;
  font-size: 3em;
  border-radius: 50%;
  position: relative;
  -moz-transition: 0.5s;
  -o-transition: 0.5s;
  -webkit-transition: 0.5s;
  transition: 0.5s;
  -moz-transform: translateZ(0);
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}
.open-close-button:before {
  content: "";
  display: block;
  position: absolute;
  background-color: #f4d6d7;
  width: 80%;
  height: 6%;
  left: 10%;
  top: 47%;
}
.open-close-button:after {
  content: "";
  display: block;
  position: absolute;
  background-color: #f4d6d7;
  width: 6%;
  height: 80%;
  left: 47%;
  top: 10%;
}
.open-close-button.open {
  background-color: #f4d6d7;
  -moz-transform: rotate(225deg);
  -ms-transform: rotate(225deg);
  -webkit-transform: rotate(225deg);
  transform: rotate(225deg);
}
.open-close-button.open:after {
  background-color: #C9353B;
}
.open-close-button.open:before {
  background-color: #C9353B;
}
</style>
</head>
<body>
  <div class='open-close-button'></div>
  <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
      <script>
(function() {
  var openclose;

  openclose = function() {
    return $(".open-close-button").click(function() {
      return $(this).toggleClass("open");
    });
  };

  jQuery(document).ready(function($) {
    return openclose();
  });

}).call(this);
    </script>
</body>
</html>

6. By Martin Wolf

Made by Martin Wolf. Open and close button with animation. Source

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title></title>

<style>
* {
  margin: 0;
  padding: 0;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

@keyframes rotate-to-right {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes rotate-to-left {
  from {
    transform: rotate(360deg);
  }
  to {
    transform: rotate(0deg);
  }
}
body {
  padding: 2em;
  font-family: 'Helvetica Neue', Arial;
}

h1 {
  margin-bottom: 1em;
  font-size: 1.5em;
  line-height: 1em;
}

p {
  font-size: 1em;
  line-height: 1.3;
}

.box-wrap {
  position: relative;
  padding-top: 20px;
  width: 300px;
}

.box {
  display: none;
}
.box.is-active {
  display: block;
}

.toggler {
  -moz-transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
  -moz-transition: left 1s ease-in-out;
  -o-transition: left 1s ease-in-out;
  -webkit-transition: left 1s ease-in-out;
  transition: left 1s ease-in-out;
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
  width: 21px;
  height: 21px;
  color: #000;
  font-family: Monospace;
  font-size: 20px;
  line-height: 19px;
  text-align: center;
  text-decoration: none;
  background: #fff;
  border-radius: 50%;
  border: 1px solid #000;
  cursor: pointer;
}
.toggler.rotate-to-right {
  animation-name: rotate-to-right;
  animation-duration: 0.5s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
.toggler.rotate-to-left {
  animation-name: rotate-to-left;
  animation-duration: 0.5s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
.toggler.is-active {
  font-size: 24px;
  left: 100%;
  transform: rotate(45deg);
}
</style>
</head>
<body>
  <h1>Click the button</h1>
<div class="box-wrap">
  <button class="toggler js_toggler">+</button>
  <div class="box js_box">
    <p>Some text here</p>
  </div>
</div>
  <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
      <script>
$(document).ready(function () {

  var toggler = $('.js_toggler'),
  box = $('.js_box');

  toggler.on('click', function (ev) {
    ev.preventDefault;

    if (!toggler.data('active')) {
      // If toggler is not active

      toggler.
      addClass('rotate-to-right').
      addClass('is-active');

      setTimeout(function () {
        toggler.
        removeClass('rotate-to-right').
        data('active', '1');
        box.addClass('is-active');
      }, 1000); // 1 second = .toggler transition

    } else {
      // If toggler is already active

      box.removeClass('is-active');

      toggler.
      addClass('rotate-to-left').
      removeClass('is-active');

      setTimeout(function () {
        toggler.
        removeClass('rotate-to-left').
        removeData('active');
      }, 1000); // 1 second = .toggler transition

    }
  });

});
    </script>
</body>
</html>