Flexbox Gallery With Wrapping - html

I am trying to create a responsive image gallery for my website using a flexbox. However, I am trying to make it wrap to the next line and stay in order, which I am having trouble with. For example:
Desktop
1,2,3
4,5,6 etc
Tablet
1,2
3,4 etc
Mobile
1
2 etc
Please see the example code I included.
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex: 33%;
max-width: 33%;
}
.column img {
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%;
}
}
<div class="row">
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 1</figcaption>
</figure>
<figure>
<img src="https://img.webnots.com/2014/04/42.png">
<figcaption>Caption 4</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/22.png">
<figcaption>Caption 2</figcaption>
</figure>
<figure>
<img src="https://img.webnots.com/2014/04/52.png">
<figcaption>Caption 5</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/32.png">
<figcaption>Caption 3</figcaption>
</figure>
<figure>
<img src="https://img.webnots.com/2014/04/62.png">
<figcaption>Caption 6</figcaption>
</figure>
</div>
</div>

The first thing I would do is nest it in a container. Flex works in order of the markup so I switched your order back to the correct numerical order. Then, what I would do is nest each of your figure's in their own div. I just used your column div to demonstrate. Then remove the media queries and let flex do its thing.
The main issue was you were trying to split them up in rows of 3 but had 2 nested per div, and were trying to set max-width to the div. To have a row of 3 they should be nested in their own div's with width: 33%;
EDIT ~ I made the min-width: 200px;because your image renders at about 160px. 200px would be a good breaking point on mobile devices. Check your dev tools for mobile.
.row {
display: flex;
flex-wrap: wrap;
}
.column {
min-width: 200px;
width: 33.33%;
}
.container {
margin: auto;
width: 100%;
}
<div class="container">
<div class="row">
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 1</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/42.png">
<figcaption>Caption 2</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/22.png">
<figcaption>Caption 3</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/52.png">
<figcaption>Caption 4</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/32.png">
<figcaption>Caption 5</figcaption>
</figure>
</div>
<div class="column">
<figure>
<img src="https://img.webnots.com/2014/04/62.png">
<figcaption>Caption 6</figcaption>
</figure>
</div>
</div>
</div>

Your HTML elements are set up incorrectly. To achieve what you want, you shouldn't put in the same element "first" and "fourth". While it looks good on desktop, you won't be able to put element "second" or "third" between them. You will only be able to change the position of those elements inside of the . Very basic HTML and CSS stuff.
I would advise you to rename class "column" to "cell" and put them in order:
<div class="row">
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 1</figcaption>
</figure>
</div>
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 2</figcaption>
</figure>
</div>
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 3</figcaption>
</figure>
</div>
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 4</figcaption>
</figure>
</div>
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 5</figcaption>
</figure>
</div>
<div class="cell">
<figure>
<img src="https://img.webnots.com/2014/04/112.png">
<figcaption>Caption 6</figcaption>
</figure>
</div>
</div>
And of course in your CSS file change the .column to .cell
This way you will achieve your wanted responsive look on tablet and mobile.

Related

I can't get my images to display in a single row on my website using HTML and CSS

Having trouble getting my images to display in a single row on larger screen sizes. This is for a simple bioSite project that I am working on.
<body>
<div class="image-gallery">
<div class="container">
<figure>
<img src="mattdee.jpg"width = "220" height = "220">
<figcaption>Matthew & his wife Deanna</figcaption>
</figure>
<figure>
<img src="mattzane.jpg"width = "220" height = "220">
<figcaption>Matthew & his son Zane</figcaption>
</figure>
<figure>
<img src="mattcarolinebiopic.jpg"width = "220" height = "220">
<figcaption>Matthew & his daughter Caroline</figcaption>
</figure>
</div>
<div class= "container">
<p>The things I love most in this life are my family,
being creative and making music,
and my profession of engineering.</p>
</div>
</div>
</body>
.image-gallery{
padding-left: 7%;
border: 3px solid black;
}
Changing the container to multiple divs for rows and columns allows you to use flex display property. This solution worked for me heres an image of the result.
<style>
.image-gallery{
padding-left: 7%;
border: 3px solid black;
}
.row {
display: flex;
}
.column {
flex: 33.33%; /* 1 / Number of Images */
padding: 5px;
}
</style>
<body>
<div class="image-gallery">
<div class="row">
<div class="column">
<figure width="100%">
<img src="img1.jpg"width = "220" height = "220">
<figcaption>Caption 1</figcaption>
</figure>
</div>
<div class="column">
<figure width="100%">
<img src="img2.jpg"width = "220" height = "220">
<figcaption>Caption 2</figcaption>
</figure>
</div>
<div class="column">
<figure width="100%">
<img src="img3.jpg"width = "220" height = "220">
<figcaption>Caption 3</figcaption>
</figure>
</div>
</div>
<p>The things I love most in this life are my family, being creative and making music, and my profession of engineering.</p>
</div>
</body>

