Issue with CSS: Parent container disregards child element's width limit

Trying to maintain a 1:1 aspect ratio for a div by using an image. The goal is for the div to fill the available height within a flex-container while the container's width adjusts to fit its content.

The square-div displays with correct dimensions, but the parent div does not respect its width.

Although setting a fixed width for the image resolves the parent div width issue, it is not feasible as the image needs to resize dynamically. Any suggestions?

.parent {
  align-items: center;
  background-color: black;
  border-radius: 12px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: flex-start;
  overflow: hidden;
  padding: 16px;
  position: relative;
  width: fit-content;
  z-index: 1;
}

.header,
.footer {
  height: 48px;
  width: 100%;
}

.square {
  box-sizing: border-box;
  flex-grow: 1;
  display: block;
  position: relative;
  width: auto;
}

.square>img {
  display: block;
  height: 100%;
  width: auto;
}
<div class="parent">
  <div class="header"></div>
  <div class="square">
    <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" width="100" height="100" />
  </div>
  <div class="footer"></div>
</div>

https://i.sstatic.net/L7pL9.jpg

Other attempted solutions:
Setting the parent div to width: auto yielded no success.
Setting the parent div to display: inline-flex had no effect.
Setting the parent div to display: table did not resolve the issue.

Answer №1

It seems like you are looking for a layout that adjusts based on content size, which can easily be achieved with the use of grid.

I noticed that you had set both the width and height of the image to 100, which I have now removed. I used your image in this example just for convenience, so please focus on the overall layout instead. I made some color changes and added a border to the image to highlight different sections for demonstration purposes. The CSS below the .parent { is primarily for illustration purposes except for the image settings.

To center the element content, I applied the following CSS:

display: grid;
place-items: center;

body {
box-sizing: border-box;
font-size: 16px;
margin: 0;
padding: 0;
}
.parent {
height: 100vh;
background-color: #00000040;
border-radius: 0.75em;
box-sizing: border-box;
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-columns: 100vw;
padding: 1em;
}
.header,
.square,
.footer {
display: grid;
place-items: center;
}
.header {
background-color: #00ff0020;
}
.footer {
background-color: #0000ff20;
}
.square {
background-color: #ff000020;
overflow:auto;
}
.square>img {
border: solid 1px magenta;
height:100px;
width:100px;
}
<div class="parent">
<div class="header">test header</div>
<div class="square">
<img src="https://i.sstatic.net/L7pL9.jpg" />
</div>
<div class="footer">footer<div>MORE test text</div></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

What is the best strategy for positioning an anchor element within a list item using JSX or Bootstrap in a way that maximizes efficiency?

I have the following code snippet <li className = "list-group-item list-group-item-success" key ={todo.id}>{todo.text}<a style="float:right;" onClick={() => props.onEdit(todo)} className="edit-todo glyphicon glyphicon-edit" href="#"& ...

Display PHP output within a styled CSS box

<html> <head> </head> <style type="text/css"> .yellow-box { position:absolute; top:100px; left:500px; width:300px; height:300px; background:yellow } </style> <div class = "yellow-box" > </div ...

Prevent border duplication in CSS and retain border visibility

Check out this fiddle where I have three divs with a border-width of 2px. Originally, the issue was that each div's border gets duplicated in between, resulting in a total border-width of 4px. To solve this, I applied a margin-top of -2px to each div, ...

When the browser size shrinks, turn off the drop-down navigation menu

I'm currently working on a responsive website, and I have encountered an issue with a dropdown menu. When the browser size is reduced to 900px, the menu shifts all the way to the left side. To address this, I added some left padding to keep it off the ...

instructions for creating a hover effect where one div vanishes when hovering over another div

Is there a way to make the line visible when hovering over my circular div? #line { display: none } <div id='circle'> <div id= 'line'> ...

Guide to configuring an Angular Material Footer using Flex-Layout

Could someone help me with setting up the footer in my Angular Material app? I want it to: stick to the bottom when the content height is smaller than the view-port move down or get pushed down when the content height exceeds the view-port One important ...

"Struggling to make the 'overflow: hidden' property work for an absolutely positioned

I'm struggling to conceal an absolutely positioned image within a CSS grid layout. Below is the code snippet: HTML: <div class="relative-parent"> <div v-for="item in 12" class="hiding-parent"> <div c ...

What could be causing my div to display a horizontal scrollbar in Firefox?

Here's a straightforward test scenario: <html> <body> <div style="border:2px solid black; overflow: auto;"> x </div> </body> </html> Upon rendering, a horizontal scrollbar appears! I was using F ...

A guide on altering the color of a badge through programming

I am curious to learn how I can dynamically change the color of a badge in Angular. My goal is to initially set the color of the badge to white, and then if the percVLRiskTotal reaches a specific value, change the color to green as an example. CSS: <sp ...

Problem with Angular Slider

I'm in the process of creating a carousel component in Angular, but I'm facing an issue where the carousel is not appearing as it should. Below is the code for my carousel component. carousel.component.html: <div class="carousel"> ...

Discovering the center of an element and implementing a left float

I'm looking for a way to dynamically position the element #ucp_arrow in the middle of a div using float: left. Here is an illustration: https://i.stack.imgur.com/nzvgb.png Currently, I have hard-coded it like this: JAVASCRIPT: $("#a_account").cli ...

When I apply position: absolute; to my navigation bar, the anchor tags and links do not seem to function properly, especially with a video playing in the background

How can I make my anchor tags/links work properly on a header with a video in the "background" and a menu on top? HTML <nav class="menu"> <a href="#" target="_blank">ABOUT</a> <a href="#" target="_blank">PRICES</a&g ...

Designing a navigation system that revolves around a central logo image

I have created a navigation bar that you can see in this image: https://i.stack.imgur.com/eT4TL.jpg Using foundation to construct a website, I have designed the nav bar in the following manner: HTML: <nav class="top-bar"> <ul> ...

What is the best way to incorporate text animation into a website using Cascading Style Sheets (CSS)?

Is there a way to make my text flash twice per second? I attempted to create animated text, but it didn't work out! ...

Unable to utilize the rupee font within a select element on Chrome and IE8 browsers

Issues with displaying rupee font in select element on Chrome and IE8 I have implemented WebRupee for showing the Indian currency symbol. The font appears correctly when used within a <p> tag, but when placed inside a select option element, it only ...

Personalized Owl Carousel - OWL.JS Hover Pause Feature

I have been working on a slider in Codepen that is functioning well, and now I am trying to tweak it for my website. My goal is to make the carousel pause when someone hovers over it. I've experimented with all the options available on the owl slider ...

Looking to display several charts on a single page with varying datasets?

I have successfully integrated a doughnut chart plugin into my website. However, I would like to display multiple charts on the same page with different data sets. Below is the code snippet for the current chart being used: This is the chart I am using & ...

What is causing the div to expand even though css max-width is applied?

I am currently facing an issue with the navigation on mobile view while using Bootstrap 4. Specifically, the .nav .dropdown-menu and .text-truncate classes are not displaying ellipsis when the content text length is too long. I have attempted to resolve t ...

Add space between the first and second rows in the grid gallery display

.gallery{ display: grid; grid-template-columns: repeat( auto-fit, minmax(250px, 1fr) ); grid-template-rows: repeat( auto-fit, minmax(250px, 1fr) ); } view grid image here Could you assist me with identifying the spacing issue between the first and second ...

Is there a way to modify the color scheme of the hamburger icon

I'm experiencing difficulties changing the color of a hamburger icon. I've searched for the specific code responsible for the color change, but I haven't been able to locate it. Ideally, I want the color to be white, but on small screens, i ...