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>

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

Display the contents of a <div> tag from one HTML file onto another HTML file

Recently I embarked on learning HTML and came across a peculiar doubt. My goal is to create a section div on the first page that changes dynamically based on the menu item clicked, rather than redirecting to another HTML page. I've experimented with s ...

employ the value of one array in a different array

Currently, I am working with an array (const second) that is being used in a select element to display numbers as dropdown options {option.target}. My question is: Is there a way to check within this map (that I'm using) if the 'target' from ...

What is the best way to align two divs next to each other while keeping the second one centered with flexbox?

Here are my two div elements: <div class='container'> <div class='left'></div> <div class='centered'></div> </div> I am looking to center the second inner div and have the first inner ...

"Encountering an issue when linking the file with phpMyAdmin

I've been struggling with this issue for hours now. I'm currently working on a registration form for my website and while coding in PHP, I connected it to MySQL and the Database using this line of code (which happens to be the 6th line): $mysq ...

Troubling inconsistency in jQuery's .css function performance

I'm facing a problem with the jquery .css function. I am using it to retrieve the actual height of elements that have their height set to auto. The code I am currently using is as follows: $(this).css({ height: $(this).css("height"), width: $(this).c ...

When using Vue with CSS3, placing an absolute positioned element within a relative wrapper can cause issues with maintaining the

Just starting out with Vue and diving into the world of CSS3! I'm currently working on a component that you can check out here: https://codesandbox.io/s/yjp674ppxj In essence, I have a ul element with relative positioning, followed by a series of di ...

I have been attempting to implement validation in JQuery, but it doesn't seem to be functioning correctly

Can someone assist me with adding validation in jQuery? I'm stuck and need help solving this problem. $(function(){ $("#btn").click(function(){ var b=prompt("Enter your link"); $("a").attr("href",b); if($("b").v ...

CSS: Adjusting the vertical alignment of numbers in an ordered list

Hey there, I have a question about the vertical alignment of numbering in an ordered list. It seems that the numbers are being pushed to the baseline when <li> elements contain floated divs only. Is there any way to prevent this and have the numbers ...

The "Overall Quantity" of items will vary as it goes through different numerical values, despite the fact that I employed --

I am currently working on an e-commerce website with a shopping cart feature. The cart displays the number of items added to it, which increases by one when 'Add to Cart' is clicked and decreases by one when 'Remove' is clicked. However ...

Child div height exceeds parent container bounds

Take a look at this example here: http://fiddle.jshell.net/dwuZp/15/ Can you determine why the "icons" div is overflowing the height of its parent div, "logoTimeAndIcons"? To visualize the issue, try changing the height to 50%. Your assistance is great ...

Utilize CSS to ensure that the hover effect of the main navigation remains active even when the mouse hovers over the sub-menu

Is there a way to make the hover state in navigation items persist when the mouse hovers over a sub-menu? Currently, when hovering over a top-level nav item, the link color changes to white and background turns black, but this reverts back once the mouse m ...

Is there a way to display two cards side by side in mobile view?

On a desktop view, 2 cards are displayed in a row. I would like them to also appear in a row on mobile view. I tried using col-sm-4 but it's not working. How can I get the cards to display in a row on mobile view so that I can see both the product pho ...

Tips for creating a margin on an HTML button's background color

My bootstrap button has dimensions of 46 x 96px and a border radius of 22px. When hovered, I would like the background color to only extend up to 40 x 90px, without filling the entire button. I understand this can be achieved using a box-shadow, but since ...

Is there a way for me to keep an image stationary on the page while allowing overlaying text to move?

Currently, I am exploring the process of coding a website that mimics the style of this particular webpage: . I am particularly interested in learning how to create a scrolling effect for text within a fixed area, while keeping the accompanying images st ...

Applying CSS styles to a shadow DOM element will not produce the desired visual

I'm encountering an issue while attempting to apply CSS to an element within a shadow-root by adding a class to it. In my Angular component, I have the following code: CSS .test { border: 1px solid red; } TS document.getElementById('my-div&a ...

Finding an element that lacks both a class and an id, and is not consistently present - what's the trick?

Currently, I am faced with a predicament in my code where a <li> element only appears under specific conditions, making it difficult to apply positioning. This element lacks an id and class attribute, which prevents me from targeting it accurately us ...

Is there a way to determine the location where my website is fetching CSS and media files from?

After implementing Wagtail on my website, I encountered an issue with the CSS and images not being found when the site was put into production. I have already tried running python manage.py collectstatic and ensured that all of my CSS files are located in ...

What is the best way to add animation to my `<div>` elements when my website is first loaded?

I am looking for a way to enhance the appearance of my <div> contents when my website loads. They should gradually appear one after the other as the website loads. Additionally, the background image should load slowly due to being filtered by the wea ...

Building a dynamic tab menu using JavaScript: A step-by-step guide

In order to generate dynamic tab menus with JavaScript, I am seeking a solution that does not rely on jQuery as it may not be supported by all mobile devices. Any advice for replicating the same functionality using pure JavaScript would be greatly apprec ...

Having difficulty adjusting the width of the CSS menu bar to fit the entire page

I found a useful CSS menu tutorial online that seems to work well, but unfortunately, it doesn't stretch the full width of the page. You can check out the tutorial here: Even after trying to set the width to 100%, the menu still falls short of extend ...