What is the best way to align a div that does not have a specified width in the

Check out my HTML code:

<!DOCTYPE html>
<html lang="en">
    <head><link href="test.css" rel="stylesheet"></head>
    <body>
        <div class="outer">
            <div class="inner">
                <img src="test.png">
            </div>
        </div>
    </body>
</html>

Here's the CSS I'm using:

.outer {
    position: fixed;
    top: 0; right: 0; bottom: 0; left: 0;
}

.inner {
    width: auto;
    margin: auto;
}

.inner > img {
    width: auto;
    max-width: 100%;
}

This snippet is a simplified version of the code I'm implementing within Bootstrap's modal.js to showcase images after clicking on thumbnails.

I want the image to be centered on the viewport in a responsive way - showing its true dimensions unless the viewport is smaller than either dimension, at which point it should resize accordingly.

The outer div usually matches the dimensions of the viewport, which is effective.

Although the image has width: auto; max-width: 100%; for responsiveness to the viewport width, it doesn't adjust to the height. Including max-height: 100%; doesn't have an effect.

Currently, the inner div aligns with the left of the viewport unless provided with a specific width.

Adding display: table to the .inner centers the div, but inhibits responsiveness to changes in viewport width.

So, how can I center the inner div while making it responsive to both width and height of the viewport without cropping or distorting the aspect ratio of the image?

Is achieving this behavior solely through CSS, HTML, and Bootstrap impossible, or would it necessitate custom JS? I lack sufficient knowledge in JS to confidently code this myself, thus hoping there's a clever CSS trick that accommodates these conflicting requirements based on viewport size.

Tagging Twitter's Bootstrap because I intend to utilize it in conjunction with Bootstrap's JS plugins for any potential solutions.

EDIT: Just to clarify, I need to center the inner div as a whole, not just the image within it.

Answer №1

modify the code in this manner

.container {
        position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
        text-align:center;
    }

http://jsbin.com/puyabore/1/edit

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

Unexpected activation of Internet Explorer media queries causing display issues

Check out my CSS code below: @media all and (max-width: 500px){ .calendar_head{ height: 120px; line-height: 60px; } } .calendar_head{ width: 100%; font-weight: 700; color: #6a7783; text-align: center; line-heigh ...

What are the steps to create a responsive Coin Slider?

Once the slider is generated, it appears that there is no built-in method to resize it, causing issues with responsive design. Is there a way to adjust the size of the Coin Slider plugin based on the media queries in Twitter Bootstrap 3? Take a look at C ...

@page Css not displaying properly on the Mozilla Firefox browser

In order to fulfill my requirement, I need to ensure that there is a consistent 10cm margin on all pages when printing. Printing on my web page involves utilizing the window.print() function. However, due to the dynamic nature of the content, the number o ...

The CSS transition is failing to revert to its original state after being combined with animations

My goal is to create a loading animation using CSS transitions and animations. I have managed to achieve the initial loading effect by scaling with transition and then animating it to scale out on the x-axis. However, when attempting to transition back to ...

Is there a way to ensure that Jade automatically updates the HTML template when the linked CSS file is modified, using livereload and Grunt?

Currently, I am in the process of setting up Grunt to ensure that whenever a linked .css file undergoes a change, the jade template which is linked to the .css file also automatically reloads. However, I am facing challenges with compiling my jade template ...

Margin isn't functioning properly

My section is not centered and adding margin-left doesn't seem to be working. Any suggestions on how I can solve this? Take a look at how it appears. Here's the code: .margins { margin-left: 100px; } <link href="https://cdn.jsdelivr.net ...

The annoying Facebook "add a comment" popup refuses to close

Occasionally, the "add a comment" popup (iframe) in Facebook's Like plug-in fails to close and obstructs access to the content underneath. This issue has been noted while using Chrome 21 and Firefox 15. To replicate this problem, you can visit the fo ...

Stop unwanted scrolling upwards by using jQuery ajax function $.load

I'm currently in the process of creating an ajax chat using jquery and php. Everything seems to be running smoothly except for one issue related to scrolling. Essentially, I've implemented a time-out function that automatically reloads the inner ...

Webpack2 now transforms sass/scss files into JavaScript rather than CSS during compilation

I've created a webpack script to compile all .scss files into one css file. I decided to use webpack instead of gulp or grunt for simplicity, as it can be configured in a single file. However, I am encountering an issue where scss files are being com ...

Creating a dropdown navigation menu using jQuery

I have been experimenting with creating a customized Drop Down menu using ul li and Jquery, following this helpful Tutorial. Here is the sample HTML Code for testing: <div id="dd" class="wrapper-dropdown-3" tabindex="1"> <span>OS</span> ...

What has become of this v-for command?

I had the idea to create an object like this: So, I started with the following code: <div class="p-2 border-2 border-blue-300 mr-auto rounded-full space-x-1"> <button class="text-white w-7 border-2 border-BgLightBlue rounded-fu ...

I must break free! Angular's $sce is failing to manage links to audio files (Strict Contextual Escaping)

I have successfully implemented Angular and $sce in my project to handle HTML special characters and link video files in the database to a video player. However, I am facing issues connecting the HTML audio player to audio files stored in the same database ...

When trying to integrate Braintree with Django, an error occurred stating "AttributeError: 'Configuration' object has no attribute 'environment&#

Currently, I am delving into Django using the book "Django 3 by example" by Antonio Mele. I encountered an issue with setting up a sandbox environment while working with Braintree. According to their documentation: gateway = braintree.BraintreeGateway( b ...

Ways to dynamically activate an anchor tag's click event using JavaScript?

I'm having trouble triggering an already written click event for an anchor tag using JavaScript. Can someone help me figure out how to do this? I have the following anchor tag: <a id="memberid">Data Member</a> <script> $("#memb ...

Inquiring about how to make the pieces move on the checker boards I created

I'm having trouble getting my SVG pieces to move on the checkerboard. Can someone please help me figure out how to make them move, even if it's not a valid move? I just want to see them in motion! The important thing is that the pieces stay withi ...

What causes the final tab to be highlighted when the page is loading?

Every time I visit the page, the last tab is always active instead of the first one, which is causing confusion. I can't figure out why this keeps happening. It seems like when i=0, the first tab should be active, but for some reason, it always defaul ...

What is the best way to reduce the size of a Bootstrap card image back to its original dimensions when

I am currently experimenting with a bootstrap card using HTML, CSS, and Bootstrap. My main focus right now is on how to shrink the image when hovering over it. The .card-header-img has a fixed height of 150px that I do not want to modify. What I want to a ...

Retrieve various Hyperlinks from database for presentation within a gridview

My objective is to include multiple Hyperlinks in a single cell of a gridview, separated by commas. The approach I have considered is to generate the HTML code during data retrieval. I have devised a simple query that creates a series of Hyperlinks based ...

How can I prevent Bootstrap from conflicting with my custom styles?

My HTML menu was functioning perfectly with specific CSS styles, until I decided to add a Bootstrap reference in the header: New Link Added <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> Afte ...

Using angular's ngShow along with animate.css for dynamic animations

I am having trouble animating the display and concealment of an element using both animate.css and angular. Despite reading through this question on Stack Overflow and going over the angular documentation for ngShow and ngAnimate, I am still unable to ach ...