How to Justify a Image Grid? - html

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>

Related

Flexbox square items independent of item count

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>

last row of flex div not starting from the left [duplicate]

This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Closed 2 years ago.
I have seen this, question has been asked a lot but I have not really gotten an answer that works. I am trying to create 3 centred divs with multiple rows using (flex box) not grid please. Is it possible and what simple way. it should be center aligned.
I am trying to achieve this.
see as its centrally aligned. but mine is kinda alined to the left and if I use Justify content:center for the wrapper the two boxes go in the middle, like this.
this is my code
<div class="wrapper">
<div id="squares">
<img src="images/galleryimage1.jpg"/>
</div>
<div id="squares">
<img src="images/galleryimage2.jpg"/>
</div>
<div id="squares">
<img src="images/galleryimage1.jpg"/>
</div>
<div id="squares">
<img src="images/galleryimage2.jpg"/>
</div>
<div id="squares">
<img src="images/galleryimage2.jpg"/>
</div>
</div>
.wrapper {
background: #ff0000;
text-align: center;
width: 90%;
height: auto;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
padding: 0 5% 0;
justify-content: center;
}
#squares {
background: #00ff00;
width: 30%;
height: 100px;
margin: 10px;
}
#squares img {
max-height: 300px;
width: 100%;
}
#squares h5 {
margin: 20px 0;
}
here's the link to my jfiddle for a clearer picture.
https://jsfiddle.net/9ros2v4j/6/
Thanks to anyone that can explain.
.wrapper {
background: green;
text-align: center;
width: 80%;
margin-left: auto;
margin-right: auto;
}
.wrapper-inner {
padding: 5px;
display: flex;
flex-wrap: wrap;
}
.square {
flex: 0 1 33.33%;
}
.square img {
width: 100%;
display: block;
}
.square-inner {
padding: 5px;
}
<div class="wrapper">
<div class="wrapper-inner">
<div class="square">
<div class="square-inner">
<img src="http://placekitten.com/200/200" />
</div>
</div>
<div class="square">
<div class="square-inner">
<img src="http://placekitten.com/200/200" />
</div>
</div>
<div class="square">
<div class="square-inner">
<img src="http://placekitten.com/200/200" />
</div>
</div>
<div class="square">
<div class="square-inner">
<img src="http://placekitten.com/200/200" />
</div>
</div>
<div class="square">
<div class="square-inner">
<img src="http://placekitten.com/200/200" />
</div>
</div>
</div>
</div>
One requirement is for justify-content: flex-start which would place your last row as per your need.
The second requirement you're asking for is that they should be centered also. For that I think you can use equal padding on both sides to make the rows look as if they are center-aligned.
Or
If you want you can place all your items in another div inside flex-container. Then you can justify-content: center the newly created div.
You can align items to the left with justify-content: flex-start; instead of justify-content: center but in order to center it all, you might need to start playing with margins and screen size.
If you open the below example on a full page, you will be able to see the expected result.
Please also note that you used id in multiple places (#squares) which could cause issues. I replaced it with a class.
.wrapper {
position: relative;
text-align: center;
height: auto;
background: #ff0000;
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
width: 100%;
}
.squares {
background: #00ff00;
width: 30%;
height: 100px;
flex: 0 31.33%;
margin: 1%;
}
#squares img {
max-height: 300px;
width: 100%;
}
#squares h5 {
margin: 20px 0;
}
<div class="wrapper">
<div class="squares">
<img src="images/galleryimage1.jpg"/>
</div>
<div class="squares">
<img src="images/galleryimage2.jpg"/>
</div>
<div class="squares">
<img src="images/galleryimage1.jpg"/>
</div>
<div class="squares">
<img src="images/galleryimage2.jpg"/>
</div>
<div class="squares">
<img src="images/galleryimage2.jpg"/>
</div>
</div>

Unable to center div with images

