Tips for arranging 6 images into three columns

I'm trying to display 6 images in total, with 3 images per column that are centered and next to each other. However, when I use flexbox in my code, it messes up the layout. Any suggestions on how to achieve this?

.gallery img {
  display: flex;
  justify-content: center;
  padding: 0;
  margin: 0;
  width: 30rem;
  height: 20rem;
  opacity: 0.6;
}
<div class="gallery">
  <h1>Gallery</h1>
  <img src="images/gallery1.jpg" alt="gallery-pic1">
  <img src="images/gallery2.jpg" alt="gallery-pic2">
  <img src="images/gallery3.jpg" alt="gallery-pic3">
  <img src="images/gallery4.jpg" alt="gallery-pic4">
  <img src="images/gallery5.jpg" alt="gallery-pic5">
  <img src="images/gallery6.jpg" alt="gallery-pic6">
</div>

Answer №1

If you're wondering how to utilize flex in your layout, here's a helpful example to guide you through the process.

The key is to apply display: flex to the container housing the images to transform them into flexible items. Remember to set properties like justify-content on the container and not on the individual items unless they are also display flex containers with child elements inside.

Additionally, ensure that flex-wrap, which defaults to nowrap, is switched to

wrap</code so that everything wraps correctly. Customizing widths is crucial – for instance, setting the gallery title to 100% width and each image to 30% width can create a visually pleasing layout. To space the items apart nicely, you can use <code>justify-content: space-between
.

This sample serves as a foundation; feel free to adjust it according to your specific needs.

To delve deeper into this topic, I recommend checking out this comprehensive guide:

A Guide to Flexbox

.gallery img {
  padding: 0;
  margin: 0;
  width: 30%;
  height: 20rem;
  opacity: 0.6;
}

.gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
h1 {
  width: 100%;
}
<div class="gallery">
  <h1>Gallery</h1>
  <img src="images/gallery1.jpg" alt="gallery-pic1">
  <img src="images/gallery2.jpg" alt="gallery-pic2">
  <img src="images/gallery3.jpg" alt="gallery-pic3">
  <img src="images/gallery4.jpg" alt="gallery-pic4">
  <img src="images/gallery5.jpg" alt="gallery-pic5">
  <img src="images/gallery6.jpg" alt="gallery-pic6">
</div>

Answer №2

Implement flexbox on the primary container and grid on a secondary container specifically designated for images.

.gallery {
  display: flex;
  flex-flow: column;
  align-items: center;
}

.gallery .grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  opacity: 0.6;
}
<div class="gallery">
  <h1>Gallery</h1>
  <div class="grid">
    <img src="https://placekitten.com/150/150" alt="gallery-pic1">
    <img src="https://placekitten.com/150/150" alt="gallery-pic2">
    <img src="https://placekitten.com/150/150" alt="gallery-pic3">
    <img src="https://placekitten.com/150/150" alt="gallery-pic4">
    <img src="https://placekitten.com/150/150" alt="gallery-pic5">
    <img src="https://placekitten.com/150/150" alt="gallery-pic6">
  </div>
</div>

Answer №3

To implement this feature into your code, consider utilizing flex and flex-wrap for the parent element, while adjusting the width/min-width and flex-grow properties for the child elements:

For instance:

.gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  padding: 0;
  margin: 0 auto;
  width: 30rem;
  height: 20rem;
  opacity: 0.6;
  gap:5px;
  background:#bee
}

.gallery h1 {
  min-width: 90%;
  margin:0;
}

.gallery img {
  width: 30%;
  flex-grow: 1;
}
<div class="gallery">
  <h1>Gallery</h1>
  <img src="https://dummyimage.com/300x200&text=1.jpg" alt="gallery-pic1">
  <img src="https://dummyimage.com/300x200&text=2.jpg" alt="gallery-pic2">
  <img src="https://dummyimage.com/300x200&text=3.jpg" alt="gallery-pic3">
  <img src="https://dummyimage.com/300x200&text=4.jpg" alt="gallery-pic4">
  <img src="https://dummyimage.com/300x200&text=5.jpg" alt="gallery-pic5">
  <img src="https://dummyimage.com/300x200&text=6.jpg" alt="gallery-pic6">
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for enlarging the box width using animation

How can I create an animation that increases the width of the right side of a box from 20px to 80px by 60px when hovered over? What would be the jQuery code for achieving this effect? ...

The Facebook Like button and Twitter Follow button are not displaying in line on iOS browsers such as Chrome and Safari

When you go to: and resize your viewport to around 400px, click the down arrow next to O&O&O&O. The Twitter and Facebook buttons will be seen on the same line. However, if you repeat the same process on Safari or Chrome for iOS, you'll n ...

