What are some techniques to ensure that my "display: grid" layout adjusts properly on mobile devices and tablets by implementing media queries?

Responsive Grid Design I am looking to transform my grid desktop view into a responsive layout for mobile and tablets using media queries in CSS. While it looks great on desktop, it does not adjust well on smaller screens.

.grid-container {
    display: grid;
    grid-template-columns: auto auto auto auto auto;
    gap: 25px;
}

/** Media Queries **/
@media screen and(max-width: 400px) {
    .grid-container {
        grid-template-columns: auto;
    }
    .grid-container .item-1 {
        grid-column: 1 / span 5;
        grid-row: 1;
    }
}

I have set up a grid with 5 columns for displaying 5 different items (cards). Item 1 spans the first three columns, Item 3 spans from row 1 - 3, and Item 5 spans from column 2 - 5.

.item-1 {
    grid-column: 1 / span 3;
    grid-row: 1;
    height: auto;
    background-color: hsl(263, 55%, 52%);
    position: relative;
}
.card-detail {
    position: absolute;
    top: 59px;
}

.item-3 {
    grid-column-start: 5;
    grid-row-start: 1;
    grid-column-end: 6;
    grid-row-end: 3;
    background-color: hsl(0, 0%, 100%);
}

.item-5 {
    grid-column-start: 2;
    grid-column-end: 5;
    background-color: hsl(219, 29%, 14%);
    color: hsl(0, 0%, 100%);
}

For mobile devices, I intend to make each grid item fill 100% of the width. On tablets, I want the items to take up 50% width (occupying 2 items per row).

Answer №1

Utilizing Grid provides a significant benefit as it eliminates the need for media queries to achieve grid responsiveness. By incorporating auto-fit or auto-fill alongside minmax, the grid automatically adjusts the number of columns based on the screen size:

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 1em;
}

