What are some techniques for creating a distinct layout using a list of items with CSS grid?

My goal is to create a unique layout for a list of posts using an array. The layout involves different columns rather than simply wrapping them into rows outside of the while loop.

I have almost everything working except for the section inside the red border. Any assistance with this particular section would be greatly appreciated.

https://i.sstatic.net/3WgaX.png

This is the desired layout that I am striving for:

In the following code snippet, you can see the CSS layout I'm trying to implement:

[CSS code here...]

The HTML structure for displaying the posts looks like this:

<ul class="posts">
    <li>
        <div class="featured-image"><img src="image-url"></div>
        <div class="description">
            <h2>Title</h2>
            <p>Description</p>
        </div>
    </li>
    [Repeat for each post...]
</ul>

Answer №1

You were missing the grid-column property, please review the snippet provided.

ul.posts {
  display: grid;
  padding-left: 0px;
  grid-template-rows: repeat(auto, 1fr);
  grid-template-columns: repeat(12, 1fr);
  justify-content: between;
  grid-gap: 10px;
}

ul.posts li {
  list-style-type: none;
}

/* Grid areas defined for specific items */
ul.posts li:nth-child(1) {
  grid-area: "g1";
  grid-row: 1/2;
  grid-column: 1/13;
}
.
. (Code continues)
.
.
<ul class="posts">
  <li>
    <div class="featured-image"><img src="https://via.placeholder.com/600x300"></div>
    <div class="description">
      <h2>Title 1</h2>
      <p>Description</p>
    </div>
  </li>
  
  /* Additional list items with featured images and descriptions*/

Answer №2

For a similar layout to the image linked below, utilize the following span grid-column: 1 / span 6; within your HTML code.

<li style="
    grid-column: 1 / span 6;
">
            <div class="featured-image" style="
    flex: 0.6;
"><img src="https://via.placeholder.com/600x300"></div>
            <div class="description">
              <h2>Title 7</h2>
              <p>Description</p>
            </div>
          </li>

This code snippet will assist in creating a design resembling the picture at this link: https://i.sstatic.net/YJN5H.png....

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

Unable to execute script tag on PHP page loading

When I make an AJAX request to fetch JavaScript wrapped in <script> tags that needs to be inserted on the current page, how can I ensure that the code executes upon insertion? Here's the snippet I'm using to add the code: function display ...

Modify the text of the "search" button in bootstrap4

Clicking the "Search" button on my website redirects me to How can I modify the URL from "search" to "my_search" in this link? I want the link to be I am using the Django web framework. The fix should also support queries from the form. My code vacanc ...

ASP TextChanged event is not triggering unless the user presses the Enter key or tabs out

My code is functioning correctly, however, the TextChanged event is triggered when I press enter or tab, causing my code to execute. I would like to be able to search for a record without having to press enter or tab. ASP: asp:TextBox ID="txtNameSearch ...

Ways to extend div to fill the rest of the page's vertical space

Apologies, my search has yielded many results, but none that directly address my specific issue. It appears that there is a proliferation of div-height problems on this platform. :-( Here is the layout I am working with for testing purposes: <!DOCTYPE ...

Tips for continuously storing user inputs in a C++ while loop

A user will input a series of numbers. The user can input as many numbers as they want, and all the numbers will be stored in a variable without being added together. #include <iostream> using namespace std; int main() { // declare variables ...

Add a sublime project with a specific directory path

Currently, I am using Sublime Text 2 for a project that is structured as follows: public vendors a.css b.css myfile.less myfile.css (compiled) I am facing an issue where I need to exclude the css files from the main 'public' folder (specifi ...

Adjusting the dimensions of a Twitter widget to fit the screen size

My website is designed to resize based on screen size, but I encountered an issue when adding a Twitter widget. Despite setting the attribute width:'auto', the widget did not resize properly. Below is the code for the widget: <script charset= ...

Can the minimum length be automatically filled between two elements?

I'm struggling to find a way to adjust the spacing of the "auto filling in" dots to ensure a minimum length. Sometimes, on smaller screens, there are only one or two dots visible between items. Is there a way to set a minimum length for the dots in th ...

The HTML select element fails to drop down over the THREE.js scene

I'm currently working on enhancing the Three.js editor for a project. editor link One of the tasks I'm tackling is adding a select element on top of the viewport in the editor. Unfortunately, the dropdown functionality of the select element isn ...

Position a div in the center of a section

Having trouble centering a <div> within a <section>? I've tried setting margin-left and margin-right to auto without success. Any other suggestions? Check out the jsFiddle illustrating the issue: http://jsfiddle.net/veWKh/ ...

How can I incorporate a .shtml file into a .php webpage?

Is there a way to include a .shtml file within a .php page? I know it can be done, as I have successfully achieved this in the past (although I cannot remember the exact code). Can someone please assist me with this? ...

How can I correct the code that is running both the if and else statements simultaneously?

In the code snippet below, a comparison is made between a username and password. If both are correct, the user is redirected to another page. However, there seems to be an issue where both the if and else statements are executing. Code: if (isset($_POST[ ...

Difficulty in arranging DIVs on a flat surface

As a user running Win XP with Firefox, IE, and Google Chrome browsers, I'm encountering an issue where I can't seem to align two DIVs on the same horizontal plane. My goal is to have the left div occupy 24% of the screen width while the right div ...

IE compatibility mode causing ckeditor dropdown to be hidden

When using the CKEditor editor bar inside a green div with another div below it, I noticed that when clicking on "font" or any other option that opens a dropdown menu, it gets hidden behind the bottom div. This issue seems to occur in browsers like Chrome ...

Conceal social media icons when hovering over specific images similar to the feature on the website medium

I am facing an issue with fading out social links that appear over medium and large images on a webpage. I want the social links to fade out when they are over the images and fade back in once they move off the images. The number of medium and large images ...

Custom sized box causing Bootstrap Navbar to be unresponsive

I am having trouble loading the Bootstrap 3 navbar inside a .box class with specific CSS rules: .box { height: 600px; width: 320px; position: relative; border: 2px solid #eee; } Unfortunately, it is not displaying re ...

Challenges with moving images in Unslider

There seems to be an issue with the unslider-arrows on the unslider banner. The images are not sliding properly when transitioning to the next image without user input. Instead, they resize from small to full size starting at the top of the .banner div. ...

Ensuring that plugin files are correctly included in the main plugin file

I've been encountering this issue for some time now, and although I have been finding workarounds, I am finally seeking a permanent solution. The problem arises when I try to include files into my main plugin document (the one that contains the plugi ...

What is the code to create rainbow-colored buttons in a continuous loop on a webpage using HTML?

My current project is built using the Jango framework. {% for tag in tags %} <div class="btngroup" role="group" aria-label="tags"> <h3><a href="{{ tag.get_absolute_url }}" class="btn btn-primary">{{tag.title}}</a></h3> ...

Is there a way to continuously switch a CSS animation class without needing a second click?

I am seeking a solution to animate the color and size of a div box, then return it to its original state when a button is clicked. Here is an example of my code: document.getElementById("andAction").addEventListener("click", function() { document.getE ...