If the image's height is less than the parent's height, center it vertically. Otherwise, restrict the height to 100

Currently, I am utilizing html5-fullscreen mode along with a full-screen parent div. My goal is to vertically center an image within this parent div, but only if the image is smaller than the parent div (i.e., the screen size). Additionally, I aim to restrict the image's max-height to 100% of the parent to prevent zooming in if the image exceeds the screen dimensions.

I have managed to center the image using two divs—one with display:table, and the other with display:table-cell. However, I encountered difficulty when trying to simultaneously set the image's max-height not to exceed that of the parent div. While I can achieve the latter by using

position:absolute; max-height:100%;
, it disrupts the centering on smaller images.

Here is an example of the markup:

<div id="fullscreen-container">
    <div id="fullscreen-img-wrapper">
        <img id="fullscreen-img" src="/images/test-6000x4000.png" />
    </div>
</div>

If you have any suggestions on how to tackle this issue, please feel free to share! Your input is highly valued.

Answer №1

<style>
#fullscreen-img-wrapper { display: table-cell;
    margin-left: auto;
    margin-right: auto;
    vertical-align: middle;
    width: 100%; }
#fullscreen-img { width:100%; }
</style>

<div id="fullscreen-container">
    <div id="fullscreen-img-wrapper">
        <img id="fullscreen-img" src="/images/test-6000x4000.png" />
    </div>
</div>

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

Child div within a parent div with absolute positioning does not extend to the full height of the webpage

I am currently facing an issue with a bug in my project that I am working on. The mobile menu is not taking the full height of the viewport when the screen size is less than 768px, even though it has been set to 100%. It only becomes visible when toggled ...

Troubleshooting Problem with CSS and Javascript Dropdown Menu

Greetings, fellow web designers! I am relatively new to the world of web design and currently, I am immersed in the process of creating a website. My current project involves implementing a dropdown menu using CSS and Javascript. While I have made signific ...

Create a ng-repeat directive to loop through images in a carousel

I have a functional carousel example in AngularJS <div id="carouselExampleControls" class="carousel slide" data-ride="carousel"> <div class="carousel-inner container"> <div class="carousel-item active"> ...

A div housing a jQuery progress bar within an unordered list (ul)

Having an issue incorporating a jQuery progress bar into a ul li tag. <ul> <li> <a href="test">test</a> <div id="progressbar"></div> </li> </ul> I have set display: block for the anchor tag and ...

How can I align the middle of the element within its parent until it hits the top-left edge, at which point should I stop center

I am trying to center an element within a parent container. It seems simple with methods like transform, flexbox, and grid... The issue arises with overflow behavior. When the parent container shrinks below the dimensions of the child element, scrollbars a ...

Add a new row directly beneath the row that has been selected using JQuery

I am currently working on a code snippet where I need to add functionality to delete and add rows for each individual row. Specifically, I want the ability to add a new row directly below the selected row. Does anyone have any insights on how this can be ...

Steps for importing an image into a phpMyAdmin database and associating it with a particular username

My goal is to upload images to a phpmyadmin database and associate them with specific usernames. I then want to retrieve these images and display them on an HTML page using jQuery, AJAX, PHP, and JavaScript. As a beginner, I am looking for the simplest way ...

Create dynamic Bootstrap 4 menus featuring three levels of navigation - the initial dropdown sub-menu may not display any selectable options

Using Bootstrap 4 and jQuery 2.2.4, I've created a navbar with three layers of menus, comprising two levels of drop-downs from the top-most level. Everything works smoothly except for a glitch on mobile devices. The menu collapses to the hamburger ic ...

Refreshing the AngularJS application by re-rendering the view without the need for a full reload

I am currently working on an application that heavily utilizes the ng-include directive, and one thing I cannot stand is having to reload the entire application just to see a simple HTML update. I have experimented with manually replaying the XHR in the N ...

Comparison between Mobile Phone's innerWidth and innerHeight Versus Resolution

My test phone, the Galaxy S3 Mini, has a resolution of 800x480 according to its specs. Strangely, when I run the following code in my HTML game: window.innerWidth window.innerHeight The width appears as 533 and the height as 295. I recently discovered ...

another option besides the nextElementSibling

I have a unique code that applies a style to the following div when another div is clicked, using nextElementSibling. var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("clic ...

Having trouble centering the links in the Bootstrap navbar?

I've been struggling to center the links/tabs on my Bootstrap navbar for days now and I just can't seem to get it right. I've gone through countless similar questions without any luck. Something must be off in my code or maybe I'm not t ...

Sending a variety of data values via the AJAX data parameter

I have successfully passed data from a form using a standard AJAX call. Now, my goal is to include two additional variables called elat and elng in this process. What would be the most effective way to incorporate the values of filter.serialize(), elat, a ...

Angular 10 Reactive Form - Controlling character limit in user input field

I'm currently developing an Angular 10 reactive form and I am looking for a way to restrict the maximum number of characters that a user can input into a specific field. Using the maxLength Validator doesn't prevent users from entering more chara ...

What could be causing the issue with the alignment of text to the right in Bootstrap 4

Is there a way to align the text in the blue column div to the right like the red div? I've tried using text-right but it doesn't seem to work. Also, how can I maintain the spacing between letters like this: [I] | dashboard? https://i.sstatic.ne ...

JavaScript problem, adds 'undefined' to page despite receiving response from the network

I am facing an issue with the values received from an ajax call that are attempting to append to the page before being assigned. Whenever I call sendRequest below, I receive a price response from the network each time, but occasionally, when I console.log ...

Populating JQuery autocomplete using a PHP array

As a beginner in the world of JavaScript and JQuery, I have been scouring the internet for hours trying to find a solution to my query. My goal is to populate a JQuery autocomplete feature using an array that I have created in PHP. Here is a glimpse of the ...

What are the best ways to optimize bootstrap col-sm-* for mobile usage?

Currently, my project is responsive up to col-sm. I am looking for a way to ensure that it renders perfectly on mobile devices without having to add col-xs-* throughout the entire project. Is there a method using jquery/scss to make col-sm behave like col- ...

Having trouble with element.scrollTo not functioning properly on mobile devices?

I have been working on creating a vertical scrolling carousel, and so far everything seems to be functioning well. I can scroll horizontally using buttons and swipe gestures successfully on the simulator. However, when I view the website on a real mobile d ...

What is the best way to arrange 4 divs with 2 on each line?

Consider the divs provided below: <body> <div id="f1">{{ f1|safe }}</div> <div id="f2">{{ f2|safe }}</div> <div id="f3">{{ f3|safe }}</div> <div id="f4"> ...