What steps do I need to follow in order to replicate the layout shown in this

I am struggling with creating a layout in CSS that resembles the one shown in this photo:

enter image description here

Here is the HTML structure I have, but I can't figure out how to style it properly.


        <ul class="list">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    

I've made some attempts at styling it, but it's not working as expected:


        .list {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-template-rows: repeat(2, auto);
            justify-content: center;
            align-items: center;
            row-gap: 50px;
            column-gap: 30px;

            > li {
                list-style: none;

                &:nth-child(4) {
                    grid-row-start: 2;
                    grid-column-start: 1;
                    grid-column-end: 1;
                }

                &:nth-child(4) {
                    grid-row-start: 2;
                    grid-column-start: 2;
                    grid-column-end: 2;
                }
            }
        }
    

Please assist me in solving this issue. Thank you!

Answer №1

Although CSS Grid is a powerful tool, I believe that for this specific situation, using flexbox would be more beneficial.

Let me elaborate on my suggestion:

  • It is essential to include flex-wrap: wrap so that the content wraps properly instead of extending in a single line.
  • I made some adjustments to the CSS styles of the li elements to achieve the desired appearance while maintaining the use of list items (such as removing bullet points, adding a gray background for better visibility, introducing spacing between items, and adjusting box-sizing to prevent excessive width due to margin). Unless there is a specific semantic requirement for using lists and list items (e.g., accessibility), it might be more suitable to use div elements instead.
  • The property flex-shrink: 0; ensures that items do not shrink to fit the container but maintain their specified size (in this case, 30% width).
.list {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

ul.list li {
  background-color: lightgray;
  box-sizing: border-box;
  padding: 1em;
  margin: 0.5em;
  list-style-type: none;
  width: 30%;
  flex-shrink: 0;
}
<ul class="list">
    <li>X</li>
    <li>X</li>
    <li>X</li>
    <li>X</li>
    <li>X</li>
</ul>

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 enabling background scrolling when Popover is displayed: (React, React Native, Native-Base)

I'm utilizing the Popover component from NativeBase to design my popover: const SettingsButton: React.FC<Props> = () => { return ( <Popover trigger={(triggerProps) => { return ( ...

What is the best way to create a four-column layout using only CSS when the widths of the columns will vary?

I am looking to create a four-column layout using only CSS, where the width of the columns will adjust responsively. To better understand my issue, take a look at the code snippet below: .row { width: 100%; display: table; } .col { display: table ...

Modifying SAS ODS contentitem in PROC SQL SELECT query

When using SAS to generate ODS output in HTML format, you have the option to include a table of contents with the "CONTENTS" and "FRAME" settings: ODS HTML PATH="C:\Path\" (url=none) BODY="body.html" CONTENTS="contents.html" ...

Transforming a Bootstrap Background Image to a Captivating Video Display

Here is the CSS code snippet that I'm having trouble with: .masthead { position: relative; width: 100%; height: auto; min-height: 35rem; padding: 15rem 0; background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0.7) 75% ...

What is the best approach to display a fluctuating price depending on specific options chosen in Next.js?

When working with 2 different select tags and rendering images based on the selection, I also want to display a price determined by the options chosen. For instance, selecting "Your Current Division" as Iron and "Your Desire League" as Bronze/Silver/Gold s ...

I'm facing challenges with setting auto heights on CSS and aligning floating divs

I'm trying to create a simple tiled list with six smaller divs inside a main div, all set to float:left. However, this is causing issues with the auto height and it doesn't seem to work properly within the div. Can anyone help me fix my code or s ...

Scrolling horizontally to display dynamic columns based on a search query in AngularJS

I am facing a challenge with implementing search functionality in a table that has multiple dynamic columns with a horizontal scrollbar. The goal is for users to be able to search for a specific column name or data, and if a match is found, the scrollbar s ...

Example of Image Slider using AngularJS

<body ng-app="myApp"> <div ng-controller="slideShowController" class="imageslide" ng-switch='slideshow' ng-animate="'animate'"> <div class="slider-content" ng-switch-when="1"> <img src="../images/ship.png" /> & ...

Struggling with Internet Explorer?

My script is working perfectly on all browsers except for Internet Explorer. The script refreshes the page to display a chat, but it seems to be causing issues in IE. Do you have any suggestions on how to make this work specifically for Internet Explorer? ...

"Utilize jQuery to target and highlight specific HTML elements when redirecting

I am working with a list of elements and I want to be able to redirect the focus to a specific element using a URL. <div id="20"></div> <div id="21"></div> <div id="22"></div> // example url http://localhost: ...

What could be causing my handle button to slide off the timeline towards the right?

I'm facing an issue with my volume bar component where the slider button is rendering outside of the timeline instead of on top of the progress bar. I need assistance in adjusting its position. // Here is the code for my volume bar component: import ...

Is it possible to apply a CSS transform without erasing the existing transform?

Looking for a solution: elem transform translateY(5px) scale(1.2) I want the element to move down an extra 5px on hover elem:hover transform translateY(5px) Is there a way to achieve this without knowing the previous state of the transform? Appr ...

What is the reason for the columns being divided on the xs-viewport in Bootstrap?

Attempting to grasp the concepts of Bootstrap has left me puzzled, as I cannot comprehend why the col-xs-11 is not aligning correctly with the col-xs-1. Despite having a total of 12 columns, the layout seems off. CSS: [class^="col-"] { height: 20px; ...

What's preventing my code from running the CSS class properly?

I'm puzzled as to why the "grow" class for image pop up is not working even though it's included in the echo script. Here is the PHP code snippet: echo "<a href='$file'><img class='grow' src='$file' alt=&a ...

Exploring ways to cycle through an array of objects during a jQuery ajax request

When making a jQuery ajax call to iterate over an API response, I am encountering the issue of receiving undefined data for all elements. Can someone guide me on how to properly iterate through an array of objects in jQuery? I am new to handling iteration ...

What is the best way to collapse the entire navigation menu after clicking a link (nav-link) within it?

I created a navigation menu using HTML and SCSS, but I'm facing an issue where the menu does not close when clicking on links inside it. The menu continues to overlay the content on the page instead of closing. Essentially, I want the navigation menu ...

Exception Regarding Security in My Java Applet

Every time I try to use an applet, I encounter the java.lang.SecurityException: Permission denied: file:////Videos/public/scripts/screenshot.jar error. Below is the applet code that I am having trouble with: <applet code="Screenshot" archive="file:/// ...

Creating a hover effect for a specific card while maintaining the rest of the cards unaffected

When hovering over a card, all icons are displayed instead of just the icons for that specific card. I want to show only the icons for the card being hovered on (example output: image). The cards are generated automatically using v-for and fetching data fr ...

Should I use a tooltip or a pop-up for the tablet version?

I'm currently working on a web application that involves presenting 10 questions, each with 3 possible answers. For each answer, I require a tooltip or popover that provides guidance. While I have attempted to use tooltips and popovers, there is an is ...

What is the best way to implement a keypress event in this code?

As a JavaScript and jQuery newbie, I have a question regarding adding a simple key press action to this code. How can I implement functionality where pressing the Right Arrow key takes me to the next image, and pressing the Left Arrow key takes me to the p ...