What is the method to achieve this layout shown in the image using CSS grid?

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

.grid-item {
  background: blue;
  height: 100px;
}

.grid-item .item1 {
  grid-row-start: 1;
  grid-row-end: 3;
}
<div class="grid-container">
  <div class="grid-item"> </div>

  <div class="grid-item item1"> </div>

  <div class="grid-item"> </div>
</div>

The current setup is not meeting my requirements View what I do not want

Desired outcome can be seen in the following image:

This is what I am aiming for

Answer №1

When it comes to CSS Grid Layout, it really shines in its ability to divide a page into distinct regions and establish the relationship between parts of a control made from HTML primitives in terms of size, position, and layer.

If you set the height of a grid-item, it goes against the core purpose of using grid layouts, which is to allocate areas to grid children based on the layout defined by grid lines.

.grid-container {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: 1fr 1fr;
  height:200px;
}

.grid-item {
  background: blue;
  
}
.item1 {
  grid-row: 1/3;
  grid-column: 2/3;
}
<div class="grid-container">
  <div class="grid-item"> </div>

  <div class="grid-item item1"> </div>

  <div class="grid-item"> </div>
</div>

Answer №2

Adjust the grid-items to remove their height and set the rows to a fixed height of 100px.

Next, designate the "tall" item to be located in column 2.

.grid-container {
  display: grid;
  gap: 10px;
  grid-template-columns: 2fr 1fr;
  grid-auto-rows: 100px;
}

.grid-item {
  background: blue;
}

.grid-item.item1 {
  grid-column:2;
  grid-row: 1 / span 2;
}
<div class="grid-container">
  <div class="grid-item"> </div>

  <div class="grid-item item1"> </div>

  <div class="grid-item"> </div>
</div>

Answer №3

Initially, there is a typo that needs to be corrected: the selector .grid-item .item1 implies that .item1 is a child of .grid-item, which is not the case. The space between classnames should be removed to become .grid-item.item1.

In addition, the height for .item1 must be reset so that it spans the entire rows it occupies and specify the columns it should be in to prevent it from appearing elsewhere.

To assist with debugging your CSS, assign different background-colors to your items to visualize their positioning accurately.

Possible CSS solution:

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

.grid-item {
  background: blue;
  height: 100px;
}

.grid-item.item1 {
  grid-row-start:1;
  grid-row-end:3;
  grid-column:2;
  height:auto;
  background:red;
}
<div class="grid-container">
  <div class="grid-item"> </div>

  <div class="grid-item item1"> </div>

  <div class="grid-item"> </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 PHP Include function seems to be malfunctioning and unable to properly display

Everything you see is what I have on my home.php page <?php include ('../bgs/bg-verbier.html'); include('../menus/menu-verbier.html'); ?> Both of the files requested are located in the parent directory. The /menus/menu-v ...

Creating an interactive timer that counts down in real-time using a combination of Django and JavaScript

When passing the date and time from views.py to my HTML page, I am encountering difficulties in utilizing that information to create a countdown timer using Javascript. For example: return render(request,'page.html',{'date':i.date,&apo ...

Reducing the Distance Between Columns in Bootstrap 4

I am currently working on embedding two websites onto my own website. The idea is to showcase these websites side by side within a custom-sized iframe that adjusts responsively based on the viewport size. To achieve this, I am utilizing Bootstrap 4's ...

Determine the vertical dimension of a child division once it has been integrated into an HTML document

My goal is to create a website with multiple pages without having to recreate the toolbar for each page. To achieve this, I have created a separate HTML file and CSS file specifically for the toolbar. On each page, I simply import the toolbar using the fo ...

Tips for navigating a list of areas in JavaScript

Currently, I am in the process of learning Javascript and I have a query regarding browsing an area list using Javascript. Could someone kindly guide me on whether it is possible to achieve this, and if so, how? Below is the HTML code snippet I am workin ...

Unable to delete borders within the container

Is there a way to eliminate the borders within this container? Visit this link for reference. I've tried using firebug but I can't seem to locate it... Appreciate any assistance you can provide. Thank you! ...

swapping out an external CSS file in React for a new CSS file

My React app is quite large and includes four main CSS files (darkLTR, lightLTR, darkRTL, lightRTL), which may not be the most efficient setup. The templates were provided by my boss, and I was instructed to use them instead of Material UI, which I initial ...

Tips for customizing elements using Wordpress

Is there a way to easily make my four divs containing icon images and descriptive text editable in WordPress? I have some experience setting up basics and using widgets in my footer and sidebar, but I'm not sure if that's the best approach for th ...

CSS - Ensuring the second line of text in ul/ol li does not extend further left than the first line

My website features several lists that have this format: > lorem ipsum I am trying to style them like this: > lorem ipsum If anyone can provide guidance on how to rephrase the question I am asking, I would greatly ap ...

Javascript use of overlaying dynamically generated Canvases

Currently, I am developing a game to enhance my skills in HTML5 and Javascript. In the beginning, I had static canvases in the HTML body but found it difficult to manage passing them around to different objects. It is much easier for me now to allow each ...

dropdown menu center

I'm currently working with a Bootstrap drop-down menu and I'm trying to figure out how to center the menu directly over the button. Currently, it's aligning to the left or right of the button depending on available space. Any help would be a ...

Trouble with .nextAll function not updating with dynamic content

Within the post request code block, the JSON response is assigned to the variable 'data' var a=data.Data.overview; var b=data.Data.extras; //summary var result=''; result+='<div class="extra-upper-block">'; result+=&apo ...

Issue with changing image source on hover using JQuery not functioning as expected

I've been attempting to modify the image src when hovering using JQuery. I'm currently working in Brackets, and when I preview it live in Chrome, everything seems fine. However, when I try to open the file directly in Chrome or IE, it doesn' ...

Can the performance be affected by using multiple IFrames?

Our RIA presentation layer utilizes Tibco General Interface, with a .NET application serving as the server side that exposes data via Web services, etc. Thus far, we have successfully developed the presentation aspect without the need for ASP.NET controls ...

Adding Firebase Data to Vue Table

I am currently utilizing Vue Table JS, which can be found at https://github.com/matfish2/vue-tables-2 <script type="text/javascript"> Vue.use(VueTables.ClientTable); new Vue({ el: "#app", data: { columns: ['name', 'code', ...

Retrieving text snippet from an HTML document using .NET

Users input HTML content using a richtext editor, allowing for a variety of elements. Here is an example of such content: <h1>Header 1</h1> <p>Some text here</p><p>Some more text here</p> <div align=right><a hr ...

The CSS file seems to be ineffective when building Vue in production

In my Vue project, I am using a single file component with a chart module. In order to customize the styles of the chart module's CSS, I created a custom CSS file in the assets folder where I added lines to override certain styles (such as changing th ...

I am currently working on developing an HTML and JavaScript audio player that can play audio at specific scheduled times

Looking to create a custom HTML/JavaScript audio player that adjusts playback based on the time of day. For instance, if it's 1:00 pm, the file will start playing from 0:00 minutes; and if it's 1:01 pm, the file will begin playing from 1:00 minut ...

What are the steps to create a hovering dropdown menu that changes the background window to transparent when hovered over?

Is there a way to create a dropdown menu that changes the background window to transparent when hovered over? Here is an example of what I am looking for: https://i.sstatic.net/FplLm.png The dropdown menu should look like this when hovered over: https: ...

What is the best way to access the display property of a DOM element?

<html> <style type="text/css"> a { display: none; } </style> <body> <p id="p"> a paragraph </p> <a href="http://www.google.com" id="a">google</a> &l ...