How do I maintain constant image height? Stuck in flexbox? - html

I am trying to create flexbox with different image heights and width. I want the break points as
max-width: 640px; ------->Only one column
max-width: 860px; -------->Two columns only
greater than: 860px;-------->Three column only
What I am messing with? Why it is becoming ugly while shrinking window?I am losing 3 column of image even before window size reach 860px breakpoint. What else can I do?
After window size reaches to 860px it is working fine, but until than it is becoming ugly.
I want my image height not be changed. Let it be as it is !
.container {
width: 100%;
height: 1000px;
display: flex;
flex-direction: column;
background: green;
flex-wrap: wrap;
align-items: center;
align-content: center;
justify-content: flex-start;
}
.box {
width: 30%;
border: 2px solid red;
margin: 8px;
}
img {
width: 100%;
height: auto;
}
#media screen and (max-width: 860px) {
.container {
background-color: red;
height: 1100px;
}
.box {
width: 46%;
}
}
#media screen and (max-width: 640px){
.container {
background-color: yellowgreen;
height: auto;
flex-direction: row;
}
.box {
width: 100%;
}
}
<div class="container">
<div class="box">
<img src="https://www.daily-sun.com/assets/news_images/2017/01/12/DAILYSUN_ZYAN.jpg" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/79/e2/8d/79e28db17abc2eb6c3085c9c72bff5e4.jpg" alt="">
</div>
<div class="box">
<img src="https://www.pinkvilla.com/files/styles/gallery-section/public/zayn_malik_tattoos_1.jpg?itok=Q5bXGlfm" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/b5/0c/30/b50c30ddaaffc98ff2cb077cc7f57823.jpg" alt="">
</div>
<div class="box">
<img src="https://images.news18.com/ibnlive/uploads/2021/01/1610783227_zayn-malik.jpg" alt="">
</div>
<div class="box">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gigi-hadid-and-zayn-malik-attend-the-manus-x-machina-news-photo-527409630-1554477973.jpg?crop=1.00xw:0.762xh;0,0.0194xh&resize=480:*" alt="">
</div>
</div>

In this cas, I would use CSS column-count and CSS grid.
Just an example with your code:
.container {
column-count: 3;
column-gap: 10px;
background: green;
padding: 10px;
}
#media (max-width: 640px) {
.container {
column-count: 1;
}
}
#media (min-width: 641px) and (max-width: 840px) {
.container {
column-count: 2;
}
}
img {
max-width: 100%;
display: block;
}
.box {
margin: 0;
display: grid;
grid-template-rows: 1fr auto;
margin-bottom: 10px;
break-inside: avoid;
border: 2px solid red;
}
<div class="container">
<div class="box">
<img src="https://www.daily-sun.com/assets/news_images/2017/01/12/DAILYSUN_ZYAN.jpg" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/79/e2/8d/79e28db17abc2eb6c3085c9c72bff5e4.jpg" alt="">
</div>
<div class="box">
<img src="https://www.pinkvilla.com/files/styles/gallery-section/public/zayn_malik_tattoos_1.jpg?itok=Q5bXGlfm" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/b5/0c/30/b50c30ddaaffc98ff2cb077cc7f57823.jpg" alt="">
</div>
<div class="box">
<img src="https://images.news18.com/ibnlive/uploads/2021/01/1610783227_zayn-malik.jpg" alt="">
</div>
<div class="box">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gigi-hadid-and-zayn-malik-attend-the-manus-x-machina-news-photo-527409630-1554477973.jpg?crop=1.00xw:0.762xh;0,0.0194xh&resize=480:*" alt="">
</div>
</div>

However for this layout using grid is the best way, but I tried to find a way by flex. For "flex-direction: column;" height is important so you should set a height that can't contain more than 2 images. I did it with "height: 70vw;"
.container {
width: 100%;
height: 70vw;
display: flex;
flex-direction: column;
background: green;
flex-wrap: wrap;
align-items: center;
align-content: center;
justify-content: flex-start;
}
.box {
width: 30%;
border: 2px solid red;
margin: 8px;
}
img {
width: 100%;
height: auto;
}
#media screen and (max-width: 860px) {
.container {
background-color: red;
height: 1120px;
}
.box {
width: 46%;
}
}
#media screen and (max-width: 640px){
.container {
background-color: yellowgreen;
height: auto;
flex-direction: row;
}
.box {
width: 100%;
}
}
<div class="container">
<div class="box">
<img src="https://www.daily-sun.com/assets/news_images/2017/01/12/DAILYSUN_ZYAN.jpg" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/79/e2/8d/79e28db17abc2eb6c3085c9c72bff5e4.jpg" alt="">
</div>
<div class="box">
<img src="https://www.pinkvilla.com/files/styles/gallery-section/public/zayn_malik_tattoos_1.jpg?itok=Q5bXGlfm" alt="">
</div>
<div class="box">
<img src="https://i.pinimg.com/originals/b5/0c/30/b50c30ddaaffc98ff2cb077cc7f57823.jpg" alt="">
</div>
<div class="box">
<img src="https://images.news18.com/ibnlive/uploads/2021/01/1610783227_zayn-malik.jpg" alt="">
</div>
<div class="box">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gigi-hadid-and-zayn-malik-attend-the-manus-x-machina-news-photo-527409630-1554477973.jpg?crop=1.00xw:0.762xh;0,0.0194xh&resize=480:*" alt="">
</div>
</div>