jpg images display trouble

I have a very simple HTML / CSS image grid. All photos are different and are inside a <figure> tag. When the images are in jpg file format, when refreshing the page sometimes some of them are repeated or appear distorted, with another ghost image on top or dissappear. This only happens with jpg, not png. Why is this? Is there a solution?
ul { padding: 0; margin: 0; }
figure { margin: 0; }
.grid-item {
float: left;
width: 33%;
}
.tm-img {
max-width: 320px;
width: 100%;
height: auto;
border: none;
margin: 0 auto;
}
.grid-item figure {
position: relative;
overflow: hidden;
text-align: center;
}
.grid-item figure img {
display: block;
max-width: 100%;
}
<ul>
<li>
<div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/01.jpg" alt="Image 01" class="tm-img">
</figure>
</div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/02.jpg" alt="Image 02" class="tm-img">
</figure>
</div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/03.jpg" alt="Image 03" class="tm-img">
</figure>
</div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/04.jpg" alt="Image 04" class="tm-img">
</figure>
</div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/05.jpg" alt="Image 05" class="tm-img">
</figure>
</div>
<div class="grid-item">
<figure>
<img src="https://stupefied-fermi-29b354.netlify.app/img/06.jpg" alt="Image 06" class="tm-img">
</figure>
</div>
</div>
</li>
</ul>
Image repeated
Image disappeared
This is the sample netlify html page
Animation
Refreshing the page multiple times shows the issue.

Unique Figcaption Changes Image Size in Grid Layout

