What is the best way to layer elements with css Grid?

I've created an app using Angular where I need to design a grid similar to the image below. The specific requirement is that item 2 should overlap item1, and all items except item1 should span the width of the container element.

https://i.sstatic.net/U48wL.jpg


            <div class="item1">
               this is item 1           
            </div>
            <div class="item2">
               this is item 2           
            </div>
            <div class="item3">
               this is item 3           
            </div>
            <div class="item4">
               this is item 4           
            </div>

Do you have any suggestions for CSS to achieve this grid layout?

  
    :host{
          height: 100vh;
          display: grid;          
       }

Answer №1

Align both .item1 and .item2 in the same row and column using grid-area, then center .item2 with align-self and justify-self:

.parent {
  height: 100vh;
  display: grid;
}

.item1, .item2 {
  grid-area: 1 / 1;
}

.item1 {
  background: red;
}

.item2 {
  background: purple;
  justify-self: center;
  align-self: center;
  padding: 1em;
}

.item3 {
  background: blue;
}

.item4 {
  background: green;
}
<div class="parent">
  <div class="item1">
    this is item 1
  </div>
  <div class="item2">
    this is item 2
  </div>
  <div class="item3">
    this is item 3
  </div>
  <div class="item4">
    this is item 4
  </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

I'm having trouble figuring out how to remove an unexpected blank space that appears after the <li> element in a basic drop-down menu code. Can

Why is the browser (Mac Chrome + Firefox) adding extra space after my simple CSS drop-down menu where I have a hidden sub-menu? Check out the code below: <ul> <li>Option Zero</li> <li>Option One Is Longer</li> &l ...

Automatically determine the text direction and alignment according to the language being used, whether it is right-to-left (

Setting the direction property for the body tag can determine whether the text should flow from left to right (ltr) or right to left (rtl). Similarly, individual elements can have their own text-align properties set. Is there a way to streamline coding ef ...

issues with padding in the main content section

When I try to search for something that isn't on the page, a strange margin or padding issue occurs. I have thoroughly reviewed the code but can't seem to pinpoint the problem. The unwanted 30pxish margin after the footer should not be present wi ...

What methods exist to enable individuals to upload files independently through an online form?

I am in the process of creating a support contact form that includes file uploads. The file upload functionality is handled using AJAX, allowing users to upload files and then submit the form when they are ready. However, the current layout where the uploa ...

Can floated divs be prevented from collapsing without taking up any width?

Let's discuss an issue that is not related to block elements collapsing when their children are floated, and has nothing to do with clearing at all. Imagine having a set of floated divs like: <div class="column">Column 1</div> <div cl ...

Leveraging Modernizr for identifying drag and drop functionalities

On my website, there is a section that requires drag and drop functionality in HTML5. I want to inform users on the homepage if their browser supports this feature. Despite attempting to use Modernizr, I have not been successful. I generated a custom file ...

What is the process of sending a file to a server and storing it in a MySQL database?

Currently, I am exploring the process of enabling users to upload images of specific dimensions (468px by 60px) to a designated directory on my server, such as example.com/banners. Subsequently, the URL link to these uploaded banners will be stored in a ...

Navigational menu header leads to specific location on the page

I need the dropdown menu header to both open the related menu and display the content of the first element of the submenu, which is an anchor link on the page. Here is the HTML code for the Dropdown Menu: <div class="collapse navbar-collapse" id="myNa ...

Z-index behaving abnormally

I've been working on developing a website and one of the key features I'm trying to implement is a slideout menu that slides horizontally when the mouse hovers over it. To make development easier, I initially set the menu to be fully extended by ...

Managing numerous ajax forms within a single page

Upon receiving advice from the following inquiry Multiple forms in a page - loading error messages via ajax, I am endeavoring to incorporate multiple forms within one page. The goal is to insert error messages into the corresponding input div classes. I pr ...

List formatted in a table

Can a list be inserted into a table using markdown? I attempted to research this but came up empty-handed. An example of a Table: Fruit |Color ---------|---------- Apples |Red Pears |Green and an example of a list: List item 1 List item 2 ...

Stylish Fonts with Bootstrap 4 and Font Awesome

I've encountered a little issue. I'm attempting to horizontally center each icon within a div, but my attempts so far have been unsuccessful. <div class="row"> <div class="bg-inverse p-x-1 col-xs-12 default-1 "> <div clas ...

Controlling Material-UI using a custom .css stylesheet

Are there alternative methods for customizing Material-UI components using CSS stylesheets? I'm aware of the {makeStyles} method and JSS overrides, but I find it messy in the code and confusing when trying to modularize it in other files. Is there a ...

Issues with Bootstrap's "img-fluid" class functionality

I recently started the web development course by Colt Steele and ran into an issue while working on the "Museum of Candy project" (I made some modifications to it). The problem is that I am using a widescreen monitor, and when I expand my window, the image ...

The width of the textarea in Bootstrap does not automatically expand to 100% width

The issue here is that the width of the textarea does not match the width of the div below it. Even setting min-width: 100% or using the bootstrap class w-100 doesn't fix this problem. https://i.sstatic.net/93uLa.png <div class="container p-5 ...

How can I trigger a function in code.gs to execute a script in an html file when a dialog is opened?

Within my Index.html file, I have a script tag that allows me to execute the code below when I launch the Modal Dialog on Google Sheets using google.run. --Index.html-- <script> function onSuccess(info) { ...add options to select tags... } functio ...

What is the best way to align two different header (h2) tags side by side?

How can I display a person's name and the date on the same line with the name aligned left and the date aligned right? <h2>Firstname Surname</h2><text align="right"><h2>Date</h2> The code above results in both elements ...

Is it possible to add and delete DIVs simply by clicking on a link?

By utilizing a select dropdown list and appending the HTML code stored in the variable "code" (resembling <div>...</div>) right before the end of the div with ID mydiv using $(code).appendTo('#mydiv');. Within these dynamically added ...

Original code

I have a database containing HTML code for a table. I would like to display this code in its original form within a textarea so that I can edit it. However, when I try to echo the code, it appears in compiled form as a table rather than the original HTML ...

Exploring the diversity of perspectives through Angular

Currently, I am in the process of integrating multiple views in Angular to address the footer issue on a website that I am constructing. I want to ensure that the information I have been studying and attempting to replicate is accurate. Here is what I have ...