Creating a 3x3 grid with CSS grid is great, especially when you want to place items in specific columns within the grid

I'm attempting to design a layout that has a unique structure:

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

The blue rectangles are represented by divs. It's like a 3x3 grid, but with spaces in the 3rd and 4th columns of the grid.

How can I incorporate these gaps using CSS grid to achieve this particular design? I've experimented with flexbox, but couldn't quite reach the desired outcome, so I'm hoping a grid layout will provide the solution.

Below is the code I have been working with:

.container{
  border: 1px solid;
  display: grid; 
  grid-template-columns: 1fr 1fr 1fr; 
  grid-template-rows: 1fr 1fr; 
  gap: 0px 0px; 
  grid-template-areas: 
    ". . ."
    ". . ."; 
}

.item{
  border: 1px solid lightgrey;
  padding: 10px;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>

Answer №1

With CSS grid, you have the flexibility to specify where an element should start in terms of rows and columns, as well as how many columns/rows it should span.

In this code snippet, the 3rd and 4th children of the wrapper are assigned specific grid positions.

.container {
  border: 1px solid;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-gap: 10px;
}

.item {
  border: 1px solid lightgrey;
  padding: 10px;
}

.item:nth-child(3) {
  grid-column: 2;
  grid-row: 2;
}

.item:nth-child(4) {
  grid-column: 3;
  grid-row: 2;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>

It's worth noting that a non-zero grid-gap is included in this demo. The grid-area settings have been removed since they are unnecessary when using the method outlined above.

Answer №2

A condensed CSS grid solution with essential code snippets:

.container{
  border: 1px solid;
  display: grid; 
  grid-auto-columns: 1fr; 
  grid-auto-rows: 1fr;
  grid-gap:5px;
}

.item{
  border: 1px solid lightgrey;
  padding: 10px;
}

.item:nth-child(3) {
  grid-column:2;
}
.item:nth-child(4) {
  grid-column:3;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>

Answer №3

What are your thoughts on padding and margin usage in CSS?

.container {
  border: 1px solid #777;
  display: inline-grid; 
  grid-template-columns: 1fr 1fr 1fr; 
  grid-template-rows: 1fr 1fr; 
  gap: 0 0; 
  grid-template-areas: 
    ". . ."
    ". . ."; 
}

.item {
  border: 1px solid lightgrey;
  padding: 2px 0;
  width: 55px !important;
  height: 50px !important;
  margin: 1px !important;
  background: #1657c9;
  text-align: center;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
}
.item-text {
  position: relative;
  top: 38%;
  margin: 0;
  font-size: normal;
}
.item-3, .item-4 {
  position: relative;
  left: 100%;
}
<div class="container">
  <div class="item item-1"><span class="item-text">1</span></div>
  <div class="item item-2"><span class="item-text">2</span></div><br>
  <div class="item item-3"><span class="item-text">3</span></div>
  <div class="item item-4"><span class="item-text">4</span></div>
</div>

Answer №4

Give this a shot: Here's some HTML:

<section class="container">
<article class="box1"> </article>
<article class="box2"> </article>
<article class="box3"> </article>
<article class="box4"> </article>
</section>

along with some CSS

.container {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 10px;
    }

    .box1 {
      grid-area: 1 / 1 / 3 / 2;
      background: green;
      height: 100px;
      margin: 10px;
    }

    .box2 {
      grid-area: 1 / 2 / 2 / 4;
      background: green;
      height: 100px;
      margin: 10px;
    }

    .box3 {
      grid-area: 2 / 1 / 4 / 3;
      background: green;
      height: 100px;
      margin: 10px;
    }

    .box4 {
      grid-area: 3 / 3 / 4 / 5;
      background: green;
      height: 100px;
      margin: 10px;
    }

https://i.sstatic.net/tmLVI.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

Using the ol tag in Google Chrome browser

I'm dealing with an issue regarding a simple ol list in a div: <div class="one">Title<hr width="200px"> <ol> <li>One</li> <li>Two</li> <li>Three</li> </ol> </div> Here's the CSS ...

TinyMCE: Tips for adding and highlighting text in your content!

Is there a way to insert, or even replace the current selection with some content and then have it automatically selected? Take for instance this text: Hello nice world! The user has selected nice. After clicking a button, this code is executed: editor. ...

Is it feasible to place an ordered list within a table row <tr>?

Below is a code snippet demonstrating how to create an ordered list using JavaScript: Here is the JavaScript code: <script type="text/javascript"> $(document).ready(function() { $("ul").each(function() { $(this).find("li").each(functio ...

Use React to insert a leading zero next to a number in the input field if the number is greater than 10

I have scoured the web and consulted resources like this one but haven't found a solution that addresses my specific issue with adding padding to input numbers. My goal is to automatically add a leading zero whenever the number inside an input field i ...

Ensuring uniform height of columns in Bootstrap

I want all columns following the first row with col-md-6 to have equal height, regardless of dynamic content (go to full page). <html> <head> <title>Page Title</title> <meta charset="utf-8"> <meta name="viewport" ...

Ways to dynamically incorporate media queries into an HTML element

Looking to make some elements on a website more flexible with media rules. I want a toolbar to open when clicking an element, allowing me to change CSS styles for specific media rules. Initially thought about using inline style, but it turns out media rul ...

Is receiving an error message about a "stray start tag footer" in your HTML validator?

I am perplexed by the error message I am receiving: Stray start tag footer. All I have included here is the tags: <!doctype html> <head> <title>title</title> <meta charset="UTF-8"> <meta name="keywords" cont ...

Keeping the color of CSS buttons consistent can be achieved by setting specific

Here is my CSS code: .horizontalcssmenu ul{ margin: 0; padding: 0; list-style-type: none; list-style:none; } .horizontalcssmenu ul a:active{ color: #00FFFF; background: #FF0033; text-decoration: none; } /* Styles for top leve ...

`To transfer the selected radio button value to a different form field using jquery, follow these steps.`

I need to set either the value no or 1 for the input field name="auth[]" below. <td> send <input type="radio" name="authorized[]'.$c.'" id="send'.$c.'"value="1" checked> </td> <td> no <input label=" ...

Looking to Move the Image Up?

I've been experimenting with creating a grid layout where the bottom image will move up until it is 10px away from the image directly above it. However, no matter what I attempt, the spacing seems to be based on the tallest image rather than the one a ...

When you hover over the Dropdown Menu, the Hoverable Dropdown Menu will automatically close

I am currently working on creating a dropdown menu that pops up when hovering over an item. However, I am facing two challenges: Once my mouse moves away from the item, the dropdown disappears. Therefore, I would like the dropdown to remain visible as lo ...

Show flexibility in the grandchildren of a row layout

My goal is to achieve a layout similar to the image below using the flex "system", but I am facing a challenge with some generated directives that are inserted between my layout div and the flex containers: <div id="i-can-control-this-div" layout="ro ...

How to efficiently pass multiple inputs from an HTML form to a controller in AngularJS using a single ng

Currently, I have multiple input text boxes in my HTML and I need to send their values to my controller to add them to an object. The challenge I'm facing is figuring out how to pass more than one value using just one ng-model. In my HTML code, I have ...

Is it necessary for a click handler to be triggered when clicking on a scrollbar?

Check out these HTML snippets: Jsfiddle <style> div { margin:20px; border: 30px red solid; padding: 20px; background-color:green; overflow-y:scroll; } </style> <div onclick="alert('div clicked');"> ...

How can the refresh event be detected within an iframe by the parent document?

Consider this scenario: We are dealing with a file upload page where we aim to avoid reloading the entire page upon completion of the upload process. To achieve this, we have enclosed the form within an iframe. The form within the iframe posts to itself an ...

Is it possible to customize the placement of the login or register success message?

I would like to display a message indicating whether the login/register process was successful or not above my form. I am using Boostrap 3 along with some PHP functions. The message appears above my navbar but vanishes after 3 seconds, which is a bit anno ...

Switch the checkbox attribute for multiple items within a carousel using Angular 2/Typescript

I am currently working on a carousel feature where each item has a checkbox above it. My goal is to be able to click on an item and have its corresponding checkbox checked. The current code successfully achieves this, but the issue I'm facing is that ...

Finding the nth-level children using JQuery

Is there a way to select nth-level children in CSS/jQuery without using any ID or class references, only tag names? I know how to get first level children with the selector div#block > div. But I am looking for a solution to target deeper levels. & ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

Ensure that the assistant stays beneath the cursor while moving it

I am currently working on creating a functionality similar to the 'Sortable Widget', but due to some constraints, I cannot use the premade widget. Instead, I am trying to replicate its features using draggable and droppable elements: $(".Element ...