Display images in flexbox at 100% height without scrollbar - html

I'm trying to display an app bar and three images in a column, that uses 100% of the height of the screen. The images are supposed to use the whole width of the column with the rest being cut off. I can get it working with just divs, but I'm having trouble when using images.
Here is a version to illustrate how it should look like. This has an app bar of height 50 and three "images" that fill the rest of the space:
https://codepen.io/Meerpohl/pen/zYxRKRV
And here is what I get with images. The images stretch the heights of my divs and ultimately of everything else, resulting in that scrollbar. Instead I need thin slices of the images.
html, body {
height: 100%;
margin: 0;
}
.root {
height: 100%;
display: flex;
flex-direction: column;
}
.appbar {
height: 50px;
text-align: center;
background-color: coral;
}
.container {
flex: 1;
}
.item {
height:33.33%;
overflow:hidden;
}
img {
width: 100%;
object-fit: cover;
}
<div class="root">
<div class="appbar">
This is a nice app bar
</div>
<div class="container">
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
<div class="item">
<img src="http://www.kleines-meerwasseraquarium.de/wp-content/uploads/2016/02/Zitronengrundel.jpg">
</div>
</div>
</div>
https://codepen.io/Meerpohl/pen/eYmVdro
The code is the same in both cases. One just uses text instead images.

.item {
position: relative;
height:33%;
overflow:hidden;
}
img {
position: absolute;
transform: translateY(-50%);
width: 100%;
object-fit: cover;
}
this should work i think!

i'm on the train right now, so I can't give you a pen.
You can position the image absolute in the parent div (this should be relative) and translateY so it is centered.
Hope this is what you want to do ;)

use this!
img {width: 100%; object-fit: cover; max-height: 33.33vh; }

Try this:
html, body {
height: 100%;
margin: 0;
}
.root {
height: 100%;
display: flex;
flex-direction: column;
}
.appbar {
height: 50px;
background-color: coral;
display: flex;
justify-content: center;
align-items: center;
}
.container {
height: calc(100vh - 50px);
display: flex;
flex-direction: column;
}
.item {
overflow: hidden;
display: flex;
flex: 1;
}
img {
width: 100%;
object-fit: cover;
}
added calc to .container and display:flex on .item, removed some unused properties.
Codepen: https://codepen.io/Liveindream/pen/NWPygpx

Related

How to make a specific div go on top of another div

I have two divs.
When resizing the browser, div 2 will go on the bottom, and div one will go on the top, something like the image below.
What I want is div 1 to go on the bottom and div 2 go on the top, basically the opposite of what it does. I know I can just put div 2 on the top in the html but I want the div 1 to stay on the left.
Current code:
.div1 {
width: 55%;
height: 80vh;
display: inline-block;
margin-right: 1.5vh;
min-width: 50vh;
}
.div2 {
width: 50vh;
height: 80vh;
display: inline-block;
}
<div class="div1">
</div>
<div class="div2">
</div>
Hope that makes sense, thx to everyone that helps in advance.
The simplest way is to make parent container as display: flex; and use flex-wrap: wrap-reverse;:
.div1 {
width: 55%;
height: 80vh;
display: inline-block;
margin-right: 1.5vh;
min-width: 50vh;
background-color: blue;
}
.div2 {
width: 50vh;
height: 80vh;
display: inline-block;
background-color: red;
}
.container
{
display: flex;
flex-wrap: wrap-reverse;
/* ignore below */
resize: both;
overflow: hidden;
border: 1px solid black;
padding: 1em;
}
<div class="container">
<div class="div1">
</div>
<div class="div2">
</div>
resize me
</div>
You can achieve this by combining two concepts: media queries and flexbox.
I've set the max-width of the screen size that the media query starts applying to 600px, but you can change this to whatever value (min or max) that you want. The switch in how the two divs render when in column-view is handled via flex-direction: column-reverse.
You'll need to wrap your divs in a parent container to apply them:
.container {
display: flex;
flex-direction: row;
}
#media screen and (max-width: 600px) {
.container {
flex-direction: column-reverse;
}
}
.div1 {
width: 55%;
height: 80vh;
display: inline-block;
margin-right: 1.5vh;
min-width: 50vh;
background: green;
}
.div2 {
width: 50vh;
height: 80vh;
display: inline-block;
background: orange;
}
<div class="container">
<div class="div1"></div>
<div class="div2"></div>
</div>
You can read up on the two concepts I mentioned above in more detail:
media queries
flexbox

How to make my content responsive to screen size both horizontally and vertically?

