Adapting CSS grid columns based on device screen size

I am currently experimenting with CSS grid to create a layout that changes based on different screen sizes. Here is what I want to achieve:

  • On a mobile screen, there should be one column that takes up the entire width. This column consists of two sections - the first section contains multiple input fields in a single column, while the second section has two columns with smaller input fields.
  • On a larger screen (1024px or wider), I would like to have two columns side by side, each containing one of the two sections. The first section should be narrower than the second. The input fields in the first section should be displayed in two columns, while the ones in the second section should be in three columns.

I hope this explanation makes sense!

Currently, my implementation sort of works but both sections have the same width. This causes the second section to "jump up" very late when the screen gets wider. For example, at 800px browser width, there is a lot of empty space to the right of the first section even though the second section could fit next to it.

Here is the code I have so far:

.surrounding-div {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(260px, 50%), max(600px, 50%)));
};
.first-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, 200px);
    grid-column-gap: 1rem;
}
.second-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, 150px);
    grid-column-gap: 1rem;
}

I'm not sure if my approach is ideal since I'm still new to working with grids. I'm unsure if grid is the best solution for this layout.

If possible, I would like to achieve this without using Media Queries.

Answer №1

To address this issue, consider utilizing @media screen. Here is an example:

@media screen and (max-width: 600px) {
  The CSS Code contained here will only apply to screens with a width less than 600px
}

By employing this technique, you have the ability to adjust the size of each column individually. You may find this resource helpful as well: https://www.w3schools.com/css/css3_mediaqueries_ex.asp

Answer №2

One way to make your website responsive is by using media queries.

.grid_parent{
    display: grid;
    grid-template: number of rows/ xfr yfr; /*Adjust the size of columns for different screen sizes*/

/*Customize the placement of grid elements*/
.grid_element{
    grid-column: startcolumn/ endcolumn;
    grid-row: startrow / endrow;

/*Create a layout for smaller screens*/
@media all and (max-width: 600px) {
    .grid_parent{
        grid-template: number of rows you want/ 1fr; /*Ensure only one column for small screens*/

    /*Adjust grid element positions as needed*/
    ....
    ....
}

If you're unfamiliar with media queries or the 'fr' unit, feel free to look it up. If you still have questions, leave a comment and I can provide further clarification.

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

Create a dynamic feature in Bootstrap4 where the navigation bar changes color as the user scrolls to different sections of the

Currently building my personal portfolio website with Bootstrap 4, I came up with a great idea: changing the navigation bar color based on different sections of the website. I attempted to achieve this using JQuery's offset and scrollTop functions, bu ...

Creating a dynamic list structure using <ul> and <li> tags in a CSHTML ASP file

I am aiming to design a visually appealing structure using ul li for categories. Each line of the structure should consist of three elements: <li> <ul class='kwicks kwicks-horizontal'> <li></li><li></l ...

Creating a new webpage that automatically populates with multiple images from a server using PHP

I'm inquiring about generating a webpage automatically from an HTML template, How can I create a new webpage from an HTML template and display multiple images from the server on the generated page? Please review my code below, Thank You. The follo ...

Maintain parent hover effect while child is being hovered over

After exploring various solutions to fix an issue, I've hit a roadblock. Adding CSS and jQuery code seemed promising at first, but upon refreshing the browser, a new problem emerged. The parent element retained its hover state background color, but th ...

Create Stunning Half-Brick Image Layouts Using HTML and CSS

Can anyone offer advice on creating complex image layouts such as the half-brick pattern using HTML, CSS, and jQuery? I am looking to be able to rotate and scale the image as well. The background-image CSS property does not seem to support scaling. I have ...

Can one intercept the window.location.replace event?

Suppose I am currently browsing the URL "example.com/somepage#somehash" and then decide to execute window.location.hash = "anotherhash", the URL will be updated to "example.com/somepage#anotherhash". This action triggers the window.hashashchange event. On ...

Achieving optimal performance with scoped CSS in Vue.js

As I work on creating a new app using VueJs, I have noticed the implementation of "css scoped" as shown below: <style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </template> ...

What is the best way to align an image with the position of a div?

Looking for assistance on positioning an image to a div's position after it has been cloned. $(".raindrop1").clone().removeClass("raindrop1").appendTo("body"); $("img").css({"left": "div".x, "top": "div".y}); .shape{ border-radius: 50px; width: 10p ...

Having difficulty aligning ListItem to the right within a List

I am working with an array that contains objects which I need to display in ListItems of a List. My goal is to show these ListItems from the array Objects in a layout where odd numbers are on the left and even numbers are on the right. However, I am facing ...

Implementing the Disabled Attribute on a Dynamically Generated Button Using React

My QWERTY keyboard is dynamically rendered through a component as Bootstrap buttons using bootstrap-react. Each button does not have an ID to avoid relying on IDs in React. When a letter button is clicked, it triggers an onClick event passed as props back ...

Incorporate an XNA game directly into a website

Is it possible to launch an XNA game directly from a web page without the need for any installations? Something along the lines of Java, Silverlight, or similar to WPF Browser Applications (XBAP). Situation: Visit a webpage, Prompted to install the g ...

Troubleshooting problem with CSS scaling on smartphones

I'm attempting to create a website (the first one I've ever designed) dedicated to my girlfriend's cat. However, when viewed on a mobile device, it doesn't look good. I've made an effort to adjust the meta viewport tag, but it does ...

When viewing in full screen, the page cuts off abruptly in the middle

Having trouble creating a login page for my capstone project as the form I designed isn't filling up the fullscreen. I created a login form with Bootstrap columns but it only takes up half of the screen, leaving the other half white. body { ...

Are the menubar menus positioned beneath a different div element?

Currently facing an issue with an HTML Menubar where the menus are appearing below another div, and not getting displayed as expected: ...

Alter the border hue of the checkbox and radio button

Is there a way to modify the border color of checkboxes and radio buttons (both circle and rectangle) using CSS? I've attempted several methods with no success. Can someone provide guidance on this? Any advice would be greatly appreciated. Thank you ...

Unable to display Bootstrap 5 modal

I am currently in the process of constructing a basic webpage that includes a navigation bar and a table. The table rows are being dynamically generated through a simple JavaScript script that fetches data asynchronously from a database. I opted to utilize ...

Boost Page Text Size Using CSS

Possible Duplicate: Allowing users to adjust font size on a webpage My goal is to create webpages where users can click an image to increase the font size of all content. Is this achievable? If so, how would I go about implementing it? I am aware that ...

Is the click count feature malfunctioning?

My goal is to track every mouse click made by the user, so I created a function for this purpose. However, it seems that the function is only counting the first click. To store the count values, I have created a variable. <!DOCTYPE html PUBLIC "-//W3 ...

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

Guide to adding checkbox IDs and inserting them into a textarea

Hello, my name is Prasath and I am currently learning about AJAX. I am working on a project involving seat layouts and finding it challenging to retrieve both the seat fare and seat number simultaneously. Is there anyone who can assist me in resolving this ...