aligning adaptable grid on the screen

Is anybody able to assist me in vertically centering the responsive matrix below so that it is equidistant from the left and right edges of the screen (parent element has a width of 100%)? Any help would be greatly appreciated. Here is my code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>image matrix</title>
    <style>
        html, body{margin: 0; width: 100%; height: 100%;}
        .container {
            width: 100%;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
        }

        .cell {
            border: 0px solid darkred;
            height: 50px;
            width: 150px;
            margin: 20px;
            background-color: #058;
        }
    </style>
</head>
<body>

<!-- this program illustrates responsive design without the use of media queries -->

    <div class="container">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>

</body>
</html>

Answer №1

By utilizing Flexbox, you can easily achieve centered cells without the need for media queries. Check out the code snippet below:

 
        html, body{
          margin: 0; width: 100%;  
          height: 100%;
        }

        .container {
            width: 100vw;
            display: flex;
            flex-wrap:wrap;
            justify-content:center;
        }

        .cell {
            border: 0px solid darkred;
            height: 50px;
            margin: 20px;
            background-color: #058;
            flex-basis:150px;
        }
<!-- This example demonstrates responsive design using Flexbox -->

    <div class="container">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>

CODEPEN LINK: https://codepen.io/emmeiWhite/pen/vYeZayL

Answer №2

To center your container, simply add the CSS rule margin: 0px auto ;.

      html, body{margin: 0; width: 100%; height: 100%;}
        .container {
            width: 60%;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
            margin: 0px auto ;
        }

        .cell {
            border: 0px solid darkred;
            height: 50px;
            width: 150px;
            margin: 20px;
            background-color: #058;
        }
<!-- this program demonstrates how to create a responsive layout without media queries -->

    <div class="container">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></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

Image tinting color picker tool (using HTML, CSS, and web technologies)

I'm currently exploring ways to apply a filter or tint on top of an image using CSS/HTML or some lightweight solution. For example, Ideally, my goal is to only have one image displayed on the page and overlay it with a transparent filter based on a ...

Having trouble selecting a button using xpath in Scrapy Python?

I am currently attempting to scrape quotes from a website called using my spider code that looks something like this: class quote(scrapy.Spider): name = 'quotes' # defining Name start_urls = [''] # Targeted urls def parse(s ...

Click to reveal sliding menu bar

Trying to create a sliding menu bar (.top-bar) that activates when clicking an arrow (#hide), causing the arrow to also slide up and switch from an up arrow to a down arrow. Here is my unsuccessful attempt: $(document).ready(function(){ $("#hide").c ...

Applying toggle() using css to manipulate various elements at once

Is there a way to toggle multiple divs of varying heights, all with the same class? What is the most effective approach to achieve this? http://jsfiddle.net/hS6RH/41/ $('.smallify').on('click', function() { if ($(this).parent(&apo ...

What are the steps to design a versatile gallery page that can serve various projects?

Allow me to get straight to the point. What is my goal? I am aiming to develop galleries for various projects. Each project's thumbnail on the main page should open a gallery as a new page. These galleries will all have the same layout but different ...

Unusual glitch with paint/rendering in Safari while navigating through a lengthy list of content

Encountering a peculiar problem where a page displaying a grid of boxes is experiencing issues in Safari, Chrome on iPhone 11 Pro, iPhone 13 Pro, and Chrome on Pixel 4a. Other devices are not affected. Symptoms include unresponsiveness and blank sections a ...

Enhancing User Experience on Android Devices: Automatic Website Zoom-In Feature for Responsive Design

For the first time, I am delving into creating a responsive website design. I seem to be encountering difficulties when it comes to smartphones. I have been using the following meta-tag to ensure that the website loads "zoomed in". (I found this method on ...

Loading Web Applications Screen

My web app contains multiple tree views. Upon loading the page, I first see the unordered lists before the styling of the trees is displayed in the DOM after a brief delay. Is there a method to mask the web app with a spinner placed in the center of the s ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...

Exploring Webpack: Combining CSS-modules with external CSS imports - A comprehensive guide!

In my React application using css-modules, I encountered an issue when trying to import CSS files from external libraries. Due to the inability to globally wrap them in a :global block, webpack interprets them as styles intended for objects only. Is there ...

Prioritize the first child in Flexbox by truncating its text before the second

I am working with a flex-box container named .search-bar, which contains two divs with text, labeled as .title. My goal is to make the second child take up any extra space available and only start ellipsizing when there is no more space left for the first ...

Implementing jQuery selector for CSS and properly handling special characters in the code

I've been attempting to use jQuery to change the background color of a radio button. I provided the CSS code below, but even after escaping special characters in jQuery as shown below, the solution still isn't working. #opt5 > [type="radio"]: ...

jQuery and Bootstrap collide

Here is my jQuery code that toggles visibility of different divs based on a click event: $(function () { $(".kyle-div, .tracey-div, .frank-div, .rosie-div").hide(); $("a").bind("click", function () { $(".conor-div, . ...

Can CSS be utilized to define the dimensions and background of a <div>?

Does applying inline styles like <div style="width: ;height: ;background: "> fall under the realm of CSS? ...

Utilizing the CSS :not() selector within a nested table

I am having an issue with my AjaxControlToolkit where the main-page CSS is affecting the inner "calendar" popup. To illustrate what I need, what I've tried, and the temporary workaround, I have simplified everything into a one-page example. I am in s ...

"Refreshing Your Internet Experience with Netbeans Chrome: A Guide to Clear

While developing an HTML / CSS / Javascript / PHP app in Netbeans, I've noticed that making changes to the HTML requires me to 'clear browsing data' in Chrome for the updates to show. I believe it's the 'cached images and files&apo ...

Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry. I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified. Howe ...

A rightward sliding submenu that appears upon clicking a button

I have a vision of creating an admin control panel for my website, featuring a set of buttons. I envision that when one of the buttons is clicked, the corresponding sub-menu will appear alongside it. For instance, if "My Account" is selected, its sub-menu ...

Issue with CSS color gradient transparency

I've successfully implemented a gradient that transitions from transparent to white by using the following CSS code: linear-gradient(to right, transparent, white) If you want to see it in action, click on this link: http://jsfiddle.net/fs8gpha2/ Al ...

Verify whether the content within the Div has been modified

I am currently making changes to content within a <div> element. I would like to determine if the data in the <div> has been modified and, if so, save it in a session to persist on refresh. How can I verify if the data has been edited and then ...