Close Modal Upon Click Outside (CSS + HTML Only)
Greetings and thank you in advance Summary Modal Works like a charm (close button works fine too) Need to close it when user clicks outside No JS / jQuery (CSS + HTML only) Simi
Solution 1:
Make Another a
inside your target with width and height match with position:fixed;
the parent and having less z-index
than the model content div
which should be position:relative
Change
in html:-
<a href="#" title="Close Menu" class="bodyclose"></a>
in css:-
.bodyclose {
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
display:none;
z-index:1;
position:fixed;
}
.modal:target {
opacity: 1;
pointer-events: auto;
}
.modal:target > .bodyclose {
display:block;
}
.modal > div {
background: rgba(115, 117, 117, 0.6);
width: 12em;
z-index:2;
position:relative;
}
body {
background: #111;
text-align: center;
}
a {
text-decoration: none;
}
.content {
text-align: justify;
text-justify: trim;
max-width: 60%;
margin: auto
}
.mbutton {
border: 1px solid rgba(115, 117, 117, 0.6);
padding: 8px 60px 8px 60px;
}
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
pointer-events: none;
}
.bodyclose {
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
display:none;
z-index:1;
position:fixed;
}
.modal:target {
opacity: 1;
pointer-events: auto;
}
.modal:target > .bodyclose {
display:block;
}
.modal > div {
background: rgba(115, 117, 117, 0.6);
width: 12em;
z-index:2;
position:relative;
}
.close {
margin: 0 auto 0.7em auto;
max-width: 15%;
}
.wrap,
.modal {
display: flex
}
.mbutton:hover,
.close,
.mitem {
color: #111
}
.content,
.mbutton,
.close:hover,
.mitem:hover {
color: #999
}
.mitem,
.close {
display: block
}
.wrap,
.modal {
align-items: center;
justify-content: center
}
.mbutton:hover,
.mitem {
background: rgba(150, 150, 150, 0.9);
}
.mitem,
.modal > div {
padding: 20px;
border: 1px solid rgba(0, 0, 0, 0.2)
}
.mitem:hover,
.modal {
background: rgba(0, 0, 0, 0.8)
}
.mbutton,
.modal > div {
margin: 5% auto;
}
<body>
<div class="wrap">
<a href="#M" class="mbutton">Menu</a>
<div id="M" class="modal">
<div>
<a href="#" title="Close Menu" class="close">☓</a>
<a href="http://google.com" class="mitem">Home</a>
<a href="http://yahoo.com" class="mitem">Photos</a>
<a href="http://bing.com" class="mitem">Details</a>
<a href="http://stackoverflow.com/" class="mitem">Contact</a>
</div>
<a href="#" title="Close Menu" class="bodyclose"></a>
</div>
</div>
<div class="content">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
voluptas sit aspernatur aut odit aut fugit,
<br>
<br>sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam
aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi
<br>
<br>>qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum
quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta.</p>
</div>
</body>
Post a Comment for "Close Modal Upon Click Outside (CSS + HTML Only)"