lilo
lilo

Reputation: 195

Pure css close modal when clicking outside content

Hi there I am having trouble closing the modal when I click outside the content, I want to do this with css.. but it is not working I am gone show you the code maybe someone can help me..

.overlay{
  height: 100vh;
  width: 100%;
  position: fixed;
  top:0;
  left: 0;
  background-color: rgba(50, 65, 97, 0.5);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all .3s; 
   }
.overlay .cancel {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
}


.overlay__content {
    position: absolute;
    top: 44%;
    left: 55.5%;
    padding:4rem 9.5rem;
    width: 540px;
    background-color: $color-white;
    box-shadow: 0 2rem 4rem rgba($color-primary, .2);
    border-radius: 3px;
    display: table;
    overflow: hidden;
    opacity: 0;
    transform: translate(-50%, -50%) scale(.25);
    transition: all .4s .2s;
  }




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

  .overlay:target .overlay__content {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  <a href="#modal1" class="add-roles-btn">My Modal</a>
  
    <div class="overlay" id="modal1">
                  <a class="cancel" href="#_"></a>
                  <div class="overlay__content">
                  something
                  </div>
      </div>

If someone can help me I would be thankful because I am new on coding and I am stuck so long with this.. :/

Upvotes: 1

Views: 1980

Answers (2)

admcfajn
admcfajn

Reputation: 2083

Your code seems to be working fine. Try clicking in the red in the snippet below. I think you just need to change the background color of your modal so that you know when clicking inside the modal & when clicking outside of it.

.overlay{
  height: 100vh;
  width: 100%;
  position: fixed;
  top:0;
  left: 0;
  background-color: rgba(50, 65, 97, 0.5);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all .3s; 
   }
.overlay .cancel {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
}


.overlay__content {
    position: absolute;
    top: 44%;
    left: 55.5%;
    padding:4rem 9.5rem;
    width: 540px;
    background-color: $color-white;
    box-shadow: 0 2rem 4rem rgba($color-primary, .2);
    border-radius: 3px;
    display: table;
    overflow: hidden;
    opacity: 0;
    transform: translate(-50%, -50%) scale(.25);
    transition: all .4s .2s;

    
  }




  .overlay:target {
    opacity: 1;
    visibility: visible;
    background-color: #f00;
  }

  .overlay:target .overlay__content {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
    background-color: #fff;
  }
  <a href="#modal1" class="add-roles-btn">My Modal</a>
  
    <div class="overlay" id="modal1">
                  <a class="cancel" href="#_"></a>
                  <div class="overlay__content">
                  something
                  </div>
      </div>

Upvotes: 1

New Developer
New Developer

Reputation: 76

just remove _

<a class="cancel" href="#_"></a>

and change it to

<a class="cancel" href="#"></a>

.overlay{
  height: 100vh;
  width: 100%;
  position: fixed;
  top:0;
  left: 0;
  background-color: rgba(50, 65, 97, 0.5);
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all .3s; 
   }
.overlay .cancel {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
}


.overlay__content {
    position: absolute;
    top: 44%;
    left: 55.5%;
    padding:4rem 9.5rem;
    width: 540px;
    background-color: $color-white;
    box-shadow: 0 2rem 4rem rgba($color-primary, .2);
    border-radius: 3px;
    display: table;
    overflow: hidden;
    opacity: 0;
    transform: translate(-50%, -50%) scale(.25);
    transition: all .4s .2s;
  }




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

  .overlay:target .overlay__content {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
<a href="#modal1" class="add-roles-btn">My Modal</a>
  
<div class="overlay" id="modal1">
	<a class="cancel" href="#"></a>
	<div class="overlay__content">
		something
	</div>
</div>

Upvotes: 1

Related Questions