I try to make a page with container and content "responsive" that reduces with the window, especially the height.
Currently my code allows to reduce width but not height. It's possible to do that ?
My current code : https://jsfiddle.net/u1Ld5r7v/1/
html,
body {
margin: 0;
padding: 0;
height: 100vh;
width: auto;
}
body {
height: 100%;
overflow: hidden;
}
main {
display: flex;
}
img {
width: 22.5vw;
height: 35vw;
margin: 0;
object-fit: cover;
}
.wrapper {
display: flex;
}
.list {
height: 100vh;
display: flex;
flex-wrap: nowrap;
overflow-x: auto;
align-items: center;
padding: 0;
}
.list a {
margin: 0%;
padding: 0 4%;
}
<body>
<main class="wrapper">
<div class="list">
<img src="https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2014/01/af2.png">
<img src="https://www.wampstore.com/store/image/cache/data/Wamp/Products/Vallejo/Flat%20Blue-900x900.jpg">
<img src="https://www.craftmasterpaints.co.uk/images/colours/decorative-flat-colour/Orange.jpg">
<img src="https://www.craftmasterpaints.co.uk/images/colours/decorative-flat-colour/Pink.jpg">
<img src="https://i.pinimg.com/originals/bf/48/a7/bf48a70ec34fbcb3d71f3c685e98f95b.jpg">
<img src="https://emmanuel.info/wp-content/uploads/2018/12/rawpixel-577494-unsplash.jpg">
</div>
</main>
</body>
Thanks for your help.
The images scale only on horizontal re-size because they are sized with viewport width units (vw).
img {
width: 22.5vw;
height: 35vw;
}
If you wanted them to re-size on vertical re-size, then you would use viewport height units (vh).
If you want them to scale on both vertical and horizontal re-size, then try vmin or vmax units.
revised demo
https://drafts.csswg.org/css-values/#viewport-relative-lengths
Problem solved, here we use different properties to manage height and width.
Demo here.
HTML
<body>
<main class="wrapper">
<div class="list">
<img src="https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2014/01/af2.png">
<img src="https://www.wampstore.com/store/image/cache/data/Wamp/Products/Vallejo/Flat%20Blue-900x900.jpg">
<img src="https://www.craftmasterpaints.co.uk/images/colours/decorative-flat-colour/Orange.jpg">
<img src="https://www.craftmasterpaints.co.uk/images/colours/decorative-flat-colour/Pink.jpg">
<img src="https://i.pinimg.com/originals/bf/48/a7/bf48a70ec34fbcb3d71f3c685e98f95b.jpg">
<img src="https://emmanuel.info/wp-content/uploads/2018/12/rawpixel-577494-unsplash.jpg">
</div>
</main>
</body>
CSS
html,body {
margin: 0;
padding: 0;
height: 100vh;
width:auto;
}
body {
height: 100%;
overflow: hidden;
}
main {
display: flex;
}
img {
width: 45vmin; /*here we use vmin rather than vh or vw*/
height: 70vmin; /*here we use vmin rather than vh or vw*/
margin: 0;
object-fit: cover;
}
.wrapper {
display: flex;
}
.list {
width: 100vw;
height: 100vh; /*here we add height proprietie !important!*/
display: flex;
flex-wrap: nowrap;
overflow-x: auto;
padding: 0;
align-items: center;
}
.list a {
margin: 0%;
padding: 0 4%;
}
Thanks for your help :-)

background image doesn't shrink as screen size in firefox

