What techniques can be used to address side margins when working with "CSS-Grid" without relying on Margin: auto;?

Currently I am engaged in a small project where I am required to design a responsive "CSS - GRID" layout specifically for the mobile version.

I have observed that besides utilizing margin: auto;, the sole method to create borders is by employing 1fr units with a middle column of at least 20 FR.

For instance, grid-template-columns: 1fr 20fr 1fr; (Employing this technique causes the borders to vanish automatically on mobile devices).

I'm interested to find out if there are any better alternative approaches to achieve this?

Answer №1

It's interesting that you bring this up. I was just pondering over the same topic recently. I've been trying to figure out the best way to create a smooth transition from desktop to mobile responsiveness. My initial approach was to utilize margin: auto;, which worked as expected:

https://i.sstatic.net/o7YEG.jpg

The traditional setup is straightforward, and the same holds true for CSS grid (for example, with grid-template-columns set to 1fr 800px 1fr)

https://i.sstatic.net/I2LZu.jpg

However, the challenge arises when transitioning to mobile devices and smartphones:

On smaller screens, the large side margins are no longer necessary. So, you're left with 1fr columns. My personal approach has been to eliminate margin: auto and rely on flexible fr values for column widths, but it requires some trial and error to find the optimal balance:

https://i.sstatic.net/vKmBK.jpg

This seems to be a common solution that many people adopt. Grid layout involves a shift in mindset towards relative proportions rather than pixel-perfect design.

Aside from this, I haven't come up with any other solutions. Personally, I like using 1fr columns, especially when they include a small margin which enhances the overall look of the page. But, of course, this is just my opinion.

I found a helpful CSS grid article that provided valuable insights into this concept. Hopefully, it will be useful to you as well. Cheers.

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

The issue of 'position: fixed' not properly functioning within the Materialize CSS modal

My goal is to have the header of the Modal stay fixed while scrolling, but for some reason, the option position:fixed; is not working. I am currently using Materialize CSS to build the modal and surprisingly, position:sticky; is working. $(document).rea ...

What is the best way to incorporate a style attribute into my EJS page content?

Having trouble with adding custom style. It seems to work, but there's an error displayed. Apologies for my poor english :( <div class="card card_applicant"> <div class="card_title" style="background-c ...

Effortlessly Transition to Full Screen with Div Expansion on Click

I'm currently working on creating a smooth transition for a div to expand fullscreen when clicked. My goal is to achieve a similar effect as the case studies on this website: Although my code can make the div go fullscreen, there's an issue with ...

The width of my root container does not display at a full 100% in mobile dimensions

After creating a simple React.js project, I set up the folder structure using `npx create-react-app`. Additionally, I added some styling with `* { margin: 0; padding: 0; box-sizing: border-box }`, and specified background colors for the body and #root elem ...

Convert JSON data into a nested unordered list

I need help converting a JSON object into an unordered list using jQuery. Here's the JSON data I have: var myJSON = "{name:\"Director\",children:[name:\"Exe Director1\",name:\"Exe Director2\",name:\"Exe Director3&bs ...

Typescript tutorial: Implementing a 'lambda function call' for external method

The Issue Just recently diving into Typescript, I discovered that lambda functions are utilized to adjust the value of this. However, I find myself stuck on how to pass my view model's this into a function that calls another method that hasn't b ...

Display the user's chosen background image as the backdrop for my activity

As I've been exploring various corners of the internet, I've come across a potential technique for accessing the user's background from within my activity. I've brainstormed a couple of ideas (even though they might seem silly, just to ...

Evaluation of HTML5 file upload functionality with unit testing

How can I perform JavaScript unit testing for file uploads using the HTML 5 File API? Let's consider the following code: <form method="POST" enctype="multipart/form-data"> <input type="file" id="fileselec ...

Struggling to implement a seamless scrolling effect using CSSTransition Group

In my quest to achieve a smooth vertical scroll effect for content appearing and disappearing from the DOM, I've turned to CSSTransitionGroup (or ReactTransitionGroup). While I'm familiar with CSS animations for such effects, I need to implement ...

Is it recommended to place JavaScript/CSS at the bottom of an HTML document?

I am currently utilizing jQuery Mobile, and at the moment, my <head> section looks like this: <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>help</title> <link ...

Encountering challenges with incorporating SVG elements in HTML and styling them with CSS

There seems to be an issue that I am encountering. I have incorporated an SVG in my HTML page, but it doesn't seem to be visible. The background of the page has a gradient effect applied to it. <div id="logo"> <svg class="icon icon-outli ...

The loading bar function seems to be malfunctioning when paired with the navbar element <nav> in an HTML document

Can you assist me with a coding issue I am facing? I have implemented a loadingbar on my webpage similar to YouTube, and it works perfectly on the page itself. However, when I try to integrate it with a navigation bar, it stops working. Below is the code ...

No routes were found that match: ""

app.routes.ts import { ModuleWithProviders } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { PushComponent } from './push/push.component'; const appRoutes: Routes = [ { path: ...

"An innovative social media platform feature resembling a Linkedin/Facebook post box

I am looking to develop a post box feature similar to LinkedIn and Facebook. When a URL is pasted into the box, I want a thumbnail to be generated. Two main questions: - Is there an existing component that already does this? I have searched extensively b ...

Is there a way to automatically redirect the main html page to a different html page upon logging in?

I have created a main page in HTML with a login box that displays a message saying "Login successful" or "Login failed" based on whether the password entered is 8 characters or more. The validation function for this works correctly, but after successfully ...

Produce an additional page while remaining on the present one

I have a PHP-based invoice system that displays all customer invoices. I am interested in adding a feature that allows users to print an invoice directly from the list page without navigating away. While I am familiar with window.print() and media="print" ...

Header that stays in place even when there is too much content to fit horizontally

On my HTML page, I have a main header and body. Inside the body, there is another header and body that overflows horizontally. I want to ensure that no white space appears on the right side of both headers when the inner body scrollbar is moved right. Ins ...

Retrieve an identification number from the Request.QueryString

Hey there! I'm having trouble reading the id from Request.QueryString within an HTML tag in ASP.NET. The code I've been using is shown below: <video src='<%# "VideoHandler.ashx?id=" + Request.QueryString["id"] ' width="220" heig ...

Is it possible to modify the CSS of a parent element in vue.js only for the current router route?

I am trying to modify the parent component's style without affecting the CSS of other components. Here is an example from my code: App.vue <div class="page-content"> <router-view ref="routeview"></router-view> </div> rou ...

Showcasing the Contrast in Appearance between Chrome and Firefox on Wordpress

Is there anybody who can help me out? I am currently using a "pullout" widget on my Wordpress website to show an inline link that opens specific text in a prettyPhoto box with scrollbars. The setup works flawlessly in Google Chrome, but when I view the pa ...