How to convert columns into rows using CSS grid

Is it possible to change the layout of a CSS grid from 3 columns to 1 row using media queries? Specifically, I want the first column to become the first row and take up the full width, while the second and third columns should combine to form one row with equal spacing. How can this be achieved?

.thegrid {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 40px;
}
<div class="thegrid">
  <div>
    content 1
  </div>
  <div>
    content 2
  </div>
  <div>
    content 3
  </div>
</div>

Answer №1

To maintain the columns, consider applying grid-column: 1/-1 to the initial div element (ensuring it spans the entire grid area). Would this achieve the intended appearance?

Answer №2

To achieve the desired layout, first set up grid display and then switch to flex when the @media screen reaches a certain threshold. Define which rows should be combined in the flexed column and specify how far they should extend. Hopefully, this explanation is clear and helpful. Best of luck!

.thegrid {
  display: grid;
  grid-template-columns: auto auto auto;
  grid-template-rows: 80px 200px;
  gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

@media only screen and (max-width: 600px) {
  .thegrid {
    display: grid;
    grid-template-columns: auto auto auto;
    grid-template-rows: 80px 200px;
    gap: 10px;
    background-color: #2196F3;
    padding: 10px;
  }
  .row-1 {
    grid-column: 1/ span 2;
    grid-row: 1;
  }
  .row-2 {
    grid-row: 2;
  }
}
<div class="thegrid">
  <div class="row-1">
    content 1
  </div>
  <div class="row-2">
    content 2
  </div>
  <div class="row-2">
    content 3
  </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

Expanding divisions vertically without the constraints of fixed heights

Instead of resorting to fixed heights and absolute positioning with top: and bottom:, I'm challenging myself to find a CSS solution without using those methods. I want to explore different ways to solve this problem and enhance my skills. I have a st ...

Problem with the fixed header on a container causing a horizontal scroll bar

This scenario presents a bit of a challenge! I could solve it by adjusting the container's height, but that would disrupt the fixed position of the sticky header. My goal is to have a scrollbar visible in both the X and Y directions on the .scrollabl ...

What is causing the div element to act as a container?

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f7fafae1e6e1e7f4e5d5a1bba3bba5">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-B0v ...

Is Selenium Webdrive the superior choice over direct requests?

My goal is to extract verification sentences from Facebook pages using BeautifulSoup. The specific sentence I am looking for is "Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." I tried parsing the ...

Mobile display issue with Foundation 4 topbar navigation

I am experiencing an issue with Foundation 4 not rendering correctly on a small screen. When running on a local server, my custom CSS and JS are disabled, even though all the necessary libraries including the Foundation library are properly loaded: Why is ...

Issue with Flask form submission in a specific scenario but functioning correctly in a different context

I'm currently working on a flask website, and I'm facing an issue with the SubmitField not functioning when I click it. I tried a similar method with a smaller form and it worked, but for this specific case, it's not working. routes.py from ...

After attempting to retrieve local .HTML content with JQuery ajax for the second time, the JavaScript code within index.html appears to be malfunctioning

My apologies for my limited proficiency in English... Currently, I am engaged in phonegap development where I am attempting to utilize JQuery ajax to retrieve local .HTML content and insert this extracted content into the div with the id="next-page" in in ...

Ensure that the card header in Bootstrap CSS is consistently displayed at a height of two lines

I am currently developing a webpage using Angular, where bootstrap cards are generated dynamically. The code snippet for this looks like the following: <div class="row"> <div class="card border-primary p-0 mb-2 col-md-4 col-sm-6 colxs-12" *ng ...

Calculate the total sum of selected values in a multiple select dropdown using jQuery

Is there a way to calculate the sum of selected items in a multiple selection dropdown menu? For instance, if I select "X12SO" and "X13SO", their values should add up to 30. let total = 0; $("select[name='myselect[]'] option").each(function(){ ...

Having trouble retrieving the input value using JavaScript

I have been attempting to retrieve the value of an input tag using JavaScript and display it on the console, but my code seems to be not functioning properly. Can someone help me identify the issue in the following code snippet? const userTextValue = doc ...

Analyze HTML while maintaining the integrity of the initial content

I am facing a challenge with my HTML files. I need to replace certain elements while leaving the rest of the content untouched. Specifically, I am looking to run this jQuery expression (or something similar to it): $('.header .title').text(&apos ...

Issues with special characters in @font-face rules

For some reason, I'm encountering issues with the CSS3 property @font-face in certain browsers when trying to use my own fonts. Chrome, Safari, and IE10 work fine, but other browsers present several problems: The code I used looks like this: (fonts G ...

Is there a way to prevent HTML code from being executed when sharing it as text? I want to be able to share code without it being activated as HTML on the page

Seems like I have a straightforward issue related to HTML tags. I am looking to post some code snippets on my blog, specifically the <h1> heading</h1>. I want visitors to view and not just the bolded text of heading. Do I need to incorporate J ...

Exploring the distinctions among different material design frameworks

Confirming my grasp of the situation here. Material design is an interface building approach developed by Google. Material lite is Google's public implementation of this concept. serves as an independent open-source adaptation. In addition, Angul ...

Reorganizing the layout of grid items

Currently, I am working on a task involving GRID CSS. The main objective of the task is to utilize GRID specifically, rather than other methods like flex. I have successfully set up two columns in my GRID, each with its own divs. However, what I am struggl ...

Building an Online Presentation through Imagery

While I am aware that there are numerous ways to implement a slideshow on a website, my question is centered around identifying the most widely used approach. Specifically, I am interested in knowing which method is considered the most mainstream for crea ...

Creating Dynamic HTML/DOM in Angular 14: A Guide for Adding New Items to a List

I am currently iterating through a list of items and displaying them within a div element. These items are rendered when the page initially loads. <button (click)="addCut()" mat-raised-button color="primary">Add New Cut</button ...

Using srcset and picture together to optimize images for various screen sizes

At my workplace, we are in the process of devising a strategy to integrate responsive images into our custom framework. Our goal is to have one centralized solution that caters to all image use cases. After investigating, it appears that using srcset for ...

Troubleshooting Problem with CSS Background-Image in Safari

These questions have been popping up all over the web with little response. I've added some CSS in jQuery like this: $('#object').css('background-image', 'url(../../Content/Images/green-tick.png)'); This works in all b ...

What is the method for showing the arrays following Json.stringify and Json.parse?

I've been facing challenges trying to redirect notifications from my mobile application to another page and store them as historical records. Despite my efforts in researching and experimenting with various methods, I have yet to achieve the desired o ...