I've been working on a website for the past few hours and recently I added an image grid using CSS3 Flexbox. However I've been having trouble centering it for the past few hours.
I've tried using justify-content: center to center it as well as margin: 0 auto but nothing works.
What am I doing wrong?
Here's my code
HTML
<div class="gallery">
<h3>Gallery</h3>
<div class="tiles">
<div class="row">
<div class="column">
<img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Mahatma-Gandhi%2C_studio%2C_1931.jpg">
<img src="https://www.biography.com/.image/t_share/MTYyNzM4ODIyODY0NDQ2NTA0/gandhi-spiritual-leader-leading-the-salt-march-in-protest-against-the-government-monopoly-on-salt-production-photo-by-central-pressgetty-images.jpg">
</div>
<div class="column">
<img src="https://akm-img-a-in.tosshub.com/indiatoday/images/story/201911/Mahatma_Gandhi-770x433.png?DtfYcyk4yzMDy1GNsIJaQgX7mrBoqTQO">
<img src="https://images.news18.com/ibnlive/uploads/2018/10/Mahatma-Gandhi2.jpg">
</div>
<div class="column">
<img src="https://images.livemint.com/img/2019/10/02/600x338/gandhi_1570037914879.JPG">
<img src="https://m.telegraphindia.com/unsafe/620x350/smart/static.telegraphindia.com/derivative/THE_TELEGRAPH/1671172/16X9/image34ba57f1-21d2-4af6-ad21-b9c036e39194.jpg">
<img src="https://img.etimg.com/thumb/msid-67754218,width-640,resizemode-4,imgsize-184267/he-whom-leaders-looked-up-to.jpg">
</div>
</div>
</div>
</div>
CSS
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
.column {
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
.gallery {
height: auto;
width: 100%;
text-align: center;
}
.gallery h3 {
font-size: 60px;
margin-bottom: 40px;
}
.tiles {
display: block;
width: 100%;
margin: 0 auto;
}
How do I center the tiles div?
if you want to have 3 columns in the center of you screen, you have to:
put the display: flex; justify-content: center in the .tiles class
change the column width property to width: 33% and removing the max-width: 25%
give to your .row class the width you want, or the max-width, like max-width:75%

Having an issue with alignment of image/text

Trying to align the image vertically, and the text centered according to image. The whole card is about 300px * 200px. I want the image to be on the left hand side with text on the right.
<div class="card center-block">
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="../im/3.jpg" width="130" height="130" style="min-height:50px;" />
}
</div>
</div>
<div class="col-sm-6" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>
CSS:
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
padding-top: 35px;
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.content{
height: 100%;
}
You can add a class to the row and use flexbox positioning. align-items: center on the row that holds the 2 columns, then also use flex on the .content element and create a flex column with justify-content: center
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.special-row {
display: flex;
align-items: center;
}
.special-row .content {
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card center-block">
<div class="container"><div class="row special-row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="http://androidandme.com/wp-content/uploads/2015/12/Google-Now-on-Tap.jpg"width="130" height="130" style="min-height:50px;" />
</div>
</div>
<div class="col-sm-6 details" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>

How to put 2 rows of images inside a 'column' using flex boxes

I'm building a site using flex boxes. I have a div with 2 'columns' inside, and 2 'rows' inside of the second 'column' which I have to fill with two images each, the problem is the images doesn't fit into the 'rows' and exceeds its width.
I need my images stretch or shrink with the navigator size, so I can't use px for their size.
This is what I want:
And this is what I get:
Here is my code:
#offices {
background: peachpuff;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.col {
background: yellow;
flex: 1;
}
.row {
background: red;
line-height: 0;
display: flex;
}
#officesImg img {
flex: 1;
width: 100%;
height: 100%;
}
<div id="offices">
<div class="col">
</div>
<div class="col" id="officesImg">
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
</div>
</div>
Here is a CODEPEN
PD: Please avoid float solutions.
You can set #officesImg to display:flex. And removed height:100% from the img that causes aspect ration issue on Firefox.
#offices {
background: peachpuff;
margin: 1em;
display: flex;
}
.col {
background: yellow;
margin: 1em;
flex: 1;
}
#officesImg {
line-height: 0;
display: flex;
}
#officesImg img {
width: 100%;
height: auto;
}
<div id="offices">
<div class="col">
</div>
<div class="col" id="officesImg">
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
</div>
</div>
Here is a slightly polished version that matches your wireframe.
#offices {
background: peachpuff;
margin: 1em;
display: flex;
}
.col {
background: yellow;
margin: 1em;
flex: 1;
}
#officesImg {
line-height: 0;
display: flex;
padding: .5em;
}
#officesImg img {
width: calc(100% - 1em);
height: auto;
margin: .5em;
}
<div id="offices">
<div class="col">
</div>
<div class="col" id="officesImg">
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_01_zpsewjzabzm.jpg" />
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" />
</div>
</div>
</div>
The quickest solution is to wrap each image element in a div.
In other words:
<div><img ... ></div>
Images remain responsive and aspect ratio is maintained.
Tested in Chrome, Firefox and IE11.
Revised Codepen