Print-ready guide to CSS and HTML

If I were to embark on the ambitious task of writing a book solely using HTML and CSS, where would I begin in defining a page header and footer with page numbers? How can I ensure that page breaks and margins are displayed in the browser, akin to a preview ...

Troubleshooting the z-index issue with CSS checkbox hack

Using the css "checkbox hack" to trigger the display of a form when a label is clicked. The form is styled using a :before pseudo element to create a transparent background, but the z-index is not working as expected. Despite adjusting z-index values, the ...

Is there a way to click on a button and have its background color change randomly each time?

I am facing an issue with a button on my HTML page. When the button is clicked, I want its background color to change to a random different color. However, despite trying various sources, I am unable to get it right. It seems like I am not placing the code ...

Instantly changing the color of individual words within a text area is a seamless process

Have you ever wondered how websites like jsfiddle.net are able to make specific attributes, such as those in the CSS section, different colors similar to a modern text editor? For instance, notice how the html tag is a contrasting color compared to the br ...

When I use the float:left property on a CSS div, the child divs float

The div "headerlinks" is supposed to float to the bottom left of the div "header", but sometimes it appears incorrectly positioned until I reload the page. It's unclear whether this issue is caused by my CSS or if it's a browser bug. For referen ...

Tips for looping through HTML DOM elements containing specific attributes?

I am looking to retrieve the first matching element, followed by the second, and so on, using the following CSS selector: [att] While the selectors below are not valid CSS3 selectors, they represent what I aim to achieve: [att][0] [att][1] ... ...

Tips for implementing resizable Material-UI Dialogs

I'm working on a project that involves creating a dialog box that can be resized and dragged. While I found steps in the Material-UI Dialog documentation on how to make it draggable, I'm still looking for information on how to make it resizable. ...

Using CSS to ensure the height of two floated divs matches the height of the first div

I'm struggling with the following code snippet: <div id="container"> <div id="left"> [ some contents...] </div> <div id="right"> <div id="inner"> [ some templates... ] < ...

The font style in React appears distinct from that of Bootstrap

I am looking to create a login page using both bootstrap and react. I found the perfect style on the official website. To implement this, I included all the necessary bootstrap and CSS files in my index.html file: <!DOCTYPE html> <html lang="en"& ...

Center-align text so it is perfectly centered over an image

Looking for help with aligning text in social media images? Currently, the text is positioned at the bottom of each image. Check out this example: http://jsfiddle.net/uT4Ey/ CSS: .img { background-color: red; height: 3em; width: 3em; display: inl ...

Toggle the display of slide numbers in RMarkdown xaringan / reveal.js presentations

In preparation for a workshop, I am creating HTML slides using the xaringan package in R, which is based on remark.js. These slides are made with RMarkdown, and while this approach has been effective, I have encountered a need to disable slide numbers on s ...

Sass - Trouble with Media Queries in Jekyll

My website was created using Jekyll and Sass. I attempted to make the site responsive, but for some reason, the media queries are not affecting the div class. This is my style.sass snippet: .img-link width: calc(32% - 14px) border: 2px solid whit ...

Customize Bootstrap radio buttons to resemble standard buttons with added form validation styling

Is there a way to style radio buttons to look like normal buttons while maintaining their functionality and form validation? I have two radio buttons that need styling but should still behave like radio buttons. Additionally, I am utilizing Bootstrap for s ...

When scrolling, the text or logo inside the navbar seems to dance with a slight wiggling or

I recently implemented a code to create a logo animation inside my navigation bar that expands and contracts as I scroll, inspired by the functionality of the Wall Street Journal website. However, this implementation has caused some unintended side effects ...

Tips for attaching my navigation bar to the top of the webpage

Can anyone assist me in fixing the top portion of my webpage so that it stays in place? I attempted to use the position:fixed attribute, but this caused issues as my content started overlapping with the fixed divs. You can find my website here: www.croo ...

Creating a visual representation of disk status: A step-by-step guide

I am currently working on a React page and I want to create an HTML layout for displaying disk status. It would be great if there is a reusable component available for this type of chart. What is the specific name for this kind of chart so that I can searc ...

Rotate the horizontal scroll of an inner div within an outer div using CSS

I am looking to create an outer div with horizontal scroll and inner div with vertical scroll where the inner div moves along with the outer div. <body> <div id="wrapper"> <div id="outer">content outer <div id="inner"&g ...

Ensure that div2 remains aligned with the left, bottom, and right edges of the screen, while also maintaining a consistent distance of 400px below div

Currently, I am faced with a challenge involving two divs: one positioned at the top (div1) and the other at the bottom (div2). The goal is to ensure that div2 maintains a consistent distance of 400px below div1 while also aligning its left, bottom, and r ...