Related

why my div keep on the side and wont center

.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%;
}
#media screen and (max-width: 800px) {
.column {
flex: 50%;
max-width: 50%;
}
}
#media screen and (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
<table align="center">
<tr>
<td align="center">
<div class="row">
<div class="column">
<img src="pics/photos/sample1.png">
<img src="pics/photos/sample2.png">
<img src="pics/photos/sample3.png">
</div>
<div class="column">
<img src="pics/photos/sample4.png">
<img src="pics/photos/sample5.png">
<img src="pics/photos/sample6.png">
</div>
<div class="column">
<img src="pics/photos/sample7.png">
<img src="pics/photos/sample8.png">
<img src="pics/photos/sample9.png">
</div>
</div>
</td>
</tr>
</table>
Someone maybe know why my div isn't go to the center and keep stay on the side?
No matter what I writing with margin or align it keep stay on the side (on the body CSS I've putted direction: rtl; if that matter.
HTML:
<table align="center">
<tr>
<td align="center">
<div class="row">
<div class="column">
<img src="pics/photos/sample1.png">
<img src="pics/photos/sample2.png">
<img src="pics/photos/sample3.png">
</div>
<div class="column">
<img src="pics/photos/sample4.png">
<img src="pics/photos/sample5.png">
<img src="pics/photos/sample6.png">
</div>
<div class="column">
<img src="pics/photos/sample7.png">
<img src="pics/photos/sample8.png">
<img src="pics/photos/sample9.png">
</div>
</div>
</td>
</tr>
</table>
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%;
}
#media screen and (max-width: 800px) {
.column {
flex: 50%;
max-width: 50%;
}
}
#media screen and (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
Thank you all for the help!
from what I can understand you want your "column" divs to be centered inside row div. I will suggest you to learn flex-box and grid which makes thing easier.
I have added only two lines of code in your ".row" class
1.display:flex
2.justify-content:center
Below is my solution.
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
display: flex;
justify-content: center;
}
.column {
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
#media screen and (max-width: 800px) {
.column {
flex: 50%;
max-width: 50%;
}
}
#media screen and (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
<table align="center">
<tr>
<td align="center">
<div class="row">
<div class="column">
<img src="pics/photos/sample1.png">
<img src="pics/photos/sample2.png">
<img src="pics/photos/sample3.png">
</div>
<div class="column">
<img src="pics/photos/sample4.png">
<img src="pics/photos/sample5.png">
<img src="pics/photos/sample6.png">
</div>
<div class="column">
<img src="pics/photos/sample7.png">
<img src="pics/photos/sample8.png">
<img src="pics/photos/sample9.png">
</div>
</div>
</td>
</tr>
</table>

Image grid with autorescaling in HTML & CSS Only

