What is the best way to ensure that my grid remains contained within its designated area?

I need to incorporate a grid of 16x16 or 64x64 into my layout without it overflowing. I'm considering using flexbox, but unsure of the implementation process. My goal is to have the grid resize based on the container size rather than exceeding its boundaries.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style.css">
    <script src="./index.js" defer></script>
    <title>Document</title>
</head>

<body>
    <div class="flex-container">
        <div id="container"></div>
    </div>
</body>

</html>
CSS:
:root {
    --grid-cols: 1;
    --grid-rows: 1;
}

#container {
    background-color: white;
    width: 50%;
    height: 100%;
}

.flex-container {
    background-color: coral;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 70%;
}

#container {
    display: grid;
    grid-gap: 1em;
    grid-template-rows: repeat(var(--grid-rows), 1fr);
    grid-template-columns: repeat(var(--grid-cols), 1fr);
}

.grid-item {
    padding: 1em;
    text-align: center;
}

.grid-item:hover {
    background-color: beige;
}
Javascript:
const container = document.getElementById("container");

function makeRows(rows, cols) {
    container.style.setProperty('--grid-rows', rows);
    container.style.setProperty('--grid-cols', cols);
    for (c = 0; c < (rows * cols); c++) {
        let cell = document.createElement("div");
        container.appendChild(cell).className = "grid-item";
    };
};

makeRows(16, 16);

Answer №1

Adjust the size as per your requirement

Try changing the following specifications to customize your grid layout:
  grid-template-columns: repeat(auto-fill,minmax(200px,1fr));
  grid-auto-rows: minmax(100px, auto);

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

Avoiding page refresh while utilizing the ng5-slider component in Angular

I am currently working with an ng5-slider that has a customizable range from 0 to 1000. However, I have encountered an issue when adjusting the slider at the bottom of the page - it refreshes and automatically takes me back to the top of the page. I would ...

What is the best way to view all of the objects in an array?

My challenge involves an array consisting of ten objects, each with six properties to be displayed on a view. I want users to have the ability to update these properties by entering new data into inputs. How can I efficiently monitor the entire array to ...

"Unveiling the mystery of ejs form submissions with undefined values

I'm facing a bit of difficulty and need some guidance. I am attempting to develop a URL shortener using Node, Express, and Ejs. However, I'm encountering an issue where my EJS form is sending undefined values. Below is the snippet of my EJS code ...

Using Jquery to dynamically add or remove a class based on scrolling behavior

Can someone help me with identifying the position of an element so that when the user scrolls to it and it appears on the screen, an icon can be highlighted? Currently, the script I am using adds the class too early, closer to the bottom of the block. I w ...

What is the best way to retrieve declared functions within the done() scope of a jQuery plugin that handles JSON requests?

As I work on a jQuery plugin, my goal is to make it easily configurable and maintainable. To achieve this, I am focusing on keeping the functions short and testable. To accomplish this, I am implementing jQuery plugin code inspired by Addy Osmani's L ...

Is there a method in AngularJS to have $http.post send request parameters rather than JSON?

I have come across some older code that utilizes an AJAX POST request using jQuery's post method. The code looks something like this: $.post("/foo/bar", requestData, function(responseData) { //do stuff with response } The request ...

How can I set the textbox focus to the end of the text after a postback?

In my ASP.Net form, there is a text box and a button. When the user clicks the button, it adds text to an ASP:TextBox during a postback (it adds a specific "starter text"). After the postback, I want the focus to be set to the end of the text in the textbo ...

Is there a problem with the layout or indexing?

My website features a div that animates in using a jQuery click function, appearing over other divs that have animated in on $(document).ready(). Strangely, this div shows up behind the others only on the home page of my website and not on any other pages, ...

The text is obscured by the overlapping div within the parent div

Here is the code snippet I am working with: .pa { width: 400px; background: #008000; position: relative; } .icon-wrap { height: 100%; float: right; width: 30px; background: yellow; padding: 5px; position: absolute; top: 0px; right: ...

Enhance Website User Experience: Keeping Track of Scroll Position When Users Navigate Back in Browser

Have you ever experienced the frustration of scrolling through a page with infinite scroll, only to click on a link and then go back to find that you've lost your place in the scroll? You're scrolling down a lot, You click on a link from the i ...

What are the best methods for rebooting a Node.js project?

As a developer with over 8 years of experience in PHP, I find myself needing to make changes to a live Node.js project, despite not being a Node.js developer myself. The task at hand is simply to change a payment gateway token, which should be a straightfo ...

What is the best way to extract a portion of a JSON string?

this.on('success', function(file, responseText) { var theID = JSON.stringify(responseText); alert(theID); window.location.href = ('want to put something here'); }); The ...

Is it possible to combine multiple filter examples together?

Could you please provide some examples or references related to the next task: var planets = [{ residents: [{ name: 'Mars' }, { name: 'Jupiter' }, ...

When a link is right-clicked, the window.opener property becomes null

Currently, I am utilizing the window.opener property in JavaScript to update the parent window from a child window. The parent window simply consists of a table with data and a link to open the child window. When the child window is launched, a JavaScript ...

What is the correct way to access an element with spaces in handlebars while working with node.js?

I have an object containing values with spaces in strings. For example: object = { "group of people": [ { (...) } ], "another group of people": [ { (...) } ] } Now, I want to use this object with the handlebars helper block in my view ( ...

Accessing data points in jQuery

Here is a sample code I'm working with : <script type="text/javascript"> var currentPicture;//default picture var picEL;//the image viewer element jQuery("#backImageShower").hover( function(i) { picEL = j ...

JavaScript menu that pops up

Hello everyone, I've recently created an HTML5 file that, when clicked on a specific href link, is supposed to display an image in a smooth and elegant manner. However, so far it hasn't been working for me as expected. Instead of smoothly popping ...

Validation message causing Bootstrap form inline to lose center alignment

Could someone please assist me with form inline in Bootstrap? I am trying to ensure that all inputs are of the same height (or centered), but I'm facing an issue when a validation message is displayed. DEMO: http://codepen.io/Tursky/pen/gpvgBL?editor ...

Deleting lines from JSON object using Angular or JavaScript

On my webpage, I have a list where you can add a new line by pressing the "+" button (easy with push), but I'm not sure how to remove lines using the "X" button. https://i.stack.imgur.com/nm06A.png This is the JSON structure: "priceExtra" : [ ...

Converting Cookies to Numeric Values in JavaScript: A Step-by-Step Guide

I'm currently developing a cookie clicker website and am encountering an issue with saving the user's score to localstorage when they click the "save" button. Here is what my code looks like: let score = 0; function addPoint() { score += 1; } ...