/* for visualization only */
.grid > div {
  border: 2px dashed red;
  min-height: 20vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div class="grid">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
  <div>Box 4</div>
  <div>Box 5</div>
</div>

To achieve a strict layout of 1 column for mobile and 2 columns for desktop, media queries can be utilized like this:

.grid {
  display: grid;
  grid-gap: 1em;
}

@media only screen and (min-width: 720px) {
  .grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .grid > div:nth-child(4n + 2),
  .grid > div:nth-child(4n + 3) {
    grid-column: span 2;
  }
}

/* for visualization only */
.grid > div {
  border: 2px dashed red;
  min-height: 20vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div class="grid">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
  <div>Box 4</div>
  <div>Box 5</div>
  <div>Box 6</div>
  <div>Box 7</div>
  <div>Box 8</div>
  <div>Box 9</div>
</div>

The use of the nth-child() selector in conjunction with grid-column: span 2 is recommended.

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

Tips for implementing ng-hide/ng-show animations

Is there a way to create an animation on an element in Angular that will be triggered when the item's visibility is changed using ng-hide/ng-show? Which specific CSS classes should be included to ensure smooth animations on elements? ...

The Bootstrap Library reference causes the anchor hover function to malfunction

Feeling frustrated after encountering an issue where the Bootstrap button class interferes with the hover function in standard CSS. Decided to create a custom solution by adding a grey box next to the button to display hover information instead of using t ...

Inject customized CSS following third-party plugin stylesheets in WordPress

I am currently exploring methods to incorporate a custom.css file after the loading of a third-party plugin. I am familiar with registering and enqueueing CSS files in functions.php. Is it possible to establish a dependency for the stylesheets of the thi ...

Maximizing Bootstrap: Enabling dual dropdown options side by side on a single row

I am attempting to design a login form within a Bootstrap navbar dropdown. My goal is to have the bottom item display two items side by side in the same row (login/register), but I am struggling to achieve this. You can view what it currently looks like h ...

Chrome's behavior of reducing the width of inline elements when the content is dynamic

Upon opening the specified URL in Google Chrome, an issue can be observed where on the initial load everything appears fine. However, upon refreshing, the width of the three items at the top (Bedingungen, Preise, and So geht's) collapse. This seems to ...

Ensure that the top of a div stays in place as the user scrolls, and spans the entire width of the div

My goal is to create a sticky or fixed position header inside a div with a list, but I'm facing an issue where it only sticks to the width of the text. If I expand the width to 100%, it takes up the size of the entire page: import styled from 'st ...

The view of the Google map is filled with numerous rounded tiles

Map divided into 3x3 tiles I am looking to customize the appearance of my map and remove all spaces and rounded corners to create a seamless, undivided visual. Is there a way to modify a map tile attribute to achieve this effect? Below is the code snippet ...

Ways to modify the color of table fonts and contents using inline CSS

I would like some help changing the color of the alphabet in my table. Here is the current format: <table border="1" background="images/haya1.jpg" cellpadding="5" cellspacing="10" width="30%" align="center" bordercolor="brown" bgcolor="red"> & ...

Rearrange the entire div container by simply dragging and dropping it. (Shift the Pop-up Modal dialog box)

How can I make a Modal pop-up draggable and change the color of the "Ok" and "Cancel" buttons on hover using a single CSS class? .hidModal{ position: fixed; font-family: Arial, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; ...

Ways to apply styles to the final element containing a specific class within a nested list using CSS

Styling the final HTML element with a specific css class presents challenges due to certain constraints that must be considered. In the website I'm currently working on, the navigation menu consists of multiple nested lists. When an element is clicke ...

What steps can I take to make the cards in bootstrap 5.3 responsive for mobile devices?

I'm struggling with my code and need some help. I've been editing it, but things seem to be getting worse instead of better. Specifically, I can't figure out how to set up my container, row, and columns in Bootstrap properly. My site uses ca ...

Selection box and interactive buttons similar to those found in Gmail

Looking to achieve these effects for <option> and <button> using CSS and JavaScript. Any suggestions on how to do this? ...

Tips for creating dynamic rope animations with canvas and CSS3 styling

Interested in creating a canvas animation, or possibly using CSS3 for the same purpose. Looking to add an automatic rope animation, similar to it swaying gently. I've searched for scripts and other solutions, but haven't found anything suitable. ...

Issues arise when attempting to alter the background image using jQuery without browserSync being activated

I am facing an issue with my slider that uses background-images and BrowserSync. The slider works fine when BrowserSync is running, but without it, the animations work properly but the .slide background image does not appear at all. Can someone help me ide ...

Hovering over the Star Rating component will cause all previous stars to be filled

I'm in the process of creating a Star Rating component for our website using Angular 11. I've managed to render the 5 stars based on the current rating value, but I'm having trouble getting the hover styling just right. Basically, if I have ...

Tips for positioning a div relative to another div while controlling its z-index

Hey there, take a look at the image below https://i.sstatic.net/zYiaY.png The issue I'm facing is quite common - I have div 1 and div 2 visible in a loop, but divs 3 are hidden. When div 2 is clicked on, everything is great so far. However, what I wa ...

Disappear from Sight: Content Concealed as Window Size Changes

Struggling with resizing text and buttons on a website built from a template for class. Looking to ensure that the text remains visible even when the window size changes. Ideally, I want the page content to stay anchored so the focus remains on the text/b ...

Please click on the element located in the top right corner of the image

I am attempting to place a font icon in the top right corner of some images, with the intention of only triggering an event when I click on the icon. However, my current setup causes the entire image to trigger the click event. How can I restructure this t ...

The CSS Menu is not displaying correctly on Internet Explorer 8

My horizontal menu appears distorted on IE8 (and possibly older versions) as shown in the first image. However, there are no issues with the latest versions of Safari, Firefox, and Chrome as depicted in the second image. Any suggestions on why this is happ ...

What is the solution to preventing borders from appearing on input tags when they are autocompleted

Recently, I encountered an issue while working on a website form where the input tag was showing borders during autocompletion, despite specifying border:none. I tried various CSS pseudo classes to resolve this problem but none of them worked. Does anyon ...