I have been trying to make a responsive auto rescaling image grid to display ads on our website. I used this as a reference - W3Schools-ImageGridMaker
Based on that, I tried 4days to come up with this piece of code.
div.gallery {
border: 1px solid #ccc;
}
div.gallery:hover {
border: 1px solid #777;
}
div.gallery img {
display: block;
object-fit: contain;
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
* {
box-sizing: border-box;
}
.responsive {
padding: 0 6px;
float: left;
width: 24.99999%;
}
#media only screen and (max-width: 700px) {
.responsive {
width: 49.99999%;
margin: 6px 0;
}
}
#media only screen and (max-width: 500px) {
.responsive {
width: 100%;
}
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
div.boxed {
border: 5px solid red;
width: 100%;
height: auto;
overflow: auto;
}
<div class="boxed">
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/UgPbxk2.jpg" alt="Shiva1"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/ColLeDr.png" alt="Shiva2"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/gVjcLg2.jpg" alt="Shiva3"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/nxGxovl.png" alt="Shiva4"></a>
</div>
</div>
</div>
This code is responsive, and on mobiles I got no problem because it looks neat by filling the full area. But if you look at it on a desktop, it injects empty space underneath some pics that doesn't fit the height. I am using bunch of URLs and can feed them as a list so that this 1280X200px area on desktop fills up neat with images that are of different sizes. I tried looking into freewall and a ton of other jsfiddles and pens but couldn't achieve on how to make the whitespace removed and make that particular box look good. Thanks.
Per my comments - I would go with a flexbox solution that uses object-fit (with a polyfil for ie):
.container {
display: flex;
flex-direction: row; /* default value so optional - lines children in a row */
flex-wrap: wrap; /* allows children to wrap */
justify-content: space-between; /* space children evenly over row */
}
.responsive {
flex-basis: 25%; /* makes the width 25% */
/* if you don't want a fixed height image, I would use the padding top trick for aspect ratio divs */
position: relative;
padding-top: 30%;
}
.responsive img {
position:absolute;
display: block;
width: 100%;
height: 100%;
top:0;
left:0;
object-fit:cover;
}
#media only screen and (max-width: 700px) {
.responsive {
flex-basis: 50%;
margin: 6px 0;
}
}
#media only screen and (max-width: 500px) {
.responsive {
flex-basis: 100%;
padding-top: 50%;
}
}
<div class="container">
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/600" alt="Shiva3"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/400" alt="Shiva3"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/900" alt="Shiva3"></a>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/700" alt="Shiva3"></a>
</div>
</div>
</div>

display of blocks in responsive version

I have 6 images wrapped inside an outer div.
I'd like to see 2 images per row in the mobile version, so there should be 3 columns with 2 images per row.
I have this HTML:
* {
box-sizing: border-box;
}
img {
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
}
.picture-box {
width: 70%; /* limit screen width - max width could have been used aswell */
margin: 0 auto; /* center content */
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.ring {
padding: 10px;
text-align: center; /* Center ring div */
}
#media screen and (min-width: 1200px) {
.ring {
width: 25%;
}
}
#media screen and (max-width: 1199px) {
.ring {
width: 33.33%;
}
}
#media screen and (max-width: 768px) {
.ring {
width: 50%;
}
.picture-box {
width: 100%;
}
}
.thumb {
display: inline-block;
max-width: 200px;
padding: 10px;
border: 1px solid blue;
}
<div class="picture-box">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
Working JSFiddle Example
I tried to change flex-direction from row to column, but it did not help.
I may need to write widths for them, but do not know how and now I cannot make them into a column.
How can I solve that?
Is this what you are looking for?
I used width like you asked about in your question - this is one way to do it.
This way you can control how many boxes (.ring's) you want to show in each breakpoint.
* {
box-sizing: border-box;
}
img {
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
}
.picture-box {
width: 70%; /* limit screen width - max width could have been used aswell */
margin: 0 auto; /* center content */
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.ring {
padding: 10px;
text-align: center; /* Center ring div */
}
#media screen and (min-width: 1200px) {
.ring {
width: 25%;
}
}
#media screen and (max-width: 1199px) {
.ring {
width: 33.33%;
}
}
#media screen and (max-width: 768px) {
.ring {
width: 50%;
}
.picture-box {
width: 100%;
}
}
.thumb {
display: inline-block;
max-width: 200px;
padding: 10px;
border: 1px solid blue;
}
<div class="picture-box">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
JSFiddle example

Nested flexbox height issue in IE

