Design a layout using predefined template areas for easy customization

Currently struggling to construct a layout of colored blocks for an exercise. I have 6 rows of blocks all the same size, but the next div, named violet, should be full width and consist of 4 rows. This is the desired outcome: https://i.sstatic.net/XG7pB.png

This is the current HTML and CSS code. I know it should involve grid template areas, but I can't seem to get it right.

body, html {
    height: 100vh;
}

body {
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container {
    width: 800px;
    height: 800px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(6, 1fr);
    gap: 15px;
}

.container div {
    border-radius: 20px;
}

.red {
    background-color: rgb(199, 48, 48);
}

.lightblue {
    background-color: rgb(55, 207, 245);
}

lightgreen {
    background-color: greenyellow;
}

.blue {
    background-color: rgb(35, 35, 121);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Grid</title>
</head>
<body>
    <div class="container">
        <!-- Row 1 -->
        <div class="red"></div>
        <div class="red"></div>
        <div class="red"></div>
        <div class="red"></div>
        <!-- Row 2 -->
        <div class="lightgreen"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <!-- Row 3 -->
        <div class="lightgreen"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <!-- Row 4 -->
        <div class="lightgreen"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <!-- Row 5 -->
        <div class="lightgreen"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <div class="lightblue"></div>
        <!-- Row 6 -->
        <div></div>
        <div></div>
        <div class="blue"></div>
        <div class="blue"></div>

    </div>
</body>
</html>

If anyone has a solution on how to achieve this using primarily grid template areas, your input would be greatly appreciated!

Answer №1

To specify the position of each grid element, you can use grid-template / grid-area. Alternatively, you have the option to "extend" div.purple with grid-column:

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  max-width: 600px;
}

.grid div {
  aspect-ratio: 1;
  border-radius: 12px;
}

.red {
  background-color: red;
}

.green {
  background-color: green;
}

.lightblue {
  background-color: lightblue;
}

.blue {
  background-color: blue;
}

.purple {
  background-color: purple;
  grid-column: span 4;
  grid-row: span 4;
}

.orange {
  background-color: orange;
}
<div class="grid">
  <div class="red"></div>
  <div class="red"></div>
  <div class="red"></div>
  <div class="red"></div>
  <div class="green"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="green"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="green"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="green"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="lightblue"></div>
  <div class="empty"></div>
  <div class="empty"></div>
  <div class="blue"></div>
  <div class="blue"></div>
  <div class="purple"></div>
  <div class="orange"></div>
  <div class="orange"></div>
  <div class="orange"></div>
  <div class="orange"></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 need assistance comprehending the terms "constructed stylesheet" and #shadow-root in CSS

I am currently trying to override some CSS for an Angular widget, but no matter what I try, the styles don't seem to change. It appears that the widget is being loaded into multiple nodes labeled #shadow-root. I have done some research on this issue, ...

How to vertically align a font-awesome icon inside a button using Bootstrap 3

I've been struggling to vertically align font-awesome arrows inside Bootstrap buttons. The goal is to have both the text and icons centered vertically within the buttons. Whether the text wraps onto multiple lines or stays on one, the arrow icon shou ...

A user-friendly JavaScript framework focused on manipulating the DOM using a module approach and aiming to mirror the ease of use found in jQuery

This is simply a training exercise. I have created a script for solving this using the resource (function(window) { function smp(selector) { return new smpObj(selector); } function smpObj(selector) { this.length = 0; i ...

Why are my indents disappearing when I save in VS Code?

Just starting out with HTML and running into an issue where my indents keep disappearing whenever I save. Here's an example of what's happening. Before saving: After saving: I've experimented with different formatting tools and double-che ...

Center the text within the surrounding box

I am trying to center text next to an inline-block element but using negative margin on the span element changed the layout of the whole content. Can anyone help me with this issue? Here is my code: fieldset { max-width: 350px; margin: 0 auto; ...

Tips on retrieving the text content of an HTML element using its tag

Is there a way to retrieve the selected text along with its HTML tags using window.getSelection()? When using window.getSelection(), it only returns plain text such as HELLO. However, I need the text with the HTML tag included, like this <b>Hello< ...

Angular Material List Component with Material Table

In this code snippet, everything seems to be functioning perfectly. When you click on the magnifying glass icon, some additional information is displayed: <mat-card *ngIf="(bills && bills.length > 0) && all" style="overflow-x: auto;" ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

Improve the parallax effect in your React component

Do you have any tips on smoothing out the scrolling animation for a React component with a simple parallax effect? I tried using requestAnimationFrame() in vanilla JS, but it doesn't seem to work well within the React component's rendering cycle. ...

Create a div in HTML with a customized style to resemble a label with rounded edges

I'm struggling with CSS styling and trying to achieve a specific look for a div, as shown in the picture. Can someone provide guidance on how to achieve this style or share the necessary CSS code? I have come across similar buttons and div designs but ...

Is there a way to determine if a user is using a mobile device and automatically direct them to a mobile-friendly website?

Currently, I am managing 2 websites and I would like to implement an automatic redirection for mobile users. Despite my efforts in researching various websites and viewing tutorials, I have not been successful in making it work. My intention is to redire ...

After clearing the option, the onChange function stops functioning

I'm facing an issue with the following code: success: function (data) { $('#' + idDivRefresh).endLoading(); if (data.message != '@Geral.Sucesso') { $('#' + idDropDown + ...

Creating a static header and footer with a scrollable body

Can someone help me with a modal popup that has a sliding effect? I need the modal body to be scrollable while keeping the Header and Footer in their fixed positions without any overflow. I've been trying to figure it out but can't seem to get it ...

stopPropagation() halts the propagation in the opposite direction

I encountered an issue while attempting to create a non-clickable div. While it does stop propagation, it doesn't prevent what should be stopped and allows things that shouldn't be clickable. js //screen has set stopPropagation $("#smokeScree ...

div consistently positioned above fixed element

My goal is straightforward - I have a fixed div at the bottom of my page that always needs to be visible. Inside this div, there are two sub-divs; one small div should always be on top, and the other should be scrollable. The issue lies with the small div ...

Experiment interrupted by svg border

How can I ensure that text always appears in an SVG tag, even if it extends beyond the boundaries? Check out this jsfiddle example where you can see how the text gets cut off. Any suggestions on how to fix this? <div class="chart"> <svg hei ...

Automatically update a Django table with JSON data

I am looking for a way to automatically append JSON data to my Django website without needing to refresh the page. This JSON data is received when certain asynchronous tasks are completed with Celery. Do you have any ideas on how I can achieve this? Than ...

The drop-down menu remains in the open position following a responsive test

Seeking assistance with a minor issue... I've incorporated a jQuery mobile menu into my responsive web design. When testing the responsiveness by adjusting the browser window size, everything seems to be working correctly. However, upon resizing it b ...

Tips for increasing the height of a div as rows are dynamically added to a table

I am facing an issue with a div main containing a table grid. Rows are dynamically added to the table, causing it to increase in height. However, the height of the div main remains unchanged, resulting in the table overlapping the footer content. What chan ...

How to stop Bootstrap collapsible items from "shifting"

I recently integrated a Bootstrap collapse plugin to manage my list of common inquiries: https://jsfiddle.net/2d8ytuq0/ <ul class="faq__questions"> <li> <a href="#" data-toggle="collapse" data-target="#faq__question_1">..</a> ...