Pure css close modal when clicking outside content












1















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.. :/










share|improve this question























  • It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

    – admcfajn
    Nov 20 '18 at 17:44
















1















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.. :/










share|improve this question























  • It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

    – admcfajn
    Nov 20 '18 at 17:44














1












1








1








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.. :/










share|improve this question














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.. :/






.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>





.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>






html css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 17:41









lilolilo

218




218













  • It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

    – admcfajn
    Nov 20 '18 at 17:44



















  • It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

    – admcfajn
    Nov 20 '18 at 17:44

















It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

– admcfajn
Nov 20 '18 at 17:44





It seems to work if I click lower down on the view... Maybe add * {box-shadow: inset 0 0 1px rgba(0,0,0,0.3)} so that you can see all of the elements without using the inspector?

– admcfajn
Nov 20 '18 at 17:44












2 Answers
2






active

oldest

votes


















1














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>








share|improve this answer































    0














    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>








    share|improve this answer
























    • I tried but still does not work :/

      – lilo
      Nov 20 '18 at 18:11











    • What browser? I tested in chrome & it worked.

      – admcfajn
      Nov 20 '18 at 22:01











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398600%2fpure-css-close-modal-when-clicking-outside-content%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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>








    share|improve this answer




























      1














      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>








      share|improve this answer


























        1












        1








        1







        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>








        share|improve this answer













        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>








        .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>





        .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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 17:48









        New DeveloperNew Developer

        762




        762

























            0














            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>








            share|improve this answer
























            • I tried but still does not work :/

              – lilo
              Nov 20 '18 at 18:11











            • What browser? I tested in chrome & it worked.

              – admcfajn
              Nov 20 '18 at 22:01
















            0














            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>








            share|improve this answer
























            • I tried but still does not work :/

              – lilo
              Nov 20 '18 at 18:11











            • What browser? I tested in chrome & it worked.

              – admcfajn
              Nov 20 '18 at 22:01














            0












            0








            0







            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>








            share|improve this answer













            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>








            .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>





            .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>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 17:48









            admcfajnadmcfajn

            1,16711123




            1,16711123













            • I tried but still does not work :/

              – lilo
              Nov 20 '18 at 18:11











            • What browser? I tested in chrome & it worked.

              – admcfajn
              Nov 20 '18 at 22:01



















            • I tried but still does not work :/

              – lilo
              Nov 20 '18 at 18:11











            • What browser? I tested in chrome & it worked.

              – admcfajn
              Nov 20 '18 at 22:01

















            I tried but still does not work :/

            – lilo
            Nov 20 '18 at 18:11





            I tried but still does not work :/

            – lilo
            Nov 20 '18 at 18:11













            What browser? I tested in chrome & it worked.

            – admcfajn
            Nov 20 '18 at 22:01





            What browser? I tested in chrome & it worked.

            – admcfajn
            Nov 20 '18 at 22:01


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398600%2fpure-css-close-modal-when-clicking-outside-content%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]