Hybrid Layout using a Fusion of Two CSS Grids

Currently, I am working on an assignment that involves creating two groups of CSS grids intertwined with each other. The desired layout should resemble the image provided.

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

Below is the code snippet I am using:

.group1 .item1 {
  grid-column: 1 / 4;
}

.group1 .item2 {
  grid-column: 1;
}

.group1 .item3 {
  grid-column: 2 / 4;
}

.group2 .item4 {
  grid-column: 2 / 3;
}

.group2 .item5 {
  grid-column: 3 / 4;
}

.group2 .item6 {
  grid-column: 2 / 4;
}
.container {
  display: grid;
  grid-gap: 5px;
  max-width: 1200px;
  margin: 0 auto 100px auto;
  border: 8px dashed #999;
}
<section class="part5 container">
  <div class="container group1">
    <div class="item item1">Item 1</div>
    <div class="item item2">Item 2</div>
    <div class="item item3">Item 3</div>
  </div>
  <div class="container group2">
    <div class="item item4">Item 4</div>
    <div class="item item5">Item 5</div>
    <div class="item item6">Item 6</div>
  </div>
</section>

I have been unable to achieve the expected output as shown in the [image] provided, and I must retain the existing HTML structure. Any assistance in rectifying this would be greatly appreciated.

Answer №1

To prevent subcontainers from interfering and better organize your elements, you can utilize display:contents along with display grid and grid-area properties.

It's worth noting that this approach may not be fully supported across all browsers at the moment.

Here is a demonstration of the concept:

.part5 {
  display: grid;
  grid-template-colums: repeat(6, 1fr);
  min-height: 100vh
}

.container.group1,
.container.group2 {
  display: contents;
}

.item1 {
  grid-column: 1/ span 6;
  grid-row: 1;
  border: solid;
  color: tomato;
}

.item2 {
  grid-row: 2 /span 3;
  grid-column: 1 /span 2;
  border: solid;
  color: turquoise;
}

item3 {
  grid-row: 2;
  grid-column: 3/span 4;
  border: solid;
  color: green;
}

...
<section class="part5 container">
...
</section>

https://css-tricks.com/get-ready-for-display-contents/

—a magical new display value that essentially makes the container disappear, making the child elements children of the element the next level up in the DOM.

A simple tweak to your code could involve updating it as follows:

.container {
  display: contents
}
...

The more direct way would be to have both groups share the same grid structure while overlapping them:

.container {
  display: grid;
  width: 100%;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(4, 1fr);
}

.group1 {
  grid-row: 1 / span 4;
  grid-column: 1 / span 6;
}

group2 {
  grid-template-rows: repeat(2, 1fr);
...
<section class="part5 container">
...
</section

Answer №2

.main-container{
  display: grid;
  grid-template-columns: repeat(6,auto);
  grid-gap: 5px;
  grid-template-areas: 
  'square1 square1 square1 square1 square1 square1'
  'square2 square2 square3 square3 square3 square3'
  'square2 square2 square3 square3 square3 square3'
  'square2 square2 square4 square4 square5 square5'
  'square2 square2 square4 square4 square5 square5'
  'square2 square2 square6 square6 square6 square6'
  ;
}
.square{
  border: 2px solid black;
  background-color: lightblue;
  padding: 10px;
  border-radius: 12px;
  text-align: center;
}
#square1{
  grid-area: square1;
}
#square2{
  grid-area: square2;
}
#square3{
  grid-area: square3;
}
#square4{
  grid-area: square4;
}
#square5{
  grid-area: square5;
}
#square6{
  grid-area: square6;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <div class="main-container">
        <div class="square" id="square1">item-1</div>
        <div class="square" id="square2">item-2</div>
        <div class="square" id="square3">item-3</div>
        <div class="square" id="square4">item-4</div>
        <div class="square" id="square5">item-5</div>
        <div class="square" id="square6">item-6</div>
    </div>
</body>
</html>

Answer №3

display: subgrid

To efficiently address this issue, utilizing display: subgrid is recommended. This CSS Grid feature is tailored for layouts like these, enabling nested grid containers to recognize the grid lines of the primary container.

Unfortunately, despite its benefits, this feature is currently unavailable. More information can be found here.


grid-template-areas

