Looking for advice on designing a unique Gallery layout using CSS?

Tasked with converting various layouts, some of which utilize a specific layout pattern (refer to the image below.) I'm seeking advice on the most efficient way to use CSS to display a list of images in this manner without dividing items into separate row containers. There must be a method to achieve this as a list.

The main challenge lies in properly spacing the images and aligning the far right image with the edge of the layout container (indicated by orange lines).

https://i.sstatic.net/wp2PS.gif

Ideally, I would prefer to keep the HTML structure like so:

<ul class="gallery-list">
    <li class="gallery-list-item">
        <img src="..." alt="..." />
    </li>
    <li class="gallery-list-item">
        <img src="..." alt="..." />
    </li>
</ul>

Thank you for your assistance, and if there is a superior solution available, I am open to closing this query and referring to that solution instead.

Answer №1

Consider this method as a potential solution, although it may not be the only one. It operates under certain assumptions, such as always having four images per row while ensuring responsiveness.

The key points are:

  • We adjust the width of the image container as a percentage of the parent container to dictate the number of images per row.
  • A negative right margin on the ul is used to counteract the right margin applied to the li for image spacing.
  • The calc function allows us to subtract the margin reserved for spacing so that all elements can stay on a single line without reflowing.

I have included media queries to illustrate how you can alter the row count for smaller screens and ensure a responsive grid layout.

In addition, some padding and borders have been added to .container to demonstrate that the images occupy the full width of the container.

This represents just one approach among many possible solutions.

body {
  margin: 0;
}
ul,
li {
  margin: 0;
  padding: 0;
  list-style: none;
}
.container {
  width: 80%;
  margin: 50px auto;
  /* additional styling for demonstration purposes */
  padding: 50px 0;
  border-left: 1px solid #CCC;
  border-right: 1px solid #CCC;
  overflow: hidden;
}
ul {
  margin-right: -5px;
}
li {
  float: left;
  margin: 0 5px 5px 0;
  width: calc(50% - 5px );
}
img {
  display: block;
  width: 100%; /* or max-width: 100%; */
  height: auto;
}
@media(min-width: 550px) {
  li {
    width: calc(33.333% - 5px );
  }
}
@media(min-width: 750px) {
  li {
    width: calc(25% - 5px );
  }
}
<div class="container">
  <ul>
    <li>
      <img src="http://placehold.it/200x200/fc0/&text=1">
    </li>
    <li>
      <img src="http://placehold.it/200x200/ccc/&text=2">
    </li>
    <li>
      <img src="http://placehold.it/200x200/fc0/&text=3">
    </li>
    <li>
      <img src="http://placehold.it/200x200/ccc/&text=4">
    </li>
    <li>
      <img src="http://placehold.it/200x200/fc0/&text=5">
    </li>
    <li>
      <img src="http://placehold.it/200x200/ccc/&text=6">
    </li>
    <li>
      <img src="http://placehold.it/200x200/fc0/&text=7">
    </li>
    <li>
      <img src="http://placehold.it/200x200/ccc/&text=8">
    </li>
  </ul>
</div>

Answer №2

If you're looking for a solution, I recommend using flexbox. You can set a fixed width on the gallery, but it can also be fluid. Here's a snippet of code to get you started:

.gallery {
  display: flex;
  flex-wrap: wrap;
  width: 440px;
  margin: auto;
  padding: 0;
  list-style: none;
}


.gallery img {
  margin-right: 10px;
  margin-bottom: 10px;
}
<ul class="gallery">
  <li>
     // Add your image sources here
  </li>

  <li>
    // Add more image sources here
  </li>

  <li>
     // And keep adding image sources
  </li>
</ul>

Answer №3

You can achieve the desired layout using display:flex and flex-wrap.

I made some changes to the markup structure, replacing ul li's with div and img elements. The last image in each row now expands to fit the container.

Take a look at this code snippet:

.gallery-list-item {
  display: flex;
  width: 100%;
  margin-top: 10px;
}
div img:not(:first-child) {
  margin-left: 10px;
}
div.gallery-list-item img:last-child {
  flex: 1;
}
div.gallery-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: 450px;
  border: 1px solid black;
  padding: 0;
}
<div class="gallery-list">
  <div class="gallery-list-item">
    <img src="http://shushi168.com/data/out/123/36699123-images.png" alt="..." height=100 width=100/>


    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>

    <img src="http://shushi168.com/data/out/123/36699123-images.png" alt="..." height=100 width=100/>

    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
  </div>
  <div class="gallery-list-item">
    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>

    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>

    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>

    <img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg" alt="..." height=100 width=100/>
  </div>

</div>

I hope this solution helps!

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

Trouble encountered with bootstrap toggling when multiple identical inputs are used

Problem with Bootstrap toggle not working on multiple identical inputs unless the first input is pressed. Even then, not all inputs are checked/unchecked. I've created a small example I want to ensure that when one input is toggled, all others have ...

Incorporate JavaScript functionality with HTML dropdown lists