While testing the desktop version of a template I am creating in Chrome I noticed when my figcaptions are all the same (example: Caption) or similar (example: Caption One... Caption Two) my images and caption line up fine. Once I make them unique the images start to change sizes and nothing lines up correctly. I'm not adding any sort of crazy captions, just simple one or two word descriptions like "Hello World" or "Example Caption". I have ran it through dev tools and looked at a few other posts on stack overflow about issues concerning figcaptions but noting seems to be working. All of my images are the same size. I'm sure it is something simple that I am just overlooking but I would appreciate a new set of eyes at this point. Thanks in advance for your time and assistance.
* {
margin: 0;
padding: 0;
}
.content-section {
display: grid;
grid-template-columns: auto auto auto;
}
figure img {
width: 100%;
height: auto;
}
<!--lines up correctly-->
<section class="content-section">
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
</section>
<!--lines up incorrectly-->
<section class="content-section">
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Some Words</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Hello World</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Something Else</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Caption Five</figcaption>
</figure>
<figure>
<img src="http://via.placeholder.com/640x360" alt="">
<figcaption>Example</figcaption>
</figure>
</section>
and answering my own question...
.content-section {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Lines up perfect

HTML / CSS Layout - display 2 images vertically in 1 figure

This is a follow-up question from this post:
HTML / CSS How do I force images to horizontally display
The code below will display 6 figures. Each figure contains a single image with a caption. The figures are laid out 3 horizontally in 2 vertical groups. Now I want to add an additional image to each of my figures directly below (vertical) the first image. Rest of the layout should remain the same. How do I do that? (I added the extra example image in each figure, but it displays horizontally instead of vertically.)
.group {
white-space: nowrap;
}
.group div {
display: inline-block;
}
<div class="group">
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image</figcaption>
</figure>
</div>
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image2</figcaption>
</figure>
</div>
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image3</figcaption>
</figure>
</div>
</div>
<div class="group">
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image</figcaption>
</figure>
</div>
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image2</figcaption>
</figure>
</div>
<div>
<figure>
<img src="example.jpg"/>
<img src="example2.jpg"/>
<figcaption>image3</figcaption>
</figure>
</div>
</div>
Simply put a <br> between the two images, i.e.
<img src="example.jpg"/><br>
<img src="example2.jpg"/>
<figcaption>image</figcaption>

Alternating block and inline behavior with div elements?

So I have a table that I'm trying to make. Essentially, it will be two rows of three images each, with very short text below each one.
My structure looks like this:
<div class="mission-statement-1">
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
</div>
<div class="mission-statement-2">
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
<div class="iconDiv">
<img src="img.jpg" />
<span>Some text</span>
</div>
</div>
I have the two main div elements with display: block with a width of 100%, so that they'd be one under the other. The inner divs are display: inline, so that they would be next to each other, and are given a width of 30%.
If I have just images alone, this works as intended. However, I'm trying to have the text centered underneath each image, and adding text will mess up the intended layout. If I use a block text element, such as a p, then I'll have six rows of one item each. If I stick with the span, then the text comes after the image (as it should given its inline property). I can't seem to make it work the way that I want it to.
Any help?
Try giving your inner divs display: inline-block;.
inline-block elements maintain the support of block styles, such as defined width and height, but can be rendered next to other objects like inline elements.
.mission-statement-1,
.mission-statement-2{
display:block;
}
.iconDiv{
display:inline-block;
text-align:center;
}
img{
background-color:green;
width:100%;
height:100%;
}
<div class="mission-statement-1">
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock">
<figcaption>Some text1</figcaption>
</figure>
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock">
<figcaption>Some text2</figcaption>
</figure>
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock" >
<figcaption>Some text3</figcaption>
</figure>
</div>
<div class="mission-statement-2">
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock" >
<figcaption>Some text4</figcaption>
</figure>
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock">
<figcaption>Some text5</figcaption>
</figure>
<figure class="iconDiv">
<img src="img_pulpit.jpg" alt="The Pulpit Rock" >
<figcaption>Some text6</figcaption>
</figure>
</div>
You can Html figure and figcaption tags.
Try using Flex with some CSS. It'll also work as a better way to have a responsive table that's mobile friendly. Also, use the tags Figure and FigCaption which will basically glue the text to the images.
.parent-wrapper {
height:100%;
width:100%;
}
.parent {
display: flex;
flex-wrap: nowrap;
margin:-10px 0 0 -10px;
}
.child {
display: inline-block;
margin:10px 0 0 10px;
flex-grow: 1;
height:150px;
}
<body>
<div class="parent-wrapper">
<div class="parent">
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
</div>
<div class="parent">
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
<div class="child"><figure><img src="http://media.nbcwashington.com/designimages/ots_dark_wx_103.png" /><figcaption>Some text</figcaption>
</figure></div>
</div>
</div>
</body>
Here is an example about flex, and breaking point that can set to let content wrap if need. You can inspire yourself from it and dig into flex here
The snippet below uses figure and figcaption, HTML tags dedicated to this kind of content, and flex and flex-wrap for the CSS part.
min or max-width can be used to enhance your styling and set some basic breaking points unless you want also or only use media queries.
.imgcpt {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
figure {
flex: 1 1 30%;
margin: 5px;
border: solid;
min-width: 300px;
text-align: center;
}
figure img {
width: 100%;
max-width: 800px;
}
figcaption>* {
margin: 0.25em
}
<div class="imgcpt">
<figure>
<img src="http://lorempixel.com/200/100/" alt="i show ..." />
<figcaption>
<h1>title</h1>
<p>Or just simple text caption</p>
<p>Or just simple text caption</p>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/201/100/" alt="i show ..." />
<figcaption>
<h1>title</h1>
<p>Or just simple text caption</p>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/202/102/" alt="i show ..." />
<figcaption>
<p>Or just simple text caption</p>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/203/103/" alt="i show ..." />
<figcaption>
<h1>title</h1>
<p>Or just simple text caption</p>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/204/104/" alt="i show ..." />
<figcaption>
<h1>title</h1>
<p>Or just simple text caption</p>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/205/105/" alt="i show ..." />
<figcaption>
<h1>Single title</h1>
</figcaption>
</figure>
<figure>
<img src="http://lorempixel.com/206/106/" alt="i show ..." />
<figcaption>
<h1>title</h1>
<p>Or just simple text caption</p>
</figcaption>
</figure>
</div>
Run snippet in fullpage or play with it and edit it # http://codepen.io/gc-nomade/pen/dvoQyR
Not so much CSS has to be used when using flex.