Div elements do not wrap on the next line - html

I defined flexbox properties of container display: flex; flex-wrap: wrap; justify-content: center; but there are always 3 divs on the first row and 2 divs on the second. How to make it wrap divs when the browser resizes?
I tried almost everything (changed the width and height of parent container, changed width to min-width/max-width, set margin of parent container margin: 0 auto;).
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="..." class="image">
<p>Fall Berry Blitz Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Spiced Rum Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Seasonal Donuts</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Myrtle Ave Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>
<!-- URL of images is correct -->
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
width: 1000px;
margin: 0 auto;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
I expect the divs would wrap on next line. But there always are 3 divs on first and 2 divs on second line.

Your HTML contained some invalid closure tags, please validate your HTML you can check out: https://validator.w3.org/
Also removed the fixed width of 1000px, you want to have a fluid parent so it sizes acording to the device or browser width.
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 0 auto;
/* changed */
width: 100%;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Fall Berry Blitz Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Spiced Rum Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Seasonal Donuts</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Myrtle Ave Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>

It happens because you set width:1000px on your .parent. This means that no matter the device size the parent will always be 1000px and there will always be 3 elements on the first row.
To solve this, set .parent {width: 100%; max-width: 1000px;}
cheers

Related

CSS Image Grid with hover effect to each image

I am trying to create a simple image grid and I would like to add hover effects to each image. I would like the effect to be a transition to a different image. so far I have the image grid worked out properly but when it comes to adding a hover effect its either effecting my entire column by making images disappear or the image I am using for the transition covers the entire grid. Any help with this would be greatly appreciated.
Here's the code that I've been working with so far
* {
box-sizing: border-box;
}
.row {
display: -ms-flexbox; /* IE10 */
display: flex;
-ms-flex-wrap: wrap; /* IE10 */
flex-wrap: wrap;
padding: 0 4px;
}
/* Create four equal columns that sits next to each other */
.column {
-ms-flex: 20%; /* IE10 */
flex: 20%;
max-width: 20%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
.image {
display: block;
width: 100%;
height: auto;
}
/* Responsive layout - makes a two column-layout instead of four columns */
#media screen and (max-width: 800px) {
.column {
-ms-flex: 50%;
flex: 50%;
max-width: 50%;
}
}
/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.column {
-ms-flex: 100%;
flex: 100%;
max-width: 100%;
}
}
<!-- Header -->
<div class="header">
</div>
<!-- Photo Grid -->
<div class="row">
<div class="column">
<img src="https://drive.google.com/thumbnail?id=1BiM6pgdVUC8H7A-pO9YMD74jwcvh8uKw" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1TBfdXETiX00bNkSxTkeePnAJb_TE8KMz" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1jN0LLKTg7H1j0PQ1estEOSgbxN8IaUhX" style="width:100%">
</div>
<div class="column">
<img src="https://drive.google.com/thumbnail?id=1XI0X_JIsOrpR6AjKxFR2MHdu7KqmbKDf" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1iSMUDDa3_NQF3FaDw_L0J3b6EypDUlRn" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1HUhl-gkKjZO2APycCl0Z5Bhm_EWcNoWo" style="width:100%">
</div>
<div class="column">
<img src="https://drive.google.com/thumbnail?id=1-g11eGfBGMtng5eW7MSBTmVjGEc7X3KT" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1GIssifV2o9YbcM-b_LDe0AwkBqb7WHkE" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1vdVGVohUrDrkjoOHAi2QFwLDFypdkRR8" style="width:100%">
</div>
<div class="column">
<img src="https://drive.google.com/thumbnail?id=1veQWJj6J3_NW48y4-_cTr2PeO9mhcktm" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1OFAR6Dh3vMJ7DQnkm4TVky3iBDada5iY" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1XRwvxsxI8N32HT-XETAxt82j6EA30jF2" style="width:100%">
</div>
<div class="column">
<img src="https://drive.google.com/thumbnail?id=1J1ctFtgHtd0G5gddcZzsSuM-eUpiWsOQ" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1-oD6Rc5q5Xzq6Qt-FJdfh7pMZo165m3Z" style="width:100%">
<img src="https://drive.google.com/thumbnail?id=1I-zlWhtolgyH_Ewr3F239maUizVjtJRM" style="width:100%">
</div>
</div>
The CSS transition property will help you to achieve this.
You can also use CSS grid for creating the Grid Layout
Checkout the sample code.
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
grid-gap: 1rem;
justify-content: center;
}
.cell {
width: auto;
height: 220px;
overflow: hidden;
position: relative;
display: flex;
}
.content {
width: 100%;
height:100%;
position: absolute;
top: 0px;
left: 0px;
opacity: 1;
transition: 0.5s ease;
}
.cell .content:last-child:hover {
opacity: 0;
}
.content>img{
width:100%;
}
<div class="grid">
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1BiM6pgdVUC8H7A-pO9YMD74jwcvh8uKw" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1TBfdXETiX00bNkSxTkeePnAJb_TE8KMz" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1jN0LLKTg7H1j0PQ1estEOSgbxN8IaUhX" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1XI0X_JIsOrpR6AjKxFR2MHdu7KqmbKDf" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1iSMUDDa3_NQF3FaDw_L0J3b6EypDUlRn" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1HUhl-gkKjZO2APycCl0Z5Bhm_EWcNoWo" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1-g11eGfBGMtng5eW7MSBTmVjGEc7X3KT" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1GIssifV2o9YbcM-b_LDe0AwkBqb7WHkE" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1BiM6pgdVUC8H7A-pO9YMD74jwcvh8uKw" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1TBfdXETiX00bNkSxTkeePnAJb_TE8KMz" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1jN0LLKTg7H1j0PQ1estEOSgbxN8IaUhX" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1XI0X_JIsOrpR6AjKxFR2MHdu7KqmbKDf" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1iSMUDDa3_NQF3FaDw_L0J3b6EypDUlRn" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1HUhl-gkKjZO2APycCl0Z5Bhm_EWcNoWo" />
</div>
</div>
<div class="cell">
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1-g11eGfBGMtng5eW7MSBTmVjGEc7X3KT" />
</div>
<div class="content">
<img src="https://drive.google.com/thumbnail?id=1GIssifV2o9YbcM-b_LDe0AwkBqb7WHkE" />
</div>
</div>
</div>