I have a div called .outer which contained a div called .film, and the div .film has some of image divs.
The problem is .outer div doesn't shrink it as the screen size in Firefox like this picture:
As you can see, the div is shrinking well in Chrome(left side) but not in Firefox(right side).
Are there any ways to shrink the .outer div in Firefox just like Chrome does?
This is the code:
* {
margin: 0;
padding: 0;
}
section, div {
position: relative;
}
.outer {
margin: 0 auto;
width: 500px;
height: 500px;
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}
.film {
width: 90%;
margin: 0 .8%;
display: flex;
flex-flow: column;
justify-content: center;
flex-shrink: 0;
padding-bottom: 33.3333%;
background-image: url('https://i.imgur.com/3p6TLYE.jpg');
background-size: cover;
background-position: center center;
overflow: hidden;
}
.film:nth-child(2n-1) {
opacity: .4;
transform: scale();
}
<div class="outer">
<div class="film"></div>
<div class="film"></div>
<div class="film"></div>
</div>
First thing which comes to mind is, setting display, width and height property of html and body tag.
Have you tried the following?
html, body {
display: block;
width: 100%;
height: 100%;
}
Possible solution I can find is that changing your 500px to auto that works in firefox I have tested while now the maximum width is changed which can be controlled by max-width tag just change your outer with my code :
.outer {
margin: 0 auto;
width: auto;
max-width: 500px;
height: 500px;
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}
I think I found the solution and add the answer for the people who are having the same problem like me.
In Firefox, you should not use the px units if you want to shrink your div as the screen size. Use the padding-bottom instead of a height of the image like this:
.outer {
margin: 0 auto;
width: 80%; // basic size of the holder(outer). Use percentage unit to control it.
display: flex;
flex-flow: row;
justify-content: center;
overflow: hidden;
}
.film {
width: 90%;
margin: 0 .8%;
display: flex;
flex-flow: column;
justify-content: center;
flex-shrink: 0;
padding-bottom: 70.3333%; // Set the height of the images.
background-image: url('https://i.imgur.com/3p6TLYE.jpg');
background-size: cover;
background-position: center center;
overflow: hidden;
}
.film:nth-child(2n-1) {
opacity: .4;
transform: scale();
}
<div class="outer">
<div class="film"></div>
<div class="film"></div>
<div class="film"></div>
</div>

Keep image's ratio in a flex container on height resize

I have a problem with image in a flex container. I want it to keep ratio when the container/window height is decreased/resized down.
Sample fiddle
html,
body {
height: 100%;
}
.wrapper {
display: flex;
width: 100%;
height: 60%;
border: 1px solid red;
}
.image-container {
display: flex;
align-items: center;
max-width: 30%;
height: 100%;
background: green;
}
img {
max-width: 100%;
max-height: 100%;
}
.content {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
width: 100%;
height: 100%;
background: yellow;
}
<div class="wrapper">
<div class="image-container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Clouds_over_the_Atlantic_Ocean.jpg/1200px-Clouds_over_the_Atlantic_Ocean.jpg" alt="">
</div>
<div class="content">
<span>Some content</span>
</div>
</div>
When you try to do it you can see that only image's height changes but width stays the same and the image is kind of stretched.
When I delete 'display: flex' from the image container, an image resizes well. I made it flex because i wanted it to be centered.
Is there a way to keep the ratio and fluidly resize image and the rest of containers?
Just add object-fit: cover; on img tag:
img {
width: 100%;
height: 100%;
object-fit: cover;
}
JSFiddle

How to fit an image vertically in a flex container with direction: column

I'm trying to fit an image vertically in a flex container which has a specific height.
The flex-direction is column, and the image is contained in a flex-item with flex-basis: 100%.
In addition, the image's max-height is 100%.
As you can see in the example, the image does not fit into the red container.
#container {
background-color: red;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: space-between;
}
#container > * {
padding: 5px;
}
#img {
flex: 0 1 100%;
/* height: 100%; */
}
img {
max-height: 100%;
max-width: 100%;
}
<div id="container">
<div id="img">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img&w=340&h=500">
</div>
<div>
Something else
</div>
</div>
Shouldn't the image shrink to fit vertically into the flex container, according to the specification?
The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.
As an additional note, if I set flex-direction: row to the container, it fits the image horizontally (which is the behaviour I would expect).
You wrote:
The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.
Actually, it is the way it should be done. The predominant implementation of the spec requires that the height property be applied to the parent when using percentage heights on the child. (Although this is slowly evolving. See my second reference below.)
References:
Working with the CSS height property and percentage values
Heights rendering differently in Chrome and Firefox (includes alternative solutions)
CSS height property definition (W3C)
For some reason, I couldn't get the <img> to behave (probably because it's a replaced element). So I removed it, and used the .img div with an image as background.
Relevant Changes
.container {
...
justify-content: center;
align-items: center;
}
.img {
flex: 1 0 auto;
background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
background-size: contain;
background-position: center;
width: 100%;
}
SNIPPET
.container {
background-color: red;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: center;
align-items: center;
}
.container > * {
padding: 5px;
}
.somethingElse {
outline: 1px dashed yellow;
background: rgba(128,0,255,.3);
color: white;
}
.img {
flex: 1 0 auto;
background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
background-size: contain;
background-position: center;
width: 100%;
}
<figure class="container">
<div class="img">
</div>
<figcaption class="somethingElse">
True Dimentions: 512 x 512 px
</figcaption>
</figure>
try this css. its works fine.
#container {
background-color: red;
/*display: flex;*/
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: space-between;
}
#img {
height: 85%;
width: 100%;
}