Home > Software engineering >  Create simple CSS modal window
Create simple CSS modal window

Time:11-13

The goal is to create a simple and CSS-only modal window (popup), when the user clicks a link.

With no dependencies or any kind of script, and with as less code as possible.

CodePudding user response:

I created this simple modal window:

.exit-intent {
  opacity: 0;
  visibility: hidden;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  text-align: center;
  background-color: rgba(255, 255, 255, .8);
  z-index: 7;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto
}

.exit-intent {
  position: fixed;
  max-width: 500px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.9);
  visibility: hidden;
  opacity: 0;
  z-index: 1;
}

.exit-intent:target {
  visibility: visible;
  opacity: 1;
}

.exit-intent-close {
  position: absolute;
  max-width: 500px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.9);
}

.exit-intent .close {
  position: absolute;
  right: 5px;
  top: 5px;
  padding: 5px;
  color: #000;
  font-size: 2em;
  line-height: 0.6em;
  font-weight: bold;
}

.exit-intent .close:hover {
  color: #999;
}

.close-exit-intent {
  background: rgba(0, 0, 0, 0.7);
  cursor: default;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  visibility: hidden;
}

.exit-intent:target .close-exit-intent {
  opacity: 1;
  visibility: visible;
}
<a href="#exit-intent">Link</a>
<div id="exit-intent" >
  <a href="#" >&times;</a>
  <h2>Window</h2>
</div>
<a href="#" ></a>

CodePudding user response:

.box {
  width: 40%;
  margin: 0 auto;
  background: rgba(255,255,255,0.2);
  padding: 35px;
  border: 2px solid #fff;
  border-radius: 20px/50px;
  background-clip: padding-box;
  text-align: center;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}

.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
<div >
    <a  href="#popup1">Open</a>
</div>

<div id="popup1" >
    <div >
        <h2>Here i am</h2>
        <a  href="#">&times;</a>
    </div>
</div>

  • Related