Creating a flexible grid layout that adjusts to show either one or two columns on smaller mobile screens

NOTE: This is not a duplicate as the layout order for columns and rows is specifically tailored for mobile devices.

I am attempting to showcase this grid design on mobile devices with the following sequence:

  1. First row: header (1 column, full width)
  2. Second row: main (1 column, full width)
  3. Third row: aside (1 column, full width)
  4. Fourth row: alerts and weather (2 columns, 50% width each)
  5. Fifth row: categories and about (2 columns, 50% width each)
  6. Sixth row: footer (1 column, full width)

Initially, I am uncertain if this layout is feasible as I am unable to implement it successfully.

I can achieve a single column per row on mobile devices, but arranging it in this specific order has been quite challenging. Despite experimenting with various solutions for the past three days, I have not been able to make it work.

Below is the HTML code for the layout:

<div class="angry-grid">
  <header>header here</header>
  <main>main here</main>
  <aside>aside</aside>
  <section id="alerts">alerts here</section>
  <section id="weather">weather here</section>
  <section id="categories">categories here</section>
  <section id="about">about us here</section>
  <footer>footer here</footer>
</div>

Here is an example of the desired outcome: https://i.sstatic.net/GbjA5.png

Additionally, the CSS snippet where the issue arises is provided below:

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.angry-grid {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-areas:
    "header header header header"
    "main main main aside"
    "alerts weather categories about"
    "footer footer footer footer";
  gap: 0px;
  height: 100%;
}
header {
  background-color: #779999;
  grid-area: header;
  height: 50px;
}
main {
  background-color: #8dd9b5;
  grid-area: main;
}
aside {
  background-color: #dd9b95;
  grid-area: aside;
}
#alerts {
  background-color: #ae69d5;
  grid-area: alerts;
  height: 200px;
}
#weather {
  background-color: #b5e5eb;
  grid-area: weather;
  height: 200px;
}
#categories {
  background-color: #6e7c57;
  grid-area: categories;
  height: 200px;
}
#about {
  background-color: #b5ba58;
  grid-area: about;
  height: 200px;
}
footer {
  background-color: #bdfb59;
  grid-area: footer;
  height: 50px;
}

@media (max-width: 767px) {
  .angry-grid {
    grid-template-areas: "header" "main" "aside" "alerts weather" "categories about" "footer";
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr 50% 50% 1fr;
  }
}

At this point, I am nearly considering reverting to building web layouts using old-school 1995 HTML tables ;)

Answer №1

Your grid-template-areas need adjustment.

Consider the following changes:

  grid-template-areas:
    "header header"
    "main main"
    "aside aside"
    "alerts weather"
    "categories about"
    "footer footer ";

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.angry-grid {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-areas:
    "header header"
    "main main"
    "aside aside"
    "alerts weather"
    "categories about"
    "footer footer ";
  gap: 0px;
  height: 100%;
}
header {
  background-color: #779999;
  grid-area: header;
  height: 50px;
}
main {
  background-color: #8dd9b5;
  grid-area: main;
}
aside {
  background-color: #dd9b95;
  grid-area: aside;
}
#alerts {
  background-color: #ae69d5;
  grid-area: alerts;
  height: 200px;
}
#weather {
  background-color: #b5e5eb;
  grid-area: weather;
  height: 200px;
}
#categories {
  background-color: #6e7c57;
  grid-area: categories;
  height: 200px;
}
#about {
  background-color: #b5ba58;
  grid-area: about;
  height: 200px;
}
footer {
  background-color: #bdfb59;
  grid-area: footer;
  height: 40px;
}

@media (max-width: 767px) {
  .angry-grid {
    grid-template-areas: "header" "main" "aside" "alerts weather" "categories about" "footer";
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr 50% 50% 1fr;
  }
}
<div class="angry-grid">
  <header>header here</header>
  <main>main here</main>
  <aside>aside</aside>
  <aside id="alerts">alerts here</aside>
  <aside id="weather">weather here</aside>
  <aside id="categories">categories here</aside>
  <aside id="about">about us here</aside>
  <footer>footer here</footer>
</div>

Adjust

Explore the grid-template shorthand syntax for a better layout management

      grid-template:
        "header header" min-content
        "main main" 1fr
        "aside aside" min-content
        "alerts weather" min-content
        "categories about" min-content
        "footer footer" min-content
        / 1fr 1fr;

By doing so, you can maintain a cohesive structure among template areas, columns, and rows.

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.angry-grid {
  display: grid;
  grid-template:
    "header header" min-content
    "main main" 1fr
    "aside aside" min-content
    "alerts weather" min-content
    "categories about" min-content
    "footer footer" min-content
    / 1fr 1fr;
  gap: 0px;
  height: 100%;
}
header {
  background-color: #779999;
  grid-area: header;
  min-height: 50px;
}
main {
  background-color: #8dd9b5;
  grid-area: main;
}
aside {
  background-color: #dd9b95;
  grid-area: aside;
}
#alerts {
  background-color: #ae69d5;
  grid-area: alerts;
  min-height: 200px;
}
#weather {
  background-color: #b5e5eb;
  grid-area: weather;
  min-height: 200px;
}
#categories {
  background-color: #6e7c57;
  grid-area: categories;
  min-height: 200px;
}
#about {
  background-color: #b5ba58;
  grid-area: about;
  min-height: 200px;
}
footer {
  background-color: #bdfb59;
  grid-area: footer;
  min-height: 40px;
}