I have nested flexboxes with some images inside, it looks good in Chrome, but in IE you can see the borders on the flex-item-wrapper are not flush against the bottom of the image. By the way, in the layout I will sometimes have several flex-row with many pictures.
.flex-list {
display: flex;
flex-direction: column;
}
.flex-row {
display: flex;
}
.flex-item-wrapper {
width: 100%;
border: 1px solid green;
}
.flex-item {
height: 100%;
width: 100%;
border: 1px solid blue;
}
.picture {
width: 100%;
}
<div class="flex-list">
<div class="flex-row">
<div class="flex-item-wrapper">
<div class="flex-item">
<a href='#'>
<img class="picture" src="http://www.picgifs.com/clip-art/cartoons/super-mario/clip-art-super-mario-832109.jpg" alt="">
</a>
</div>
</div>
<div class="flex-item-wrapper"></div>
<div class="flex-item-wrapper"></div>
<div class="flex-item-wrapper"></div>
</div>
</div>
This seems to be working:
.flex-row {
display: flex;
flex: 0 0 auto; /*added*/
}
or
.flex-row {
display: flex;
height: 100%; /*added*/
}
See simplified demo:
.flex-list {
display: flex;
flex-direction: column;
}
.flex-row {
display: flex;
flex: 0 0 auto;
}
.flex-item {
flex: 1;
border: 1px solid blue;
}
.picture {
width: 100%;
height: auto;
}
<div class="flex-list">
<div class="flex-row">
<div class="flex-item">
<a href='#'>
<img class="picture" src="http://www.picgifs.com/clip-art/cartoons/super-mario/clip-art-super-mario-832109.jpg" alt="">
</a>
</div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
</div>
The problem seems due to the nesting flexbox. This fixes it:
.flex-row {
width: 100%;
align-self: flex-start;
}
.flex-list {
display: flex;
flex-direction: column;
}
.flex-row {
display: flex;
width: 100%;
align-self: flex-start;
}
.flex-item-wrapper {
width: 100%;
border: 1px solid green;
}
.flex-item {
height: 100%;
width: 100%;
border: 1px solid blue;
}
.picture {
width: 100%;
}
<div>
<div class="flex-list">
<div class="flex-row">
<div class="flex-item-wrapper">
<div class="flex-item">
<a href='#'>
<img class="picture" src="http://www.picgifs.com/clip-art/cartoons/super-mario/clip-art-super-mario-832109.jpg" alt="">
</a>
</div>
</div>
<div class="flex-item-wrapper"></div>
<div class="flex-item-wrapper"></div>
<div class="flex-item-wrapper"></div>
</div>
</div>
<div></div>
</div>

Unable to add responsive flexboxes in a row

I'm trying to place 4 equal sized flex-boxes in a single row in large and medium screen
and in 2 rows (2 in each) for small screen and in a 4 rows(1 in each row) for xsmall screens.
Two of the flex boxes are embedded codepens and the other two image thumbnails.
As of now,all 4 flex boxes are not of equal sizes and appear in a column.
here is the JSFiddle.
.box {
display: flex;
flex-flow: row wrap;
justify-content: center;
align-content: center;
align-items: center;
}
.box div.A {
order: 1;
flex: 0 1 auto;
align-self: auto;
min-width: auto;
min-height: auto;
}
.box div.B {
order: 2;
flex: 0 1 auto;
align-self: auto;
min-width: auto;
min-height: auto;
}
.box div.C {
order: 3;
flex: 0 1 auto;
align-self: auto;
min-width: auto;
min-height: auto;
}
.box div.D {
order: 4;
flex: 0 1 auto;
align-self: auto;
min-width: auto;
min-height: auto;
}
<script src="https://assets.codepen.io/assets/embed/ei.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="box">
<div class="A">
<div class="thumbnail">
<p data-height="268" data-theme-id="0" data-slug-hash="VaKMBJ" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen Simple Javascript App by Rashid Naushad (#rashidnaushad) on CodePen.</p>
</div>
</div>
<div class="B">
<div class="thumbnail">
<p data-height="268" data-theme-id="0" data-slug-hash="pyPjae" data-default-tab="result" data-user="rashidnaushad" data-preview="true" class="codepen">See the Pen Tribute to Raghuram Rajan by Rashid Naushad (#rashidnaushad) on CodePen.</p>
</div>
</div>
<div class="C">
<div class="thumbnail">
<img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" />
<div class="caption">
<h5 class="text-center">Coming Soon!</h5>
</div>
</div>
</div>
<div class="D">
<div class="thumbnail">
<img class="img img-responsive img-rounded" src="http://cleantallahassee.com/wp-content/uploads/2015/09/coming-soon.jpg" alt="Coming Soon!" />
<div class="caption">
<h5 class="text-center">Coming Soon!</h5>
</div>
</div>
</div>
</div>
I would do a very simple responsive column layout with media queries. No change to your HTML.
JSFiddle
CSS:
.box {
display: block;
width: 100%;
text-align: center;
margin: 0 auto;
}
.thumbnail {
height: 278px;
}
.box div.A, .box div.B, .box div.C, .box div.D {
float: left;
}
#media (max-width: 519px) {
.box div.A, .box div.B, .box div.C, .box div.D {
width: 100%;
}
}
#media (min-width: 520px) {
.box div.A, .box div.B, .box div.C, .box div.D {
width: 50%;
}
}
#media (min-width: 820px) {
.box div.A, .box div.B, .box div.C, .box div.D {
width: 25%;
}
}