Another effective solution would involve making the primary container (.part5.container) a grid container and structuring the child containers as needed using grid-template-areas.

Similar to the previous feature, grid-template-areas is not yet accessible. Additional details are provided here.


A Creative Approach

This approach combines CSS Grids with a touch of absolute positioning to work around the missing features mentioned earlier. No modifications are necessary in the HTML structure.

.part5.container {
  display: grid;
  border: 8px dashed #999;
  height: 100vh;
  grid-template-rows: auto auto;
  grid-template-columns: 35% 1fr;
  grid-template-areas: " group1 group1 " 
                       "   .    group2 ";
}

.container.group1 {
  grid-area: group1;
  display: grid;
  grid-template-rows: 50px 1fr;
  grid-template-columns: 35% 1fr;
  grid-gap: 5px;
  position: relative;
}

.item1 {
  grid-column: 1 / -1;
}

.item2 {
  position: absolute;
  top: 55px;  
  width: 35%; 
  height: calc(100vh - 71px); 
}

.item3 {
  grid-column: 2;
}

.container.group2 {
  grid-area: group2;
  display: grid;
  grid-template-rows: 1fr 50px;
  grid-template-columns: 1fr 1fr;
  grid-gap: 5px;
  margin: 5px 0 0 5px;
}

.item6 {
  grid-column: 1 / -1;
}

.item {
  background-color: lightgreen;
  display: flex;
  align-items: center;
  justify-content: center;
}

body {
  margin: 0;
}

* {
  box-sizing: border-box;
}
<section class="part5 container">
  <div class="container group1">
    <div class="item item1">Item 1</div>
    <div class="item item2">Item 2</div>
    <div class="item item3">Item 3</div>
  </div>
  <div class="container group2">
    <div class="item item4">Item 4</div>
    <div class="item item5">Item 5</div>
    <div class="item item6">Item 6</div>
  </div>
</section>

Answer №4

As I dedicated time to honing my grid skills, I crafted the following code structure based on the layout you provided:

body {
    margin: 0;
    padding: 0;
    font-family: Trebuchet MS;
    background: #000;
    height: 100vh;
}
.grid-container {
    display: grid;
        grid-template-columns: repeat(6, 1fr);
        grid-template-rows: repeat(6, 1fr);
        /* grid-template-rows: auto; */
    gap: 8px;   
  border: 8px dashed #888;
}

.each-grid-item {
    text-align: center;
    align-content: center;
    padding: 30px;
    border: 2px solid red;
    background: #000;
    color: #fff;
    }

.grid-item-1 {
    grid-column: 1/7;
    grid-row: auto;
}

.grid-item-2 {
    grid-column: 1/3;
    grid-row: 2/7;
}

.grid-item-3 {
    grid-column: 3/7;
    grid-row: 2/4;
}

.grid-item-4{
    grid-column: 3/5;
    grid-row: 4/6;
}

.grid-item-5{
        grid-column: 5/7;
        grid-row: 4/6;
}

.grid-item-6{
    grid-column: 3/7;
    grid-row: 6/7;
    
}
<div class="grid-container">
  <div class="each-grid-item grid-item-1">1</div>
  <div class="each-grid-item grid-item-2">2</div>
  <div class="each-grid-item grid-item-3">3</div>
  <div class="each-grid-item grid-item-4">4</div>
  <div class="each-grid-item grid-item-5">5</div>
  <div class="each-grid-item grid-item-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

Improving the Efficiency of CSS Selectors

Is there a tool available that can simplify complicated CSS selectors to improve performance? This tool would analyze all CSS rules and generate simplified single-pathed selectors with all the necessary attributes. It would then scan each DOM node to find ...

Changing the type of value in a React select onChange