How to put a header in between of two images on the same line (using the <div> blocks)?

I am trying to create a webpage for an imaginary pizzeria as practice. I want to insert the header of the webpage in between two images(same) such that the three(image, header, image) elements are in line. But with the code below I am getting a screen like this. How do I get them all on one line?
<body>
<div style='float:left'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<div style='float:right'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<hr style='clear:both;' />
</body>
This can be done using display: flex and justify-content: space-between:
.header {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
<div class="header">
<div>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<div>
<img src='body.png' style='width:100px; height:100px;'>
</div>
</div>
I've added a container div and given it the class header and applied the flexbox to it.
You can simply do this :
header {
text-align: center;
}
h1 {
font-size: 25px;
display: inline-block;
vertical-align: top;
margin-top: 25px;
}
img {
display: inline-block;
width: 100px;
height: 100px;
margin:0 10px;
}
<header>
<img src='https://lorempixel.com/100/100/'>
<h1>Ralph's Pizzeria</h1>
<img src='https://lorempixel.com/100/100/'>
</header>
<hr>
Here's another solution if you want to continue to work with float.
Simply move the 2nd image to before the title.
<body>
<div style='float:left'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='float:right'> <!-- Just moved this up here -->
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<hr style='clear:both;'/>
</body>
However, I suggest using flex-box if you don't have to support older browsers.

Using flexbox, image overflow throughout container

How to make my imgs not to overflow outside its container <div class="display"> and perfectly make a square/box?
It should've work for flexbox.
As you can see, this snippets resulting 3 images with 1 outside which not properly a box.
.display {
min-width: 500px;
max-width: 500px;
}
.scenery {
display: flex;
flex-direction: column;
}
.scenery_1 {
display: flex;
}
.scenery_2 {
display: flex;
}
<div class="display">
<div class="scenery">
<div class="scenery_1">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
<div class="scenery_2">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
</div>
</div>
I'm not sure why you're using display: flex, but this works:
.display {
min-width: 500px;
max-width: 100%;
display: block; /* Add this, remove everything else*/
}
<div class="display">
<div class="scenery">
<div class="scenery_1">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
<div class="scenery_2">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
</div>
</div>

how to place two divs side by side

How can I place 2 divs side by side, both which have images. I want the divs to remain side by side and the images auto size ,with screen size.When I reduce the size of the screen the images re-position themselves one below the other.
How will be the css for the below html?
<div class="container">
<div class="wrapper">
<div class="left">
<img src="img1.png" />
</div>
<div class="right">
<img src="img2.png" />
</div>
</div>
</div>
You can use css float to get contained elements to sit either side
.container {
width: 100%;
}
.left, .right {
width: 50%;
float: left;
}
<div class="container">
<div class="left"><img src='http://placehold.it/350x150'/></div>
<div class="right"><img src='http://placehold.it/350x150'/></div>
</div>
Edit: You can use Flex-Box
.container {
width: 100vw;
display: inline-flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
align-content: space-around;
}
.container > img {
align-self: auto;
margin: 4px;
}
<div class="container">
<img src='http://placehold.it/350x150'/>
<img src='http://placehold.it/350x150'/>
</div>
Try using bootstrap grid's. It can be something like this.
<div class="col-sm-12">
<div class="col-sm-6">
<img src="http://www.thebrandbite.com/wp-content/media/2015/07/apple-7.jpg" width="30%">
</div>
<div class="col-sm-6">
<img src="https://d3nevzfk7ii3be.cloudfront.net/igi/KRLMkuaBjm5mKDDP" width="30%">
</div>
</div>
Demo

How to create a grid of images using flexbox that fully wraps around a main image?

I want to create a grid of images that first has a large featured image, then to it's right, has a grid of 4 (2 on each row).. and then underneath that, rows of 4 images at a time.
Loosely based on this design.
I think flexbox would probably be able to nail this one.
Say I have some markup like this
<div class="image-grid">
<div>
<img src="https://unsplash.it/1024/1024">
</div>
<div>
<img src="https://unsplash.it/1000/800">
</div>
<div>
<img src="https://unsplash.it/1100/1000">
</div>
<div>
<img src="https://unsplash.it/1120/1000">
</div>
<div>
<img src="https://unsplash.it/1130/1024">
</div>
<div>
<img src="https://unsplash.it/1101/1024">
</div>
<div>
<img src="https://unsplash.it/1020/1024">
</div>
<div>
<img src="https://unsplash.it/1021/1024">
</div>
<div>
<img src="https://unsplash.it/1002/1024">
</div>
<div>
<img src="https://unsplash.it/1003/1024">
</div>
<div>
<img src="https://unsplash.it/1004/1024">
</div>
<div>
<img src="https://unsplash.it/1005/1024">
</div>
</div>
with CSS
.image-grid {
display: flex;
flex-wrap: wrap;
}
img {
width: 100%;
}
.image-grid > div:first-child {
flex-basis: 50%;
}
.image-grid > div {
flex-basis: 25%;
}
this almost does what I need it to. Codepen here.
I need two things fixed though...
display 4 images to the right of the featured image, instead of the current 2.
stretch each image to fit it's space so it's a tight grid (no spacing around any image). I'm thinking of using the object-fit CSS property but I haven't got it working yet.
Thank you.
So I figured the easiest way would be to use a framework like Foundation to create the top row, and then use flexbox for the rest.
<div class="row">
<div class="medium-6 columns featured-image">
<img src="https://unsplash.it/1023/1024">
</div>
<div class="medium-6 columns featured-image-grid">
<div class="row">
<div class="medium-6 columns">
<img src="https://unsplash.it/1024/1024">
</div>
<div class="medium-6 columns">
<img src="https://unsplash.it/1022/1024">
</div>
</div>
<div class="row">
<div class="medium-6 columns">
<img src="https://unsplash.it/1021/1024">
</div>
<div class="medium-6 columns">
<img src="https://unsplash.it/1020/1024">
</div>
</div>
</div>
</div>
<div class="image-grid">
<img src="https://unsplash.it/1020/1024">
<img src="https://unsplash.it/1020/1025">
<img src="https://unsplash.it/1020/1022">
<img src="https://unsplash.it/1020/1021">
<img src="https://unsplash.it/1020/1029">
<img src="https://unsplash.it/1020/1028">
<img src="https://unsplash.it/1020/1027">
<img src="https://unsplash.it/1020/1023">
<img src="https://unsplash.it/1020/1024">
<img src="https://unsplash.it/1020/1025">
</div>
with the CSS
.row {
max-width: 100%;
margin: 0 !important;
}
.columns {
padding: 0;
}
.featured-image img {
height: 400px;
}
.featured-image-grid img {
height: 200px;
}
img {
object-fit: cover;
width: 100%;
}
.image-grid {
display: flex;
flex-wrap: wrap;
}
.image-grid > img {
flex-basis: 25%;
height: 200px;
width: auto;
}
Codepen here.
It needs some work to make it responsive, but it works for what I needed it to do.
.image-grid {
display: flex;
flex-wrap: wrap;
flex-direction: column;
width: 200px;
height: 100px;
}
.image-grid, .featured > img:first-child {
width: 100px;
height:100px;
}
.image-grid > img {
width: 50px;
height:50px;
}
<div class="image-grid featured">
<img src="https://unsplash.it/1024/1024">
<img src="https://unsplash.it/1000/800">
<img src="https://unsplash.it/1100/1000">
<img src="https://unsplash.it/1120/1000">
<img src="https://unsplash.it/1130/1024">
</div>
<div class="image-grid">
<img src="https://unsplash.it/1101/1024">
<img src="https://unsplash.it/1020/1024">
<img src="https://unsplash.it/1021/1024">
<img src="https://unsplash.it/1002/1024">
<img src="https://unsplash.it/1003/1024">
<img src="https://unsplash.it/1004/1024">
<img src="https://unsplash.it/1005/1024">
</div>