Make flex items use 100% height of parent with flex-direction: row - html

So I am attempting to have 3 flex items with equal height and width inside a flex container. I am successful in making the 3 flex items have equal width. However, the first flex-item has a div (.images) that is also a flex container which contains a few more children than the other 2 flex items' (.images) div. This results in the height of the 1st flex-item to be larger than the other 2. How do I make the height of the other 2 flex items have the same height as the first, even though they do not have the same amount of children? I researched this issue, but I only found answers when the flex-direction property is set to column. In my case the flex-direction property is set to row within the flex container.
body {margin: 0}
.flex-container {
display: flex;
flex-direction: row;
width: 100%;
}
.flex-items {
width: 33.333%;
height: 100%;
}
.images {
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 100%;
justify-content: center;
}
<div class="flex-container">
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image1.jpg" alt="">
<img src="image2.jpg" alt="">
<img src="image3.jpg" alt="">
<img src="image4.jpg" alt="">
<img src="image5.jpg" alt="">
</div>
</div>
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image6.jpg" alt="">
<img src="image7.jpg" alt="">
</div>
</div>
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image8.jpg" alt="">
<img src="image9.jpg" alt="">
</div>
</div>
</div>

You need to give the images inside .images a height of 100% and a width of auto - if you want them to scale proportionately, like so:
.images {
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 100%; (images is getting 100% height, but nothing is telling its children to go up in height)
justify-content: center;
}
.images img { (address the children)
height:100%;
width:auto;
}

If you don't set images width and height they will use auto, and scale it from that for the flex box depending on what browser your on of course.
Images can be manipulated inline or using css by using the height or width property, an example of both is below.
Inline Example 1:
<img src="#.png" width="auto" height="100%" />
Css Example 2:
div.images > img {
height: 100%;
width: auto;
}
It is also good idea to optimize images for your web page and not to rely on auto scaling for the best results. After all you are the one that knows what the end result should be.
:)

Related

How to specify when element should wrap with flexbox?

I have 2 blocks that are wrapped in flex container. I specified so that flex-row will wrap, but is there any way to clearly indicate when should elements wrap? I want to resize content inside flex-items until some breakpoint and only then wrap them.
Code looks like that:
.flex-row{
display:flex;
flex-wrap:wrap;
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="img"/>
</div>
</div>
Appreciate your help. Thank you in advance.
Without the flex-wrap property it wont wrap so you can use media querys to exactly define when to wrap the elements. But you also have to set the width for the flex-items.
.flex-row {
display: flex;
}
/* Wrap */
#media only screen and (max-width: 1023px) {
.flex-row {
flex-wrap: wrap;
}
.block1,
.block2 {
width: 100%;
}
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="http://via.placeholder.com/350x350"/>
</div>
</div>
You could use min-width on the blocks. This means they will fully stretch, but when the screen size limits them to being 200px in this example, that breakpoint will lead them to wrap, and their width will never go below 200px.
Another option is to just apply flex-wrap: wrap; on that specific breakpoint you want with a media-query.
For further control, you could also look into flex-basis: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis
EDIT: Responsive image
Genereally it's good to include this line of code on most images: max-width: 100%; height: auto; as this will make images auto-responsive. max-width: 100%; forces the image to never overflow from its container, and height: auto; adjusts the images height so its aspect ratio is correct. Try dragging the screen size and you will see its effect :)
.flex-row {
display: flex;
flex-wrap: wrap;
}
.block1,
.block2 {
min-width: 200px;
}
.block2 img {
max-width: 100%;
height: auto;
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="http://via.placeholder.com/350x350" />
</div>
</div>

How to make divs stack on top of each other as page is minimized

I am trying to figure out how to make a section of divs on my page responsive so that as the page is minimized, the divs begin stacking on top of each other. I've tried flex-direction: column but that doesn't seem to be working. I have attached photos of what I am trying to achieve.
Here is my HTML:
<div>
<!-- <section> -->
<center>
<div class="container">
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
</div>
</center>
<!-- </section> -->
</div>
CSS
.container {
display: flex;
flex-direction: row;
}
Photos of what I am trying to achieve
Desktop
Tablet
Mobile
FYI I have taken some code out for confidentiality purposes.
try flex-wrap: wrap;
use .item3{ flex: 1 } if needed
A flex-box solution is hard because it is meant for 1 directional layouts where as your desired layout is 2 directional based on screen width. However, you can come close to the desired layout behavior by using flex-wrap which depends on either the width of your flex-items or flex-container to force the items to a new line. Try the below and adjust either the items' or container's width
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: start;
}
You can give max-width: 1200px; to your body element then
margin:0 auto to center vertically.
Than create a flexbox inside container class and make flex-wrap: wrap; to make flexbox create new row else all the images will always stay in same row.
You can also add padding: 0 1rem; for always having empty spaces when images comes to limit of wrapping.
/* RESET */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: antiquewhite;
min-height: 100vh;
max-width: 1200px;
margin: 0 auto;
}
img {
max-width: 100%;
display: block;
}
/* RESET ENDS */
.container {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
gap: 1rem;
padding: 0 1rem;
}
<div class="container">
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
</div>

Why do img elements behave different inside of a flexbox? [duplicate]

