I have been searching for an answer to this specific question but haven't found one yet.
My goal is to create a simple layout like this:
-------------------
header
-------------------
|
content | graphic
|
-------------------
Here are the requirements:
- The header height is determined by the font size;
- The content has a fixed minimum width;
- The graphic should be as large as possible while maintaining aspect ratio, and its height will not exceed the screen's height minus the header's height unless it would result in inadequate width for the content panel;
- The content should expand horizontally to fill any remaining space after accommodating the image and respecting the previous constraints.
[edited to add:]
- Vertically: The image is vertically centered if its height is less than the maximum allowed height based on the width and aspect ratio constraints;
- Horizontally: The image aligns with the right side of the screen, and the content extends up to the left edge of the image, excluding manual padding.
I have attempted using flexboxes, achieving success with the first three requirements. However, I struggle to make the content pane grow horizontally without causing the image to take up all available space, resulting in undesired alignment. The HTML and CSS code below represent my current approach:
https://i.sstatic.net/4nBXS.png
You can view and test the code here: https://jsfiddle.net/uv566jc3/:
.grid {
border: solid 1px #e7e7e7;
height: 95vh;
display: flex;
flex-direction: column;
}
.header {
flex: 0;
}
.grid__row {
flex: 1;
display: flex;
flex-direction: row;
}
.grid__item {
flex: 1;
padding: 12px;
border: solid 1px #e7e7e7;
}
img {
flex: 1;
object-fit: contain;
overflow: hidden;
border: solid 1px #e7e7e7;
}
<div class="grid">
<div class="header">Some header stuff
</div>
<div class="grid__row">
<div class="grid__item">1</div>
<img id="pic" src="https://s27.postimg.org/oc7sozu7n/clouds.png">
</div>
</div>
- The grid structure uses flexbox in a column direction containing header and grid__row elements;
- The header has a flexible height (flex 0) depending on its content;
- The grid__row fills the remaining height (flex 1) after accounting for the header and contains grid__item along with the image;
- The grid__item expands horizontally to fill available width (flex 1);
- The img element utilizes object-fit = contain for desired sizing properties and overflow=hidden for unknown effects.
No explicit min-width is set on the grid__item in the jsfiddle, but that shouldn't impact the outcome significantly.
Is there a straightforward way to achieve the desired layout using CSS? Any guidance or feedback would be appreciated. Apologies if this topic has been discussed before.