@media (max-width: 767px) {
  .angry-grid {
    grid-template-areas: "header" "main" "aside" "alerts weather" "categories about" "footer";
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr 50% 50% 1fr;
  }
}
<div class="angry-grid">
  <header>header here</header>
  <main>main here</main>
  <aside>aside</aside>
  <aside id="alerts">alerts here</aside>
  <aside id="weather">weather here</aside>
  <aside id="categories">categories here</aside>
  <aside id="about">about us here</aside>
  <footer>footer here</footer>
</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

Incorporate the Bootstrap classes to arrange the divs side by side, ensuring that any surplus space is utilized effectively

Having implemented a layout with two images and two paragraphs side by side, everything seemed to be working fine until I encountered an issue when setting both images' height to 400px. The goal was for the paragraph to adjust its size based on conten ...

What is the best way to add content to a TextArea generated by the html helper in MVC 3?

I am attempting to retrieve a List collection of comments from the View using Razor and Microsoft's HTML helper for TextArea (@Html.TextAreaFor). While I can populate individual comments easily, I am unsure how to add the ENTIRE list collection of com ...

What is the most effective way to utilize getStaticPaths in a dynamic manner within next.js

There is a need to paginate static pages for each of the 3 blog categories, but the problem lies in the variable number of pages and the inability to access which category needs to be fetched in getStaticPaths. The project folder structure appears as foll ...

To prevent the background window from being active while the pop-up is open

I have a link on my webpage that triggers a pop-up window, causing the background to turn grey. However, I am still able to click on other links in the background while the pop-up is open. I tried using the code document.getElementById('pagewrapper&ap ...

Issues arising from TypeScript error regarding the absence of a property on an object

Having a STEPS_CONFIG object that contains various steps with different properties, including defaultValues, I encountered an issue while trying to access the defaultValues property from the currentStep object in TypeScript. The error message indicated tha ...

Selenium is detecting a textbox as hidden, despite it being visible in the browser on my end

My password textbox code is as follows: <input class="blahblah" id="someId" type="password"></input> While I can manually interact with this textbox in the browser, when attempting to automate testing using selenium, an error is thrown sayin ...

Having trouble accessing HikVision Web SDK functions in Angular 17

First and foremost, this concerns the HikVision cameras specifically. Because the SDK is compiled to plain JavaScript (with jQuery included), I had to import the JS files in angular.json and use declare in the component TS file (in this instance, camera.c ...

Browser hangs due to the success of ajax request

Whenever there is a change in the input, an ajax call is triggered and upon successful completion, around 80% of the content on the page gets modified. It seems that handling 2 to 3 request responses is okay, but after that, the browser hangs until the su ...

Tips for showing a username when using jQuery's ( document ).ready function?

I am currently using WordPress and have added some content after a div class. I would like to know if it is possible to display the username of the logged-in user within this document ready function? Below is the code I have written. The shortcode does no ...

"Trouble encountered when submitting data to an external system by writing the image source - issues with Firefox and

I've been diving deep into various online resources to find a solution for an issue I'm encountering. My familiarity with Javascript is limited, so please bear with me. Whenever I click on the next button during checkout, a script runs to submit ...

Using jQuery ajax in PHP, the ability to remove retrieved information from a different page is a

I'm currently working on a jQuery AJAX PHP application that allows for adding, deleting, and displaying records using switch case statements to streamline the code. Everything seems to be functioning correctly with inserting and displaying records, bu ...

Is it possible to have a getter in vuex dynamically update when another getter's value changes?

Within my vuex store, I have this getter that retrieves authority_count: authority_count (state, getters) { return getters.dataView?.getUint16(4, true) || state.pack.authority_count; } I want authority_count to default to state.pack.authority_count ...

Delete property from object in JavaScript

Looking for a solution to replace the content of an object with another in an array of objects without passing the reference directly. The challenge is not knowing what values my function will pass, so I can't use them as keys to avoid copying the re ...

What is the reason behind the success of this location?

Curious about the functionality of this particular JavaScript code with its corresponding HTML structure: function getCityChoice() { const location = document.querySelector("form").location.value; console.log(location) } <form name="reserve" a ...

Connecting events across various browsers

In a function, I had to add an event to an element called newElement. To cater for different browsers like IE and Firefox, I checked the condition based on the attachEvent property (only works in IE). if ( typeof(newElement.attachEvent) != "undefined" ) { ...

Angular 2 dropdown list that allows users to add a personalized value in the HTML code

Here is the scenario I am dealing with We are displaying a fixed dropdown list to the user For example, a dropdown list has 4 options such as apple, orange, grape, pineapple and 'create your own' If the user is not satisfied with the provided ...

Tips for implementing lazy loading for a section of a template in Angular 2

I have an Angular 2 component that has several sub-components within it. Some of these sub-components are expensive to load and may not always be necessary, especially if the user doesn't scroll far enough down the page. Although I am familiar with l ...

Tips for effectively redirecting multiple links on a website

Can anyone advise me on how to redirect certain HTML pages to corresponding PHP pages? For instance, if a user lands on www.example.com/sample.html from another website, I want them to automatically be redirected to www.example.com/sample.php without seei ...

What is the best way to link JavaScript files from a Grails plugin in a separate Grails application?

I have developed a unique plugin that offers multiple Grails controllers and domain objects. Additionally, I have created several AngularJS UI components that I wish to utilize in various other projects. These specific files are located within the web-app ...

Enhance your web form with an auto-suggest textbox that allows for multiple selections,

Looking for assistance in implementing an auto complete text box with the ability to select multiple options using Dojo. Any experts out there who can offer their guidance? ...