Adjust picture to fit in div and align vertically

JSFiddle

In my fluid layout, I have a div with a required aspect ratio of 1:1. This div can contain text, an image, or both.

The challenge is to vertically align the text and ensure the image fits within the div without exceeding its boundaries or losing its aspect ratio.

Although I have successfully achieved most of this, I'm struggling with keeping the image aligned and proportionate to the div size simultaneously.

Here's how I maintain the 1:1 ratio of the div:

#upload-drop{
    background-color: #e6e6e6;
    position: relative; 
}

#upload-drop:before{
    content: "";
    display: block;
    padding-top: 100%;
}

#upload-drop >div{
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}

To vertically align content, I use the following CSS:

.valign-center{
    text-align: center;
    white-space: nowrap;
}
.valign-center:before {
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    margin-right: -0.25em;
}
.valign-center-content{
    display: inline-block;
    vertical-align: middle;
}

For images, I apply the following styles to maintain their aspect ratio and fit inside the parent div:

#upload-img {
    width: 100%;
    height: 100%;
}    
img{  
    width:auto;
    height:auto;
    max-width: 100%;
    max-height: 100%;
}

If I remove width and height on #upload-img, everything aligns perfectly, but portrait images may no longer fit the div properly.

How can I ensure both alignment and proper fitting for all types of content?

Answer №1

After implementing these adjustments, your sample images now adjust smoothly to different screen widths. However, I am unsure how they will handle extremely tall portrait images.

If the width is not restricted to 80%, the images would remain centered but might exceed the boundaries of their container.

#user-uploaded-img{
    width: 100%;
    max-height: 100%;
}    
img{  
    width:auto;
    height:auto;
    max-width: 80%;
    max-height: 100%;
}

Answer №2

Don't forget to include the following styles when using the image tag:

style="width:100%;height:100%" 

Take a look at this JSFiddle example

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

Design inspired by dot matrix patterns

Is it possible to create a background HTML page filled with a dot matrix designing tool? Can anyone provide guidance on how to achieve this? If I need to use a background-image for this, where can I find the appropriate picture? Thank you to everyone in ...

Troubleshooting techniques for resolving CSS issues with the video.js package in ReactJS

When using video.js in react to build a video player, I encountered an issue with the npm package not providing its inbuilt CSS. How can I add the inbuilt CSS of video.js? Instead of the control bar, I am getting something different than what is shown in t ...

Is there a potential white-space issue with jQuery CSS in Internet Explorer versions 7 and 8

Recently, we encountered an unusual issue with jQuery and CSS while working on compatibility with IE7 and IE8. Our project involves animations and height measurements, and during the animations, we wanted to prevent text from wrapping. To achieve this, we ...

The menu bar created from getJSON is not displaying the jQuery UI CSS styles as expected

I have been working on creating a jQueryUI menubar using JSON data. The JSON data is successfully being converted into valid HTML, but the menubar does not display with the desired styling. It appears as an unstyled unordered list rather than a styled menu ...

"Discover the step-by-step process for displaying the menu on a WordPress

After installing the new classifieds theme on my Wordpress website, I noticed that the menu from the previous theme is still showing up. However, when I hover over the menu, it disappears and only reappears once my cursor is no longer hovering over it. ...

Is there a term similar to "Rise above the Rest" in the world of Web Development?

Hey, I've encountered a new issue right now. Currently, I have two elements that are fixed to the top and bottom of the page. However, the elements in between them are overlapping the top element. Even though I tried keeping both elements fixed, th ...

The current code lacks any form of line breaks

While attempting to edit a CSS file in Sublime Text 2, I noticed that all the code is displayed without any line breaks. This makes it difficult to read and work with. Is there a way to fix this issue and format the code into readable sections? ...

Issue with KeyboardDatePicker in Material-UI/Pickers occurs when InputAdornmentProps property with position start is added, leading to an unexpected margin

I am currently working with a component from material-ui/pickers and this is the code snippet: <KeyboardDatePicker value={selectedDate} onChange={(_, newValue) => handleClick(newValue)} labelFunc={renderLabel} disableToolbar variant=&a ...

What is the best way to create a container filled with numerical figures?

If I have the number 445758, and I want each of these numbers to be displayed in their own box like this: Can you explain how to achieve this? ...

What is the best way to utilize the sx prop in Material UI for applying styles specifically when the component is active or selected?

In the code snippet below, I have set up the useState hook so that when a ListItem is clicked on, the selected state becomes true. My goal is to apply specific styling when an item is selected using the sx prop instead of creating a styled component for su ...

What is causing the floated element to overlap the element below instead of vice versa?

div { background-color: #00FFFF; } p { width: 50px; height: 50px; border: 1px solid black; margin: 0px; } #p1 { background-color: red; float: left; } #p2 { background-color: green; } #p3 { background-color: orange; } #p4 { backgr ...

Modern UI Styling for ASP.NET MVC with Metro Design

I am interested in incorporating the METRO UI CSS into my application: Begin by creating an ASP NET MVC Project To download METRO UI MVC, follow these steps: Package Manager Console: PM> Install-Package Metro.UI.CSS Adjust the bundles to incl ...

Prestashop 1.7 - Enhanced top menu navigation with drop-down subcategories

While using the ps_mainmenu top menu with the default Prestashop theme, I noticed that the drop-down menu only works for parent categories and not subcategories. You can see this in the attached image and HTML code from Chrome. I'm looking for a way t ...

Having trouble with a dropdown menu that allows for multi-select options?

var expanded = false; function showCheckboxes() { var checkboxes = document.getElementById("checkboxes"); if (!expanded) { checkboxes.style.display = "block"; expanded = true; } else { checkboxes.style.display = "none"; expanded = fa ...

Modifying a single element within a class with Jquery

Is it possible to create a stack of pages on a website using JQuery? I found this image that shows what I'm trying to achieve: image. Instead of applying ID css for each page, I'd like to use JQuery. While researching similar questions, I came ac ...

What is the best way to align headings directly above their own bottom border?

Is there a way to make a heading align perfectly with its bottom border? Can negative padding achieve this effect, or should I use underline instead? Perhaps positioning it at the top border of the element below is an option. Thank you! h1 { font: bol ...

"Utilize jQuery's append method to dynamically add elements to the DOM and ensure

I am currently facing an issue with my AJAX function that appends HTML to a page using the jQuery append method. The HTML being appended is quite large and contains cells with colors set by CSS within the same document. The problem I'm encountering i ...

Assign a class to a button created dynamically using Angular

While working on my project, I encountered an issue where the CSS style was not being applied to a button that I created and assigned a class to in the component.ts file. Specifically, the font color of the button was not changing as expected. Here is the ...

Creating personalized buttons with Bootstrap

I'm looking to create custom buttons using Bootstrap. The "Get Quote" button should have a magnifying glass symbol on its left, and the "Clear" button should have a symbol on its left as well, like shown in the image below. Additionally, I want to cha ...

Trouble with buttons in table cells

There is a problem with a button placed in a table cell that spans two rows and does not fill the second row. How can I ensure that this button fills both rows? I have attempted to adjust the height in the button's style as well as in the table cell i ...