<Select options={options} value={selectedBusinessList} isMulti placeholder="Select Business" onChange={(value: any) => setSelectedBusinessList(value)} onInputChange={query => { if ...

My attempts to troubleshoot the JavaScript function have all been unsuccessful

I am completely new to JavaScript and feeling a bit embarrassed that I'm struggling with this. My goal is to build a website that takes first and last names as input and generates email addresses based on that information. Despite my attempts, moving ...

Creating a unit by merging variables in SASS

Is there a way to merge two variables in SASS to create a single unit together? The current configuration is outlined below: $font-size: 16; $font-unit: px; $base-font-size: {$font-size}{$font-unit} !default; @if unit($base-font-size) != 'px' ...

Selecting a specific element from a group of elements sharing the same class using JQuery

Is there a way to target individual elements from a group of elements that share the same classes or other properties without assigning different classes to each element? I need to be able to work on each element separately. I have attempted this approach, ...

Issue with importing styles within a media query

I am having trouble importing a style that is based on a media query. When I try to import the styles, it doesn't seem to work. However, if I directly include the styles within the media query itself, they show up on the page. For reference, you can ...

Problem with CSS/SASS dropdown navigation bar

Having trouble with a design issue, I have successfully recreated the Twitter navigation bar and now I want a dropdown menu to appear when hovering over the 'settings' text. However, I'm facing difficulties as the overflow is hidden in the n ...

Preventing the background color from exceeding the text boundaries using CSS

I'm having trouble containing the background color within the text content. I want the right spacing to be equal to the left, but as you can see in this screenshot, the right side is not adjusting: https://i.sstatic.net/cD4GK.png Here's the cod ...

Styling with CSS

What is the preferred method of CSS styling in the industry? Option 1: Create classes for each attribute (similar to bootstrap) and then attach the classes in HTML. For example: HTML: <p class="text-white text-bold"> John Doe </p> CSS: .te ...

Struggling with a CSS problem that jQuery can't seem

I recently experimented with Infogrid on my website. After adding a header and footer, I noticed that the text in the last block of iron man was being cut off. Even though I removed overflow:hidden; from the body and html, the issue persisted with the te ...

Steps to showcase specific data in the bootstrap multi-select dropdown menu sourced from the database

Utilizing CodeIgniter, I have set up multiple select dropdowns using bootstrap. The issue I am facing is with displaying the selected values in these drop-downs on the edit page. Would appreciate some guidance on how to tackle this problem. Here is a sni ...

When the CSS animation has finished in JavaScript

I am currently developing a game using HTML/JavaScript, and I have implemented a "special ability" that can only be activated once every x seconds. To indicate when this ability is available for use, I have created a graphical user interface element. Since ...

choosing a specific element within an HTML string stored in a variable

I have a variable containing HTML string data like this: var htmlstring = "<div><div class='testdiv'>924422</div></div>" My goal is to extract the content of the div with a specific class, in this case .testdiv. How ca ...

The 'formGroup' property cannot be bound in the LoginComponent because it is not recognized as a valid property of the 'form'

When working on my Angular project and using ReactiveFormsModule to create a form, I encountered an error message during the build process. The specific error was: src/app/security/login/login.component.html:11:13 - error NG8002: Can't bind to ' ...

Move the container all the way to the left

On my page, I have a grey section with the heading 'Start Analysis' that needs to be shifted to the left along with all its contents. How can this be achieved? The HTML code for the section is as follows: <!DOCTYPE html> <html lang="en ...

Using Python to scrape the contents of a webpage that contains JavaScript, potentially utilizing the Selenium library

Looking to analyze the contents of a webpage that contains JavaScript. Any suggestions for a more efficient method than using Selenium? If not, when the page is loaded in a browser, it contains the following elements: <div class="js-container"> ...

Tips for maintaining consistent email formatting across mobile devices

I'm having trouble with getting my flyer to display properly on all screen sizes. It looks fine in inspection mode on Chrome, but breaks on iPhones. I've attached images for comparison and provided a link to the flyer. Any help would be greatly a ...

Exploring GLTF models with Threejs - just a click away!

I am currently developing a project in three.js where I aim to load a GLTF file consisting of geometric shapes. My goal is to extract information, specifically the name, of the shapes that are clicked within the GLTF file. At this stage, I am using console ...

What could be causing my background image to vanish or flicker in Bootstrap?

Something unusual is happening with my website. There's a section with a static background image, and as you scroll, a quote and button (which will eventually link to a store) appear on top of it. Previously, this setup was working fine. However, yest ...

Hiding a span element with CSS and revealing it on hover is possible, but this technique does not apply to p elements

My goal is to create a selection using radio buttons with explanations that appear when hovering over the label. I have tried putting the explanation text in a span-element instead of a p-element, but I'm struggling to understand why there is a differ ...