I'm sure this has been asked before but I can't find an answer to the exact question.
I want a very simple layout:
-------------------
header
-------------------
|
content | graphic
|
-------------------
where:
- The header height is set by the font size;
- The content has a fixed minimum width;
- The graphic is as large as possible given those two constraints and preserving aspect ratio (i.e. it will be as tall as the screen minus the header unless that would make the content panel too narrow);
- The content is as wide as possible given those three constraints.
[edited to add:]
- Vertical: The image is vertically centered when the width+aspect ratio constraints result in it being shorter than the maximum height
- Horizontal: The image is always hard against the right side of the screen (except for any manually added padding) and the content always goes right up to the left side of the image (also except for any manually added padding).
I've tried using flexboxes and have satisfied the first three constraints, but I can't get the content pane to grow horizontally to fill the space not used by the image. The best results I've got are using the HTML and CSS below but, as you can see in the screenshot below, this results in the content div and the image taking up the same size instead of the content div pushing the image to the right. (This is expected behavior from setting flex=1 on both, so I wasn't expecting it to work; but at least this gives me the image size behaving how I'd like).
What I'm using is at 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>
- grid style is flexbox in column direction containing header and grid__row;
- header has flex 0, i.e. height is set by contents;
- grid__row has flex 1, i.e. fills the height left over after the header; is also a flex container in row direction containing grid__item and img.
- grid__item has flex 1, i.e. fills the width available;
- img has flex 1 and uses object-fit = 1 (which gets the image sizing properties I need) and overflow=hidden (which I don't really understand what it's doing, but if I leave it out the img container expands horizontally by a factor of 2).
I haven't set min-width explicitly on the grid__item item in the jsfiddle, but I don't anticipate that making any difference.
Is there an easy way to get what I want in CSS? Apologies if this is a duplicate.