Creating a seamless transition from 2 CSS grid columns to 1 on mobile devices without the need for a media query

Is utilizing a media query the sole solution for keeping itemX and itemY in the same cell on each device? Are there any native CSS grid methods that could achieve the same result?

Feel free to check out the demonstration on the following fiddle: https://jsfiddle.net/forusak/ctg3auh0/

.container {
  display: grid;
  grid-template: repeat(10, auto) / repeat(auto-fit, minmax(250px, 1fr));
  column-gap: 30px;
  color: white;
}

.container>* {
  background-color: #b90011;
  border: 1px solid black;
  padding: 5%;
  height: 20px;
}

.item1 {
  grid-row: 1 / 10;
  height: auto;
}


/* Comment out the section below to observe the lack of mobile responsiveness */

.itemX,
.itemY {
  grid-area: 3 / 2 / 3 / 2;
  width: 40%;
}

.itemY {
  margin-left: auto;
}
<div class="container">
  <div class="item1">   </div>
  <div class="item">   </div> 
  <div class="item">   </div>
  
  <div class="itemX"> itemX  </div>
  <div class="itemY">  itemY  </div>
  
  <div class="item">  </div>
  <div class="item">  </div> 
</div>

Answer №1

Take a look at the code snippet provided below. If the screen width is less than 464px, items itemX and itemY will stack vertically.

body {
  padding: 1rem;  
}

.res-grid-1 {
  --min-size: 25rem;
  
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--min-size), 1fr));
  grid-gap: 1rem;
}

.res-grid-1 > div {
  padding: 5rem 1rem;
  text-align: center;
  font-size: 2rem;
  background: #557571;
  color: #ffffff;
}

.res-grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11.5rem, 1fr));
  grid-gap: 1rem;
}
<div class="res-grid-1">
  <div>Item 1</div>
  <div class="res-grid-2">
    <div>Item X</div>
    <div>Item Y</div>
  </div>
</div>

However, there is a minor issue between screen widths of 1280px and 1328px where itemX and itemY are displayed horizontally instead of vertically due to grid nesting. While it's possible to achieve a responsive CSS grid without media queries, dealing with nested grids without media queries can be tricky.

If you prefer using media queries, you can address this issue by making the following CSS adjustments:

In the res-grid-2 class, replace the line:

grid-template-columns: repeat(auto-fill, minmax(11.5rem, 1fr));

with:

grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));

and add:

@media only screen and (max-width: 576px) {
  .res-grid-2 {
    grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
  }
}

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

Guidelines for implementing drag and drop functionality for my specific scenario

Looking to create a restricted area for dragging elements within my app. Specifically, I want the element to only be draggable within a certain defined space. Here is what I currently have: http://jsfiddle.net/L73T5/10/ html <div id='background ...

Issues with Bootstrap 5 navbar-light and font style rendering on Edge browser

My website seems to be having compatibility issues with Microsoft Edge. While everything works fine on Chrome, the navbar classes "navbar-light" and "bg-light" do not apply properly in Edge, and the font style defaults. I am using Bootstrap 5 and webfonts ...

Display text on the screen with a customized design using JavaScript's CSS styles

I need help with printing a specific page that contains some information designed in print.css. I want to print this page, including an image, with the same style. function printContent() { var divContents = document.getElementById("terms").innerH ...

Modify the background color of a particular TD element when clicking a button using JavaScript and AJAX

When I click on the "Active account" button inside the designated td with the <tr id="tr_color" >, I want the value "1" to be added to the 'active_account' column in the database. I have been successful with this functionality. However, I a ...

Adjust the alignment of the text to match the orientation of the image

When creating a layout with text and an image, I want them to be contained within a parent element that has a specified height, such as 100px. The text should adjust to the content, whether it's a short phrase or a lengthy paragraph, without a fixed ...

Ways to position a child element at the center of its parent container?

Hey there, is there a way I can achieve this specific layout without relying on position: absolute? Flexbox would be the preferred method. The main goal here is to have the middle element horizontally centered within its parent element, regardless of other ...

Creating personalized CSS for Sharepoint online site customization

While looking into supported and unsupported customization options and custom CSS for branding in SharePoint online, I found myself perplexed by the vague and confusing information provided by Microsoft's articles. Specifically, I was studying the ap ...

Utilizing SVG elements for interactive tooltips

Can the < use > element show the rect tooltip on mouseover in modern browsers? Refer to 15.2.1 The hint element. <svg id="schematic" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <symbol i ...

How to Align Navigation Searchbar in Center When Bootstrap is Collapsed

I am attempting to center my search bar when it is collapsed. Currently, it appears like this: As you can see, the links are centered, but not the search bar. This is the existing HTML structure: <!-- Navigation Bar Start --> <div class ...

Darken the backdrop of the bootstrap modal

When the modal is opened, the background turns black instead of having an opacity of 0.5 or something similar. How can I resolve this issue? I am using Bootstrap 3.2. Below is some CSS code snippet: .fade.in { opacity: 1; } .modal-open .modal { overflow ...

What are some clever ways to handle the transition of the display property?

While transitions for the display property may not work, I am exploring alternatives. I attempted using the visibility property but it didn't quite fit my needs. In my current setup, different text is displayed when hovering over an anchor tag by sett ...

Using CSS to create sleek all-black Flaticons

After trying various solutions, I am still unable to fix this issue. I downloaded some icons and in the demo, they all appear as they should, with a more colorful aesthetic. Here is what the icons should look like: However, this is how the icons actually ...

Tips for keeping Sub Menu visible at all times

Is there a way to keep the sub menu always visible on the homepage of my website's navigation? Thank you for your help! Check out my website here ...

What is the best way to show a check mark when selecting a checkbox?

I have customized my checkbox to appear as a circle, and now I want to display a check mark when the user selects the checkbox. When I remove -webkit-appearance: none;, the check mark appears but my CSS stops working. Is there a way to achieve this witho ...

How to Align Bootstrap 4 Navbar Brand Logo Separate from Navbar Toggler Button

Can anyone help me with center aligning navbar-brand? When I right align navbar-toggler for mobile devices, it causes the logo to be off-center. Is there a way to independently align these two classes, and if so, how can I achieve this? <nav class="n ...

Achieving Desired Styling Without Altering the HTML Structure: A Guide to CSS Formatting

Check out this HTML code: <head> <meta charset="UTF-8"> <title>Countdown</title> <style> /* CSS goes here */ </style> </head> <body> <ul> <li>one</li> ...

The 600 pixel wide page is not completely filling the 640 pixel width screen on a mobile device

Currently, I am conducting a test on a webpage using an iPhone 5s with a screen resolution of 1136 x 640 pixels. The meta tag is set as follows: <meta name="viewport" content="user-scalable=yes, initial-scale=1, width=device-width, shrink-to-fit=no"&g ...

A dynamic homepage that transforms upon login, created without the use of any content management systems

I am embarking on creating a website with registration and login features without relying on any CMS, using only pure HTML/CSS and other necessary tools. My main confusion lies in how I can develop a homepage that can cater to any visitor by displaying rel ...

Adjust my website to fit various screen sizes and mobile devices

I'm encountering an issue on my website regarding resizing elements and content on various resolutions, particularly on mobile devices. You can view the website here. I've been testing it on both an iPad and a 14" laptop. While everything appear ...

Ways to align radio buttons vertically

I am currently working with three radio buttons that are initially aligned horizontally, but I would like to change their alignment to be vertical instead. What is the best way to achieve this? * { margin: 0; padding: 0; box-sizing: border-b ...