This question already has answers here:
How do browsers calculate width when child depends on parent, and parent's depends on child's
(1 answer)
Why don't flex items shrink past content size?
(5 answers)
Closed 2 years ago.
If I have a flexbox with a max-width: 500px, and inside I have 3 img elements, they extend beyond the max-width of the flexbox.
However, if I wrap all of those img elements in a div, they remain contained within the max-width of the flexbox.
I thought it might be due to the display property, but changing the img element to display: block also doesn't keep it contained.
Overflow of flexbox:
.flex-box {
display: flex;
justify-content: center;
align-items: center;
max-width: 500px;
margin: 0 auto;
}
img {
width: 100%;
max-width: 100%;
}
<div class="flex-box">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
Contained within flexbox:
.flex-box {
display: flex;
justify-content: center;
align-items: center;
max-width: 500px;
margin: 0 auto;
}
img {
width: 100%;
max-width: 100%;
}
<div class="flex-box">
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
<div>
<img src="https://images.pexels.com/photos/1586154/pexels-photo-1586154.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="">
</div>
</div>

How can I center content without content stretching?

I want to center the 2 images but the images are being stretched out of proportion because of display: flex but justify-content wont work without it
is there another solution to centering the images?
<div class="column-photo">
<div class="center">
<img src="img/2.%20GenerationAnxiety%20page%202,2.png"
style="width:40vw;">
<img src="img/3.%20GenerationAnxiety%20page%203,3.png"
style="width:40vw;">
</div>
column-photo {
flex-wrap: wrap;
flex: 33.33%;
}
.center {
display: flex;
justify-content: center!important;
align-content: center!important;
}
You have missed dot with class column-photo.
Alternate way without specific width
.column-photo {
text-align:center;
display:block;
}
.center {
display:inline-block;
float:none;
}
If you want stretch the images but keep the aspact ratio you should wrap the images in a div with a individual width. Then you can give the image a width of 100% to keep the aspact.
If you should use vw in this case is another question
<div class="column-photo">
<div class="center">
<div class="img-container" style="width: 40vw">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="img-container" style="width: 40vw">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
</div>
img{
width: 100%;
}
Looks like you've messed up some properties:
.center {
display: flex;
justify-content: center;
align-items: center;
}
<div class="center">
<img src="https://pbs.twimg.com/profile_images/843090368837042176/Nl-rCb9c_400x400.jpg" style="width:10vw;">
<img src="https://pbs.twimg.com/profile_images/843090368837042176/Nl-rCb9c_400x400.jpg" style="width:20vw;">
</div>

CSS - Image height won't scale with width

Earlier I was pointed out how to achieve a certain layout I wanted to have for my page. However, this messes with my image height. As far as I understand, height: auto; should set the height to right proportion when a certain width is set.
Here's my code:
.floatingImage {
width: 50px;
height: auto;
}
#floatingImageContainer {
background-color: red;
width: 75%;
height: 100%;
margin: auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
<body>
<div id = "floatingImageContainer">
<img src = "images\miniImages\1.jpg" class = "floatingImage"></img>
<img src = "images\miniImages\2.jpg" class = "floatingImage"></img>
<img src = "images\miniImages\3.jpg" class = "floatingImage"></img>
<img src = "images\miniImages\4.jpg" class = "floatingImage"></img>
<img src = "images\miniImages\5.jpg" class = "floatingImage"></img>
<img src = "images\miniImages\6.jpg" class = "floatingImage"></img>
</div>
</body>
My guess is that it's got to do with the display property or maybe the flex-wrap, but that was the solution for my last problem and I'm not entirely sure yet how it could effect my image height... haven't added a margin in the screenshot, however that wouldn't change the height.
here's a screenshot of the issue:
what the hell
Thank you in advance!
New problem:
You need to add align-items with an appropriate setting to the container for the auto height to work, for example align-items: flex-start;. Otherwise the items will be stretched to full height of the container by default:
html, body {
margin: 0;
height: 100%;
}
.floatingImage {
width: 50px;
height: auto;
}
#floatingImageContainer {
background-color: red;
width: 75%;
height: 100%;
margin: auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
}
<div id="floatingImageContainer">
<img src="http://placehold.it/300x500" class="floatingImage">
<img src="http://placehold.it/300x200" class="floatingImage">
<img src="http://placehold.it/200x200" class="floatingImage">
<img src="http://placehold.it/300x400" class="floatingImage">
<img src="http://placehold.it/400x200" class="floatingImage">
<img src="http://placehold.it/300x350" class="floatingImage">
</div>
BTW, as mentioned in the comments: Closing </img> tags are invalid HTML - erase them...
Use
align-items: center
on the container to prevent it from stretching its children.
.floatingImage {
width: 50px;
height: auto;
}
#floatingImageContainer {
background-color: red;
width: 75%;
height: 100%;
margin: auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
<body>
<div id="floatingImageContainer">
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
<img src="http://lorempixel.com/g/400/200/" class="floatingImage"/>
</div>
</body>
Instead of using display: flex to center the images horizontally which I am guessing was your last issue to fix, just use display: block and text-align: center. This will not mess with the images.
Also, I recommend setting image widths inside the actual <img> tag, setting the width property inside the tag and with no height will do the exact same as using CSS height: auto. I recommend this because for one it helps with arbitrary user agents(i.e. speech browsers), and relaying the correct aspect ratio for them. These will not normally be able to read the CSS. In addition, this allows the browsers to size the images even before the CSS and images resources are loaded. Not supplying, a width attribute in the image tag will cause the browser to render it as 0x0 until the browser can size.
Here is a working example:
#floatingImageContainer {
background-color: red;
width: 75%;
height: 100%;
display: block;
text-align: center
}
<div id="floatingImageContainer">
<img src="http://lorempixel.com/g/400/200/" width="50" />
<img src="http://lorempixel.com/g/400/200/" width="50" />
<img src="http://lorempixel.com/g/400/200/" width="50" />
<img src="http://lorempixel.com/g/400/200/" width="50" />
<img src="http://lorempixel.com/g/400/200/" width="50" />
<img src="http://lorempixel.com/g/400/200/" width="50" />
</div>