I am attempting to achieve the following: The user can choose between "Option One" or "Option Two". If they select "Option One", the result will be 66 + 45, and if they select "Option Two", the result will be 35 + 45. How can I make this work using a com ...

Tips for aligning a div within a flexbox to the bottom position

I'm having trouble aligning the div at the bottom of the flex box. I attempted to use align-content: flex-end; but it didn't work. Is there a way to keep the <.div class="parameters"> at the bottom of the row? Especially when the ...

Is it possible to enhance the GamepadAPI's functionality?

I've been working on enhancing the built-in GamepadAPI by adding custom controller code. With TypeScript, I created a basic function to trigger a "gamepadconnected" event. // emulate gamepadconnected event function dispatchGamepadConnectedEv ...

"Facing a dilemma with Javascript's Facebox getElementById function - not fetching any

Utilizing facebox, I am initiating a small modal dialog with a form and attempting to retrieve the value from a text field on that form using javascript. Below is the HTML code: <div id="dialog-form95" style="display:none"> <div class="block"> ...

AJAX Username verification failing to validate

Working with PHP and MySQL, I created a basic form which includes a textfield for the username and a submit button. The page is named checkusername.php. Additionally, I implemented an AJAX function for handling this process. Subsequently, there is another ...

Conceal navigation options on smaller screens and enable drop-down menu functionality with Bootstrap 3

With the usage of Bootstrap 3, I have successfully hidden two menu items when the screen is resized. However, these items still appear in the drop down menu that appears on the top left (with three bars) when the screen size is reduced. Is there a way to k ...

Animate linear-gradient using jQuery script

I have been searching for a circular progress bar plugin but haven't found one that suits my needs, so I decided to build my own using pure CSS: http://jsfiddle.net/9RFkw/ While it looks great, there are two issues I am facing: 1.) When at 25% in F ...

Determine the number of elements chosen within a complex JSON structure

I need to figure out how to count the length of a jsonArray, but I'm stuck. Here's an example to start with: https://jsfiddle.net/vuqcopm7/13/ In summary, if you click on an item in the list, such as "asd1", it will create an input text every t ...

Achieving opacity solely on the background within a div class can be accomplished by utilizing CSS properties such

Whenever I adjust the opacity of my main-body div class, it results in all elements within also becoming transparent. Ideally, I would like only the gray background to have opacity. See below for the code snippet: <div class="main1"> content </di ...

The code written inside the <script> tag seems to be malfunctioning when placed in the component.html file within VScode while working with

I'm facing an issue where the script tag for jQuery included in index.html is not being executed within the component.html. There are no errors detected when inspected. Additionally, I have used another script for a toast message box. <script sr ...

Struggling to apply size in percentage to several images used as a background

Displaying two background images with different positioning: html { background: top left no-repeat url(http://wallpapers.pupazzo.org/animals/Peek-a-Boo_Red_Fox_Kit.jpg), top right no-repeat url(http://www.dcwild.com/images/Fox-Kit-Rive ...

Automated line wrapping within a div

My goal is to showcase images within a unique div structure. Currently, the arrangement appears as follows: It seems that the green divs are extending beyond their parent div (the black one). This is how it's structured: <div class="photoView c ...

When the tailwind utilities are implemented, they do not appear on the screen

Recently, I embarked on a project using Tailwindcss/Next.js and encountered an issue when trying to apply a box-like shadow to a button with a new utility in pure CSS. Despite my efforts, the changes don't seem to take effect. Can you spot what I migh ...

Center elements at % on the webpage using CSS styling

When creating two bars at 70% and 100%, I also want a small triangular shape to point specifically at the 70% mark. Here is how I am currently drawing the triangle: .arrowUp { width: 0; height: 0; border-left: 10px solid transparent; border-right ...

What is the best way to specify attributes such as font size and padding for the mobile version in AMP for email?

I attempted to utilize media queries to create a responsive AMP email, but it seems that this feature is not supported in AMP. However, I discovered that you can define media="(max-width: 599px)" and media="(min-width: 600px)" for </amp-img>, but the ...

Execute a JavaScript function on a Node server following a click event in an HTML document

Hello everyone, I am currently working on a project using node.js in a Windows environment. With the help of the express module, I am trying to create a static page that includes a Submit form. When someone clicks the "Submit" button, I intend to execute a ...

Unable to create follow/unfollow feature with jquery ajax

Issue: While the database part for following and unfollowing actions is functioning correctly, there seems to be a problem with the jQuery and Ajax section. The follow button changes to unfollow (with some CSS styling) only after refreshing the page, rathe ...

Utilizing Multiple Carousels on a Single Bootstrap 5 Page

I am facing a challenge in making multiple Bootstrap carousels work correctly on a single page. According to Bootstrap guidelines, each carousel should have a unique ID. However, I am struggling to implement this and only one carousel functions properly ...

When the enter key is pressed, the form will be submitted and the results will be displayed in a modal window without

Behold, my unique form! It lacks the traditional <form></form> tags. <input id="query" name="query" style="font-size: 18pt" id="text" data-email="required" type="text" placeholder="Search Here from <?php echo $row['no']."+"; ?& ...