What steps should I take to design a CSS grid layout similar to this?

I'm currently working on creating a grid layout similar to the designs shown below:

https://i.sstatic.net/SHLFe.png


While I have been successful in most aspects of the layout, I am facing difficulty with the first column. I'm trying to prevent it from spanning the full height of the row, aiming for a grid layout similar to the one illustrated in the first image. The gray cells represent divs where additional content will be inserted later.

https://i.sstatic.net/mJfz8.png

Below is a snippet of my CSS code:

.cards_grid {
    grid-auto-columns: 1fr;
    grid-column-gap: 1.5rem;
    grid-row-gap: 1.5rem;
    -ms-grid-rows: auto 1.5rem auto;
    grid-template-areas:
        "first first first first second second second second second third third third"
        "fourth fourth fourth fourth fifth fifth fifth fifth sixth sixth sixth sixth";
    -ms-grid-columns: minmax(auto, 1fr);
    grid-template-columns: minmax(auto, 1fr);
}

I've also experimented with grid-layout-rows: masonry, but unfortunately, it lacks support across various browsers.

Answer №1

By sticking to the method of explicitly defining grid areas, you can apply the same technique for vertical placement as you do for horizontal placement. For example, if you have defined 12 columns horizontally, you can extend this approach vertically.

In this case, it appears that the grid will consist of 5 rows, with certain elements spanning multiple rows. This snippet streamlines the CSS and sets the entire grid to have a 4:3 aspect ratio, though you may need to adjust this based on your specific requirements.

.cards_grid {
  width: 100vmin;
  height: 75vmin;
  display: grid;
  grid-column-gap: 1.5rem;
  grid-row-gap: 1.5rem;
  grid-template-areas: "first first first first second second second second" "first first first first second second second second" "third third third fourth second second second second" "third third third fourth fifth fifth fifth sixth sixth sixth" "third third third fourth fifth fifth fifth sixth sixth sixth";
}

.cards_grid > div {
  background: gray;
}

.cards_grid > div:nth-child(1) {
  grid-area: first;
}

.cards_grid > div:nth-child(2) {
  grid-area: second;
}

.cards_grid > div:nth-child(3) {
  grid-area: third;
}

.cards_grid > div:nth-child(4) {
  grid-area: fourth;
}

.cards_grid > div:nth-child(5) {
  grid-area: fifth;
}

.cards_grid > div:nth-child(6) {
  grid-area: sixth;
}
<div class="cards_grid">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

Answer №2

If you're looking for an alternative, I'd recommend giving this a shot and see how it works out for you

<!DOCTYPE html>
<html>
<head>
<style>
body{
      padding: 0;
    margin: 0;
}
.container{
      width: 100%;
    height: 490px;
    background: #e5e5e5;
    display: flex;
    flex-wrap: wrap;
}
.box-cl-1{
      width: 30%;
    display: flex;
    flex-direction: column;
}

.box-1{
    margin: 10px;
      flex: 30%;
    height: 130px;
    background: #8f8f8f;
}

.box-2{
    margin: 10px;
      flex: 50%;
    height: 200px;
    background: #8f8f8f;
}

.box-cl-2{
    width: 70%;
    display: flex;
    flex-direction: column;
}

.box-3{
    margin: 10px;
    flex: 70%;
    height: 250px;
    background: #8f8f8f;
}

.box-4{
    margin: 10px;
    flex: 30%;
    height: 250px;
    background: #8f8f8f;
}

.box-5{
    margin: 10px;
      flex: 50%;
    height: 200px;
    background: #8f8f8f;
}

.box-6{
    margin: 10px;
      flex: 50%;
    height: 200px;
    background: #8f8f8f;
}

</style>
</head>
<body>

<div class="container">
    <div class="box-cl-1">
      <div class="box-1"></div>
      <div class="box-2"></div>
    </div>
    <div class="box-cl-2">
        <div style="display: flex;">
            <div class="box-3"></div>
            <div class="box-4"></div>
        </div>
        <div style="display: flex;">
            <div class="box-5"></div>
            <div class="box-6"></div>
        </div>
    </div>
</div>

</body>
</html>

Answer №3

A great option is to implement a basic masonry style for your website. Check out this resource on GitHub:

Answer №4

To start creating your grid layout, you first need to define a wrapper with the class .grid-box. Next, specify the structure of your grid using properties like grid-template-columns and grid-template-rows.

Once your template is set, you can position each individual box within the grid using properties like grid-*-start. Below is an example template for a grid layout:

.grid-box {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: 100px 200px 100px;
  grid-template-rows: 25% 25% 25%;
  grid-gap: 10px;
  padding: 10px;
}

.box-1 {
  background: red;
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 1;
  grid-row-end: 2;
}
...
<div class="grid-box">
    <div class="box-1">1</div>
    <div class="box-2">2</div>
    <div class="box-3">3</div>
    <div class="box-4">4</div>
    <div class="box-5">5</div>
    <div class="box-6">6</div>
</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

The animations in fontawesome 6.2 are completely ineffective

