Flexbox square items independent of item count - html

I use some inline styling in the HTML Doc. I would like to achieve a flexbox with n divisions where divs are squared. Within these divs I want to add certain images (here a placeholder)
I was looking up some other threads where there was padding used as a measure to adjust the box "height" since it is calculated upon width. However this solution only expands the current box in height (outlined with the blue border).
Has anyone a tip on how to avoid this?
EDIT: Apparently the padding solution works while using units like vh and vw instead of percentage and as long as I do not insert an image
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.image {
position: relative;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>

We now have access to aspect-ratio in CSS although it is poorly supported at the time of writing.
The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions.
.container {
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
margin: 2.5%;
flex: 1;
aspect-ratio: 1;
border: 5px solid blue;
overflow: hidden;
}
.image {
max-width: 100%;
display: block;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>

To manage this, there is a little known trick (tbh I didn't know you could do this) with setting an aspect ratio on divs of unknown/dynamic widths. See this article.
I ended up adding position: absolute for the images to not mess with the height of the divs after applying this 1:1 ratio for your scenario:
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
This might be what you are trying to do:
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
.image {
position: absolute;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>

Related

How to Justify a Image Grid?

I want all the rows to be of the same width and the images to occupy the full width of the row. Images can be fit to cover the dimensions. I have attached the code pen: https://codepen.io/shridhar_ke/pen/gOvpZOB
.header {
text-align: center;
padding: 32px;
}
.container {
width: 90%;
display: flex;
flex-direction: column;
margin-left: auto;
margin-right: auto;
}
.row {
display: flex;
padding: 0 4px;
height: 150px;
width: 100%;
margin-top: 8px;
text-align: justify;
}
.row img {
margin-left: 8px;
horizontal-align: middle;
height: 100%;
object-fit: cover;
width:auto;
}
<div class="container">
<div class="row">
<img src="https://via.placeholder.com/150x100.png" style="height:100%">
<img src="https://via.placeholder.com/160x200.png">
<img src="https://via.placeholder.com/200x200.png" style="height:100%">
</div>
<div class="row">
<img src="https://via.placeholder.com/180x240.png" style="height:100%">
<img src="https://via.placeholder.com/300x200.png" style="height:100%">
<img src="https://via.placeholder.com/400x200.png" style="height:100%">
</div>
<div class="row">
<img src="https://via.placeholder.com/250x300.png" style="height:100%">
<img src="https://via.placeholder.com/320x200.png" style="height:100%">
<img src="https://via.placeholder.com/240x180.png" style="height:100%">
</div>
</div>
I think it's possible with pure CSS. The key here is using the flex property.
Using the flex property
By setting flex: 1 1 auto; to the images, they're allowed to grow or shrink in order to fill up the entire row.
The only 'problem' here is that when the viewport gets too small, some rows will start shrinking and others won't. This is due to the fact that the height of your rows is hard coded to 150px. This is demonstrated by the code below:
.container {
width: 90%;
display: flex;
flex-direction: column;
margin-left: auto;
margin-right: auto;
}
.row {
display: flex;
padding: 0 4px;
height: 150px;
width: 100%;
margin-top: 8px;
/* Can be left out:
text-align: justify; */
}
.row img {
margin-left: 8px;
/* This property doesn't exist:
horizontal-align: middle; */
height: 100%;
object-fit: cover;
/* Can be left out:
width: auto; */
flex: 1 1 auto; /* Added this property */
}
<div class="container">
<div class="row">
<img src="https://via.placeholder.com/150x100.png" />
<img src="https://via.placeholder.com/160x200.png" />
<img src="https://via.placeholder.com/200x200.png" />
</div>
<div class="row">
<img src="https://via.placeholder.com/180x240.png" />
<img src="https://via.placeholder.com/300x200.png" />
<img src="https://via.placeholder.com/400x200.png" />
</div>
<div class="row">
<img src="https://via.placeholder.com/250x300.png" />
<img src="https://via.placeholder.com/320x200.png" />
<img src="https://via.placeholder.com/240x180.png" />
</div>
</div>
Adding responsive units to the mix
This problem can be fixed by changing the height of the rows using a responsive unit, like vw (viewport width), like I did in the final version below:
.container {
width: 90%;
display: flex;
flex-direction: column;
margin-left: auto;
margin-right: auto;
}
.row {
display: flex;
padding: 0 4px;
height: 15vw; /* Changed this property */
width: 100%;
margin-top: 8px;
}
.row img {
margin-left: 8px;
height: 100%;
object-fit: cover;
flex: 1 1 auto; /* Added this property */
}
<div class="container">
<div class="row">
<img src="https://via.placeholder.com/150x100.png" />
<img src="https://via.placeholder.com/160x200.png" />
<img src="https://via.placeholder.com/200x200.png" />
</div>
<div class="row">
<img src="https://via.placeholder.com/180x240.png" />
<img src="https://via.placeholder.com/300x200.png" />
<img src="https://via.placeholder.com/400x200.png" />
</div>
<div class="row">
<img src="https://via.placeholder.com/250x300.png" />
<img src="https://via.placeholder.com/320x200.png" />
<img src="https://via.placeholder.com/240x180.png" />
</div>
</div>

How to make the grid columns smaller and the same size?

I have a gallery of gifs and I put them in a grid, here's the image. I want to make the columns smaller like at least 5 images in one row, and in the same size(in the image you can see there's a difference in height).
here's the css:
.gallery {
margin: 5px;
border: 1px solid #ccc;
}
.gallery:hover {
border: 1px solid #777;
}
.gallery img {
width: 100%;
height: 100%;
object-fit:cover;
}
.container {
background-color: #222324;
padding: 20px;
margin: 20px;
overflow: auto;
text-align: center;
}
and the html:
<div class='container' style='display: grid;grid-template-columns: auto auto auto;'>
<div class='gallery'>
<img src=GIFURL>
</div>
<div class='gallery'>
<img src=GIFURL2>
</div>
...
</div>
thanks
You can have 5 images in a row with help of grid-template-columns: repeat(5, 1fr); It will accommodate 5 elements in a row all having the same width.
you can control the height of images based on viewport height by putting height: calc(100vh /3); on gallery image.
.gallery {
margin: 5px;
border: 1px solid #ccc;
}
.gallery:hover {
border: 1px solid #777;
}
.gallery img {
width: 100%;
height: calc(100vh /3);
object-fit:cover;
}
.container {
display: grid;
grid-template-columns: repeat(5, 1fr);
background-color: #222324;
padding: 20px;
margin: 20px;
overflow: auto;
text-align: center;
}
<div class='container'>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/200">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/400">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/300">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/300">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/200">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/100">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/500">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/300">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/300">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/100">
</div>
<div class='gallery'>
<img src="https://picsum.photos/id/237/200/100">
</div>
</div>

Vertical "space-between" distribution of images without flexbox

I have several columns of images of different sizes. As the sizes are unknown, one column will be the tallest. I now want to stretch out the other (smaller) columns to match that height by increasing the gaps between the images accordingly. Here is an example image:
And here is a jsfiddle of this example that I set up with flexbox.
#main {
width: 50%;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column:last-child {
margin-right: 0;
}
.column img {
width: 100%;
align-self: center;
}
<div id="main">
<div class="column">
<img src="http://placekitten.com/g/200/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/g/200/400">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/200">
<img src="http://placekitten.com/200/280">
<img src="http://placekitten.com/g/200/250">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/400">
<img src="http://placekitten.com/200/220">
<img src="http://placekitten.com/g/200/260">
</div>
</div>
However in my specific case, I cannot use flexbox (as I need to absolute position some children), so I am now looking for a way to achieve the same thing without flexbox. Is there any way to get this vertical "space-between" distribution without flexbox?
Based on the comment regarding absolute positioning:
I have tried to get the absolute positioning to work without any success. Basically I am trying to place captions underneath each images, however this captions should not be part of the flow, so the gaps should keep the same with as if there were no captions. When I tried to place the captions underneath, I ended up with all captions on the bottom of the entire column.
The solution is to rust wrap the images and captions in a div (or better still a figure) and give that position relative...then position your captions absolutely.
Like so:
#main {
max-width: 80%;
margin: auto;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.holder {
position: relative;
}
.column img {
display: block;
max-width: 100%;
height: auto;
}
.caption {
position: absolute;
bottom: 0;
text-align: center;
background: rgba(255, 255, 255, 0.5);
width: 100%;
}
<div id="main">
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/300">
</div>
<div class="holder"> <img src="http://placekitten.com/200/300"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/400">
</div>
</div>
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/200">
<div class="caption">My Caption</div>
</div>
<div class="holder"> <img src="http://placekitten.com/200/280"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/250">
<div class="caption">My Caption</div>
</div>
</div>
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/400">
<div class="caption">Superduper long Caption In Here</div>
</div>
<div class="holder"> <img src="http://placekitten.com/200/220"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/260">
</div>
</div>
</div>
fix image height and use "object-fit:cover;"
#main {
width: 50%;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column:last-child {
margin-right: 0;
}
.column img {
width: 100%;
align-self: center;
height:100px;
object-fit: cover;
}
<div id="main">
<div class="column">
<img src="http://placekitten.com/g/200/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/g/200/400">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/200">
<img src="http://placekitten.com/200/280">
<img src="http://placekitten.com/g/200/250">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/400">
<img src="http://placekitten.com/200/220">
<img src="http://placekitten.com/g/200/260">
</div>
</div>

Overflow with a flex div not working in FF

I have a simple demo of an error I found. There is a flex div and a few images inside.
The sizes of the div and the images are unknown, I put some fixed values just to represent the problem.
The problem is that the images are not overflowing the div's width in FF, but they do in Chrome (the expected and desired behavior).
The main goal is to make only 3 images to be visible (33.3333% of the div's width for each image), even if there are more than 3 images.
Demo: http://codepen.io/alexandernst/pen/mPoeLP
HTML:
<div class="wrapper">
<img class="box" src="http://placehold.it/150x150">
<img class="box" src="http://placehold.it/150x150">
<img class="box" src="http://placehold.it/150x150">
<img class="box" src="http://placehold.it/150x150">
<img class="box" src="http://placehold.it/150x150">
</div>
CSS:
.wrapper {
border: 1px solid red;
width: 400px;
height: 200px;
display: flex;
overflow: hidden;
}
.box {
border: 1px solid green;
max-width: 33.33333%;
position: relative;
padding-right: 10px;
align-self: center;
}
I'd suggest wrapping each of the images in a div and use the .box class on that instead.
Seems to work in Chrome 51 + FF 46
* {
box-sizing: border-box;
}
.wrapper {
border: 1px solid red;
width: 400px;
height: 200px;
display: flex;
align-items: center;
overflow: hidden;
}
.box {
border: 1px solid green;
flex: 0 0 33.33333%;
position: relative;
padding-right: 10px;
}
.box img {
width: 100%;
height: auto;
display: block;
}
<div class="wrapper">
<div class="box">
<img src="http://placehold.it/150x150">
</div>
<div class="box">
<img src="http://placehold.it/150x150">
</div>
<div class="box">
<img src="http://placehold.it/150x150">
</div>
<div class="box">
<img src="http://placehold.it/150x150">
</div>
<div class="box">
<img src="http://placehold.it/150x150">
</div>
</div>
It is because you have padding-right. Try adding box-sizing: border-box;:
.box {
border: 1px solid green;
max-width: 33.33333%;
box-sizing: border-box;
position: relative;
padding-right: 10px;
align-self: center;
}
box-sizing will take padding in its calculations.
Updated codepen.

CSS Styling for div layout

Please see my HTML structure below - I am trying to have the .prod divs to be to the right of the logo, and the logo to be the full height of the .row div
I know this can be done using tables and floats but I want to try and avoid using those.
Here's my structure:
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
}
.row .prod {
width: 75%;
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Wrap the additional info inside another div and give it the remaining width.
I have given it 74% because of the extra space from inline-block elements. Adjust it to your requirement. I would prefer flexbox if you are implementing it for modern browsers.
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
vertical-align: top; /* Default to baseline, align to the top */
}
.row .product_info {
width: 74%; /* Remaining width */
}
.row .product_info .prod {
/* width: 75% */ Remove
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="product_info">
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
</div>
flexbox can do that.
.row {
display: flex;
}
.image,
.prod {
flex: 1;
background: lightblue;
text-align: center;
height: 75px;
}
.image {
flex: 0 0 auto;
background: orange;
display: flex;
flex-direction: column;
}
.image img {
height: 100%;
width: auto;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="http://lorempixel.com/output/food-q-c-50-50-1.jpg" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Flexbox?
I'm still pretty new to flexbox myself, but you could use the align-items: stretch property on your container to cause your items to stretch, but give the .prod div's a max-height so that they only stretch so much. You can also set various widths properties using the flex property on your flex items so that you get the width your after.
For more information: https://css-tricks.com/snippets/css/a-guide-to-flexbox/