My goal is to utilize CSS grid to craft a unique layout by establishing a grid area. However, the layout is not functioning as anticipated

Currently, I am in the process of mastering CSS grid layout design.

If you are curious to see my progress so far, check out this Codepen link showcasing my code.

Additionally, for a visual representation of the layout, feel free to view the layout image on Github.

At the moment, I am trying to add an extra row with three columns above the footer by tweaking the following CSS code sections responsible for the layout:

grid-template-areas:
    "nav nav nav"
    "sidebar mycover mycover"
    "sidebar main content1"
    /* "main main main" */    // This line seems to cause issues
    "footer footer footer";

I observed that when I remove the fourth row code "main main main", everything looks fine as per the initial layout image. However, when uncommented, the entire layout crashes and appears blank. Any insights into what might be causing this issue?

Below, you can find snippets of relevant CSS and HTML code for better reference:

/* CSS Code */
body {
  margin: 0px;
  padding: 0px;
}

.item {
  background-color: #1eaafc;
  background-image: linear-gradient( 130deg, #6c52d9 0%, #1eaafc 85%, #3edfd7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #ffffff;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}

.nav {
  grid-area: nav;
}

.cover {
  grid-area: mycover;
}

.sidebar {
  grid-area: sidebar;
}

.content1 {
  grid-area: content1;
}

.content2 {
  grid-area: main;
}

.footer {
  grid-area: footer;
}

.container {
  border: 5px solid red;
  display: grid;
  width: 100%;
  height: 100vh;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 100px 1fr 1fr 100px 80px;
  grid-template-areas: "nav nav nav" "sidebar mycover mycover" "sidebar main content1"
  /* "main main main" */
  "footer footer footer";
}
<div class="container">
  <div class="item nav">nav</div>
  <div class="item cover">cover</div>
  <div class="item sidebar">sidebar</div>
  <div class="item content1">content1</div>
  <div class="item content2">main</div>
  <div class="item footer">footer</div>
</div>

Answer №1

In order for the grid area to be valid, it must be either a square or a rectangle with dimensions of 3 x 1, 2 x 2, 4 x 2, or 1 x 1. Any non-rectangular shapes are not permitted. To illustrate this concept further, I introduced a new grid area called content2 and another one named lowercover to your layout.

Your existing main grid area was only covering a single column and row, specifically the third row. Therefore, attempting to place three main grid areas across three columns in the fourth row is not feasible.

To address this issue, I created a new CSS class called orange by modifying the properties of the existing item class. By switching the values of the red and blue components (e.g., changing #1eaafc to #fcaa1e), I applied this new class to main, lowercover, and content2. This adjustment now accurately represents the desired layout you were aiming for in your original code.

If you wish to implement a non-rectangular shape within a grid, consider nesting another grid or flexbox within your current layout. However, managing multiple layouts for various device screen sizes may pose challenges.

This explanation of grid-template-areas not functioning correctly offers additional insight on this topic.

For more comprehensive information on CSS grid, refer to this detailed guide: complete guide to CSS grid

body {
  margin: 0px;
  padding: 0px;
}

.item {
  background-color: #1eaafc;
  background-image: linear-gradient( 130deg, #6c52d9 0%, #1eaafc 85%, #3edfd7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #ffffff;
  border-radius: 4px;
  border: 2px solid #171717;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}

.nav {
  grid-area: nav;
}

.cover {
  grid-area: mycover;
}

.sidebar {
  grid-area: sidebar;
}

.lowercover {
  grid-area: lowercover;
}

.content1 {
  grid-area: content1;
}

.content2 {
  grid-area: content2;
}

.footer {
  grid-area: footer;
}

.main {
  grid-area: main;
}

.container {
  border: 5px solid red;
  display: grid;
  width: 100%;
  height: 100vh;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 100px 1fr 1fr 1fr 80px;
  grid-template-areas: 
    "nav nav nav"
    "sidebar mycover mycover" 
    "sidebar main content1" 
    "lowercover lowercover content2"
    "footer footer footer";
}

.orange {
  background-color: #fcaa1e;
  background-image: linear-gradient( 130deg, #d9526c 0%, #fcaa1e 85%, #d7df3e 100%);
}
<div class="container">
  <div class="item nav">nav</div>
  <div class="item cover">cover</div>
  <div class="item sidebar">sidebar</div>
  <div class="item lowercover orange">lower cover</div>
  <div class="item main orange">main</div>
  <div class="item content1">content1</div>
  <div class="item content2 orange">content2</div>
  <div class="item footer">footer</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

Struggling with html code?

Struggling with some school work and facing a challenge. I need the menu to be centered, but it's not cooperating. My teacher was no help, so I'm turning to this website for assistance. This is what I have come up with: * { background-col ...

Is there a way to transform an HTML table from a horizontal layout to a vertical

I have reorganized the bus seating layout horizontally using a table. However, I now need to transform it vertically for mobile phones without disrupting my existing code. My attempt at using transform(rotate90deg) achieved the desired result, but the tab ...

One way to automatically create unique reference numbers is to generate a random number between 1 and 99 and append it to a date format, such as YYMMDD. For example,

I have a code that generates numbers like 030317-001 to 030317-010 in a list. How can I modify it so that the same sequence is generated with just one button click, without any repeats, up to a maximum of say 030317-099? <?php $x = date("dmy");// ...

Creating a sliding bottom border effect with CSS when clicked

Is there a way to animate the sliding of the bottom border of a menu item when clicked on? Check out the code below: HTML - <div class="menu"> <div class="menu-item active">menu 1</div> <div class="menu-item">menu 2</ ...

CSS tip: Create a trendy design by adding a slanted border to the last

I need help creating a unique menu list with a white border design. The borders should all be straight by default, except for the last border which must be slanted. ul { background-color: #183650; } li { list-style: none; display: inline-block; b ...

Can the HTML attributes produced by AngularJS be concealed from view?

Is there a way to hide the Angular-generated attributes such as ng-app and ng-init in the HTML code that is output? I want to present a cleaner version of the HTML to the user. Currently, I am using ng-init to populate data received from the server, but ...

Separate your buttons with style using the Bootstrap button groups with

Is there a way to add border line separators between buttons in Bootstrap button groups? <div class="btn-group" role="group" aria-label="Button group with nested dropdown"> <button type="button" class=&quo ...

How can I keep the CSS3 animation playing even after the hover state ends?

When hovering over the small menu logo on my site, there's a subtle wiggle effect that I really like. However, I would prefer if the animation played all the way through before repeating. Here's the code I'm currently using: Here is my code ...

How come my diary section (5th) is showing up/operating in my teacher's section (4th)?

My journey with HTML, CSS, and Javascript began as a beginner. After following a tutorial on YouTube and making some modifications, everything was running smoothly until the diary section unexpectedly appeared under the teacher's section, which should ...

What is the best way to vertically center a div that has a fixed position and a specific width in pixels

Is there a way to center a fixed position div with a specified width of 300px, rather than in percentage? <div class="mainCont"> <div class="childCont"> </div> The childCont div has a fixed position and width of 300px. How can I ensur ...

Display hyperlinks upon hovering over text

In the sign-up form, there are options for two different types of users: teachers and students. When the sign-up option is selected, a drop-down menu with choices for teacher and student will appear. However, I would like it to function more like other w ...

What is the best way to link one element to another element?

Apologies for the mismatched title, but here's my issue: I need to create hidden checkboxes for each Polymer's paper-button div in jsp/style related problems. <%int i; %> <form ACTION="Computation.jsp"> <% for(i=0; i<n; ...

HTML5 Embedding a redirect iframe for connection status validation [UPDATE]

I've created an offline HTML file and embedded an iframe within it that redirects to a site if there is an available internet connection. However, in the event of no internet connection, the iframe will redirect to an offline page. Could someone plea ...

What is the correct CSS2 selector syntax to apply the :hover effect to the nth child of a specific element?

My goal is to apply a unique background color to each menu li element when hovered over, and I want to accomplish this using CSS2. <ul> <li> <li> <li> <li> </ul> I managed to add a background color to t ...

The fuse-sidebar elements are not being properly highlighted by Introjs

I have recently developed an angular project that utilizes the fuse-sidebar component. Additionally, I am incorporating introjs into the project. While introjs is functioning properly, it does not highlight elements contained within the fuse-sidebar. The ...

Is it necessary for me to employ MetaTag http-equiv in conjunction with DTD XHTML 1.0 Transitional//EN?

Currently using Asp.net for my project. In the document, I have included the following: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> I am wondering if it is necessary to a ...

Having trouble adjusting the space between the footer and the bottom of the page

Looking to create a footer using multiple elements The first element should be positioned at: left: 944px; top: 9749px; The second element should be positioned at: left: 715px; top: 9819px; The third element should be positioned at: left: 718px; top: ...

implementing a time picker feature with jQuery

As I am not familiar with jQuery, I came across this script while searching for a time picker. If anyone could guide me on how to incorporate this code into my HTML page to enable the time picker functionality, I would greatly appreciate it. /* jQuery t ...

Delete auto-generated list using handlebars JS

I have successfully created a dynamic list using Handlebars.js and its template. However, I am now facing confusion on how to remove or delete items from the list using a function or specific code. As I am new to Handlebars, I would appreciate any help. ...

Is there a problem with Chrome's display?

I'm currently using Chrome version 53.0.2785.143 and have noticed a rendering issue that tends to occur more frequently within my application when utilizing CSS transform scale. Feel free to check out this JSFiddle link which demonstrates the problem ...