What is the best way to enable this image lightbox to function seamlessly with both horizontal and vertical images?

Looking to create a responsive image lightbox? After starting from an example on W3Schools (https://www.w3schools.com/howto/howto_js_lightbox.asp), I ran into issues with portrait oriented images. Ideally, the solution should handle both landscape (e.g., 1200x800) and portrait (e.g., 800x1200) images.

The goal is to ensure that the images resize appropriately for different orientations and work seamlessly across modern browsers, iOS, Android, and even IE11 compatibility.

I attempted to address this by adding "max-width: 1200px;" to the lightbox-content class. While it worked for horizontal images, vertical images ended up exceeding the desired height due to enlargement.

<div class="lightbox">
    <span class="close" onclick="closeLightbox()">&times;</span>
    <div class="lightboxTitle">My Title</div>
    <div class="lightbox-content">
        <div class="slide"><img src="img1.jpg"></div>
        <div class="slide"><img src="img2.jpg"></div>
        <div class="slide"><img src="img2.jpg"></div>

        <!-- Next/previous controls -->
        <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
        <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>
</div>


/* The Modal (background) */
.lightbox {
    display: none;
    position: fixed;
    z-index: 99999999;
    padding-top: 60px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
}

/* Modal Content */
.lightbox-content {
    position: relative;
    margin: auto;
    padding: 0;
    width: 100%;
    max-width: 1200px;
}

.lightboxTitle {
    position: absolute;
    top: 20px;
    left: 25px;
    font-size: 20px;
}

/* The Close Button */
.close {
    color: white;
    position: absolute;
    top: 10px;
    right: 25px;
    font-size: 35px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #FF8511;
    text-decoration: none;
    cursor: pointer;
}

/* Hide the slides by default */
.slide {
    display: none;
}

.slide img {
    width: 100%;
}

/* Next & previous buttons */
.prev,
.next {
    cursor: pointer;
    position: absolute;
    top: 50%;
    width: auto;
    padding: 16px;
    margin-top: -50px;
    color: white;
    font-weight: bold;
    font-size: 20px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;
    user-select: none;
    -webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
    right: 0;
    border-radius: 3px 0 0 3px;
}

.prev {
    left: 0;
    border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
   .prev:hover,
   .next:hover {
   background-color: rgba(0, 0, 0, 0.8);
   color: #FF8511;
}

Answer №1

In a previous project, I encountered a similar issue which I managed to resolve by utilizing a JavaScript library called .

This library enables you to manipulate images after they have finished loading, allowing you to apply specific CSS classes based on their aspect ratio.

$('#container').imagesLoaded( function() {
  // images have loaded
  // check image height/width > 1, portrait
  // check image heidht/width <= 1, square or landscape
  // assign different classes for each case to handle
});

CSS:

.img-container {
  //customize container styles here

}

// Use these classes for the images
.img-container img.portrait {
  height: 100%;
  width: auto;
}

.img-container img.landscape {
  width: 100%;
  height: auto;
}

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

Tips for ensuring the final row remains fixed at the bottom of the table while maintaining a minimal body height:

I'm currently working on designing an invoice table using HTML and CSS. I have dynamic rows that are added to the table, and I want the body of the table to have a minimum height to fit around 5-6 rows, after which it should extend based on the number ...

Using the justify-content:space-between property does not seem to be effective in a Nextjs

In my Next.js app, I am facing an issue with the justify-content: space-between; property not working as expected for two divs inside a container. However, justify-content: center; works fine. Interestingly, when I use the same code in an index.html file, ...

What are the methods for managing HTML encoding with the python win32com package?

When using python, I employ win32com to convert word documents into HTML: from win32com import client as wc import os word = wc.Dispatch('Word.Application') doc = word.Documents.Open(wordFullName) doc.SaveAs(htmlFullName, 10) However, the ge ...

What is the best way to place <a> links next to each other in the header?

Looking to align the links horizontally in the header using CSS, can someone assist me with this? <body> <nav> <div class="wrapper"> <a href="index.php"><img src="spaceship.png" alt="blogs logo& ...

A pair of variables combined within a set of single quotation marks

After exploring numerous examples, I am still struggling to include a variable inside quotes. This situation seems unique and difficult to resolve. Take a look at the code snippet below: Var x='http://www.xyzftp/myservice/service1.svc' Var y=&a ...

Tips for ensuring the accurate retrieval of prop values by waiting for the value to be set

Encountering an issue with the .toUpperCase() method within one of my components, resulting in the following error: TypeError: Cannot read properties of undefined (reading 'toUpperCase') This problem seems to stem from a race condition in fetc ...

Utilizing Selenium to Click and Drag on the Browser Window in Order to Select Text on the Website

I am attempting to mimic the action of highlighting text on the screen by moving the cursor to a specific element and clicking and dragging. However, my current code does not seem to be replicating this behavior as expected. Despite defining the element, ...

Hovering over elements with the FireFox CSS transition 'transition-all' can sometimes lead to flickering and unexpected behavior

Can someone help me understand why I am experiencing a flickering effect on an element when using transition: all 0.5s ease-out; in FireFox? It's difficult to describe, but you can see it happening in this live example here: (take a look at the logo ...

Styling a component next to another using CSS

Struggling with a simple question here. I'm experimenting with HTML and CSS, trying to create a page with a central content box flanked by two arrow images on each side. While the concept isn't too difficult, I'm running into issues with usi ...

The color of the ellipsis value does not match properly

In my table, I have lines that end with 3 dots '...' when the text overflows. To achieve this, I used the ellipsis value of the text-overflow property. While this works well and the '...' are displayed when resizing the window, I encoun ...

transmit static information using a form

I'm currently working on sending data from one page of my website to another. It seems like using an HTML form with the action attribute set to the destination would be the way to go. <form method="POST" action="/destination"> <input ty ...

Ignored are the white spaces within a pre tag

From what I understand, the 'pre' tag is supposed to collapse all white spaces and tabs into one space. However, I have noticed that this doesn't happen for me. I'm curious as to why this may be the case. Could it possibly be dependent ...

Switch a div to a computed location

I'm having trouble getting this code to animate a div to a calculated position. Can someone please help me troubleshoot? $(document).ready(function() { var is_Clicked = false; $("#togglebutton").click(function() { if (is_Cli ...

Is it possible to simultaneously play several one-shot sound effects on mobile web browsers?

I'm attempting to trigger a sound effect when the user clicks a button on my web application. Initially, I experimented with this approach: var sound = new Audio("soundeffect.ogg"); function clickHandler(){ sound.play(); } Unfortunately, if the ...

Does Less undergo a compilation process in a single pass?

Does Less execute a single pass on the files, or does it perform multiple passes? I am particularly worried about what will happen if I include another file that redefines variables and mixins. Will the generated output use the original values, or will it ...

Concealing a form after submission using JavaScript

After submitting the form, I am attempting to hide it and display a loading GIF. I've experimented with various methods, including changing ClassName to Id, but haven't had success. This is for a school project, and I've spent a significant ...

Is there a way to store image URLs in a fashionable manner?

Hey there! I've been working on creating a HTML page that showcases multiple images. Everything is running smoothly on my localhost, but when I try to access it online, the images take forever to load. Any suggestions on how I can cache image URLs in ...

What is the best way to display an alert when the button is clicked repeatedly?

Is it possible to keep displaying the alert every time we click the button, rather than just once after clicking it? I currently have an alert set to trigger when a button is clicked, but it disappears after 3 seconds. How can I make it show up again with ...

What could be causing my function to fail <object>?

Within index.php, I am calling the function twice, which includes chart.html. index.php chart_line($valuesNight); //first call chart_line($valuesEvening); //second call ?> <?php function chart_line($jsonDataSource){ ?> < ...

Is there a way to use jQuery to load a ul from a different webpage into a div?

Progress is being made as I work on making two nearly identical pages function seamlessly together. One page features an alphabet list, and when a letter is selected, the other page filters results (last names from a PHP database query) and displays them o ...