Pure CSS lightbox with automatic closing on click

If you have any insights on improving this code, I would greatly appreciate it.

I've developed a type of "lightbox", but the issue is that it closes whenever the user clicks anywhere. This is not my intention. I want it to only close if the user clicks outside of the window, not within it.

You can view a live demo at http://jsfiddle.net/Bw3Ku/1/

.box {
    display: none;
    position: fixed;
    z-index: 999999;
    width: 100%;
    height: 100%;
    text-align: center;
    top: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
}

.box img {
    max-width: 90%;
    max-height: 80%;
    margin-top: 2%;
    position: absolute;
    z-index: 99999;
}

.box:target {
    outline: none;
    display: block;
}

Thank you in advance for your help.

Answer №1

Check out this jsFiddle. The .Info div remains clickable because it's inside the anchor tag. Remember to close the anchor tag just before the .Info div. Set .Info to display:none and add this CSS rule:

.box:target + .Info {
    display: block;
}

Kudos to Joel for a CSS-only lightbox solution. Here is all the code in case the fiddle breaks:

HTML:

<a href="#resume">    
   <div class="seg1">
      <p> Resume </p>           
   </div>
</a>    
<a href="#" class="box" id="resume"></a>      
<div class="Info">
  <p>
    <label for="login">Your name</label>
      <input type="text" name="yourName" id="login" placeholder="" />
  </p>    
  <p>
    <label for="password">Email</label>
      <input type="text" name="yourEmail" id="subject" placeholder="[email protected]" />
  </p>    
  <p class="">
    <button type="submit" class="">submit</button>
  </p>    
  <p>
    <label for="password">Message</label>
      <input type="text" size="40" style="height:100px;" name="kommentar" class="message" id="kommentar" />
  </p>
</div>

CSS:

a:link, a:visited, a:active, a:hover{color:#88d8d8;}
a{text-decoration:none;}

.box {

    display: none;
    position:absolute;
    z-index:99;
    width: 100%;
    height: 100%;
    text-align: center;
    top: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
}

.box:target {
    outline: none;
    display: block;
}
.box:target + .Info {
    display: block;
}
.Info{
    display:none;
    height:400px;
    width:400px;
    position:relative;
    background:#fff;
    z-index:99999999;
    margin-top:calc(20% - 50px);
    margin-left:calc(50% - 250px);  
    border:2px solid #88d8d8;
}


.seg1 p:first-child{font-size:36px;padding:0;margin-left:60px;margin-top:95px;color:#88d8d8;text-transform:uppercase;}

/* Styles for the circle (.seg1) */
.seg1{
  -webkit-border-radius:400px;
  border:1px solid #b1ebeb;
  height:250px;
  width:250px;
  float:left;
  background-color:#f1fbfb;
}
.seg1:hover{
   animation-duration: 2s;
   animation-name: zoomin2;
   animation-fill-mode: forwards;
   -webkit-animation-duration: 2s;
   -webkit-animation-name: zoomin2;
   -webkit-animation-fill-mode: forwards;
   -webkit-transition: all 1s ease-in-out;
   -moz-transition: all 1s ease-in-out;
   -ms-transition: all 1s ease-in-out;
   -o-transition: all 1s ease-in-out;
   transition: all 1s ease-in-out;
   background-image:url(http://www.clickwallpapers.com/wp-content/uploads/2014/04/Background-1.jpg);   
   background-position: -30px center;
   background-size: cover;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered while submitting ajax request to server

When I click a button, a function is triggered with the argument insertSongPlay(newSong.songID);. After logging the value of newSong.songID, which is 90 as desired, an ajax call is made: function insertSongPlay(songID) { $.ajax ({ type: " ...

Using conditional statements in CSS with the "if else"

Is it possible to use an if-else statement in CSS? This is the part where I would like to change the text color: <?php echo $status; ?> There are two statuses: Pending & Delivered. Pending should display in red and Delivered in green. Could I imp ...

Fixed top Bootstrap navbar - featuring a logo on the left and navigation links on the right

Having trouble creating a responsive fixed navbar with logo and navlinks on both sides. I removed all Bootstrap elements from the code I'm sharing here, can someone help me out? I need a solution using Bootstrap to ensure everything is responsive. Th ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

Tips for aligning an element at the center based on the viewport rather than the parent element

I'm trying to center an element within a header with five elements, consisting of a button on the left, a logo in the middle, and three buttons on the right. I want the logo to be centered based on the viewport. I've created a codepen to demonstr ...

Error encountered when trying to show a modal using a PHP script

I'm encountering an issue with the modal system on my website. Let me share some code snippets: MODALS ARE BUILT WITH W3.CSS LIBRARY modal.js (including debug statements) let modal = null; let title = null; let content = null; $(document).ready(() = ...

Ways to insert text at the start and end of JSON data in order to convert it into JSONP format

Currently, I am working on a project where I need to add a prefix "bio(" and a suffix ")" to my JSON data in order to make it callable as JSONP manually. I have around 200 files that require this modification, which is why I am looking for a programmatic ...

Design a universal web service indicator akin to an "activity indicator" that works seamlessly across all web browsers

My web service is quite database-heavy (using PHP and mySQL), resulting in the user being faced with a blank screen for several seconds. When a user clicks a link on an HTML page, a javascript function is triggered, which then initiates an AJAX call to in ...

Ways to adjust the font size of mat-menu-item?

I came across a query regarding this matter on another platform: How can the font size of mat-menu-item be changed to small in Angular? Unfortunately, the solution provided did not work for me. I attempted to implement the suggested code in my Angular a ...

What are some ways I can efficiently load large background images on my website, either through lazy loading or pre

Just dipping my toes into the world of javascript. I'm currently tackling the challenge of lazy loading some large background images on my website. My goal is to have a "loading" gif displayed while the image is being loaded, similar to how it works ...

Swapping elements using jQuery

My PHP script generates a table with multiple rows of items. Each row displays different content, including an image of emptybox.jpg in the first column, and a link to select the item in the last column: output .= '<a href="./select_item.php id=se ...

Generating a JSon output that contains HTML table data specifically meant to be seen within the fnopen callback function of a jQuery datatable

After spending countless hours searching for a solution to my problem, I decided to take matters into my own hands. The issue at hand revolves around a jQuery datatable that showcases various aspects of a resolver's case load. Claim ID Claimant Nam ...

The use of numbers for identification

Currently, I am in the process of extracting data from a JSON array that has the structure as shown below "title": Title, "chapters": { "13c": { "words": 123, "spaces": 321 }, "15d": { "w ...

Floating unordered list items in a gallery display

I am dealing with a Gallery view where I am using the UL LI elements with float left to create a gallery-like layout with multiple lines based on the content. However, I am encountering an issue where after the first line, the layout seems to malfunction i ...

How to Use JQuery to Retrieve the Nearest TD Element's Text Content

Hey there, here is some HTML code that I need help with: <tbody> <tr> <td class="text-center"><input type="text" class="form-control cardSerialNumber"></td> <td class="text-center"><button type="button" onc ...

Struggling to Align NAV buttons to the Right in React Framework

My current Mobile Header view can be seen here: https://i.stack.imgur.com/CcspN.png I am looking to achieve a layout similar to this, https://i.stack.imgur.com/pH15p.png. So far, I have only been able to accomplish this by setting specific left margins, ...

The names in the lists appear with a visible scrollbar, making them easily

I'm having some issues with this dropdown menu. http://jsfiddle.net/vnr0rbuu/2/ The problem arises when using a different mouse or a PC, as the scrollbar appears and my list items don't apply the inline-block style correctly. Any suggestions on ...

Displaying JSON data based on a specific key

My current challenge involves dealing with a JSON string structured like this: {"cat1":"m1","cat2":["d1","d2","d3"],"cat3":["m1","m2","m3","m4"]} As part of my learning process in Javascript and AJAX, I am attempting to display the different values based ...

What are some ways to stop the form from refreshing while still successfully submitting the form data on the current

As my form continued to submit and refresh, I consulted a helpful resource on Stack Overflow titled How to prevent page from reloading after form submit - JQuery in search of a solution. Interestingly, the difference between the provided answer and my situ ...

What could be causing the flickering of the rotateY (flip) css3 animation in Chrome?

Check out my jsFiddle at this link: http://jsfiddle.net/Grezzo/JR2Lu/ If you press the i key on your keyboard, you'll see the picture flip around to reveal some text. However, there seems to be a flickering issue during the animation, especially with ...