I've been struggling to make a spinner icon spin on my webpage. I made sure to download the latest version of font awesome and created a simple HTML file to troubleshoot the issue. Take a look at the HTML code below: <!doctype html> <html la ...

Ways to align two HTML elements on the same line but on different sides

I'm in the process of customizing this layout with Bootstrap: https://i.sstatic.net/jJlOl.png Here's the code snippet: <div class="container-fluid" id="products"> <h2 class='wishlist-title text-center'& ...

Choosing the type attribute of an input tag by using a javascript variable as a condition: What's the best approach?

I am currently utilizing an input tag on the client side to receive text input. <input type="text" class="form-control" placeholder="Enter Message" id="messageText" autofocus/> My goal now is to dynamically set the type attribute based on a Javasc ...

Utilize a React component within a JavaScript file by incorporating ReactDOM in a script found in an HTML file

Can someone explain the process of importing a component into a script in an HTML file? What specific modifications need to be made to ensure everything runs smoothly? Do the Babel libraries need to be included in the HTML file, especially when Webpack ...

Tips on introducing a random pattern into a Javascript gaming experience

To kick off the game, let's generate a pattern randomly. Follow these steps: -Include a function that generates the pattern. (Hint: JavaScript's Math.random function might come in handy) -Invoke your new function from the startGame function I&a ...

What is the best method for accessing data values from HTML within a JavaScript function?

Greetings, I am relatively new to using jquery and ajax. Here is a snippet of my code: .html <li class="list-group-item"> <h2>Vocabulary</h2> <h4> <span class="label label-success">Like Name</span& ...

Facebook is failing to detect any meta tags in the header of my React web application

After checking my code on https://validator.w3.org/, everything seems fine. However, I noticed that Facebook is not reading most of the content from the head section - not even a single meta tag. On the other hand, Google is able to read all the content fr ...

How can you determine the index of a table column in JavaScript when you only know its class name?

I am looking for a way to dynamically hide/show table columns based on their classes, without having to add classes to each individual <td> element. This should be accomplished using classes on the columns themselves. Here is a sample of the table ...

Display of items in a submenu through a drop-down menu when hovering over it

As I work on creating a Navbar with a dropdown menu, I'm encountering an issue where hovering over the main list element displays all the submenu contents at once. No matter what I try in my CSS, including adjusting the positioning of li and ul elemen ...

Utilizing PHP variables within HTML code that is stored within another PHP variable

I've written a .php script that contains some added html in a variable. However, I need to extract text from another variable and include it in the html. Can someone guide me on how to achieve this? Below is the variable containing the html code: $a ...

What is the best way to assign my function to dynamically generated cards in vanilla JavaScript using event delegation?

I've been experimenting with a card flip feature on dynamically generated elements. The first function I created only applied to the template, so I tried delegating the event to an existing HTML element. However, it's not functioning as expected. ...

Creating smooth animations in JavaScript without relying on external libraries such as jQuery

Is there a way in JavaScript to make a <div> slide in from the right when hovered over, without relying on jQuery or other libraries? I'm looking for a modern browser-friendly solution. const div = document.querySelector('.fro ...

Is there a way to ensure that GIFs in jQuery Mobile always start from the beginning?

My cross-platform mobile app built with jQuery Mobile is a small quiz application. After each correct or wrong answer, I display a 3-second GIF. Currently, I have set up the code to show the GIF for 3 seconds before moving on to the next page: else if ($. ...

Displaying information based on selection in AngularJSIn this tutorial

I am a beginner in Angularjs and currently working on developing a website. One of the tasks I have is to create a combo box that, when an option is selected, calls a method to load values into a Scope variable and update it. Below is the code I have so fa ...

Seeking assistance in comprehending the method for styling table data using inline CSS in a JavaScript file

I am currently using a JavaScript file that was created for me to update form data based on user selections and display radio buttons along with the corresponding costs. Although the inline CSS in this script works perfectly fine in Chrome and Firefox, it ...

Issue with JQuery addClass functionality in Firefox

I've come across numerous posts on this topic, but none of them have provided a solution. I recently added drag and drop functionality to my website. When I drag an item over a valid container, I add a specific class to it. Here is the HTML for the ...

Access a webpage in an html document using URL variables

In the process of developing an MVC web app without utilizing any MVC framework, I have created an index.html file with a section that dynamically loads all the views as needed by the user. However, I encountered an issue where direct URLs such as www.foo. ...

What is the best way to measure the timing of consecutive events within a web browser, utilizing JavaScript within an HTML script tag?

Currently delving into the realm of JavaScript, transitioning from a Java/Clojure background, I am attempting to implement a basic thread-sleep feature that will display lines of text on the screen at one second intervals. Initially, I considered using t ...

Guide to utilizing a download link for file retrieval in Python

I'm currently working on creating a script that can automatically download specific files from webpages and save them to designated folders. For the most part, I've been successful in achieving this using Python, Selenium, and FirefoxPreferences ...

How come my blocks are spilling over into other blocks?

I have successfully created a webpage using Drupal 8 and Bootstrap, featuring a "Learn more" button to expand the details. However, I am facing an issue where the information blocks overflow when collapsed. The gray line and label are spilling into the up ...