What is the best way to ensure that my divs adjust to fit the remaining space, irrespective of the number I create

I'm working on creating an etch-a-sketch in the browser. Right now, it only works with a size of 7200, but I want to make it more dynamic. I'd like users to be able to input any number and have the grid adjust accordingly. For example, if they enter 16, it should create a 16x16 grid, or if they enter 100, a 100x100 grid. Also, how can I ensure that my div elements adapt to fill the screen no matter how many are generated?

Here's my HTML:

<DOCTYPE html>
<html>
<head>
    <title>Project</title>
    <script type="text/javascript" src="JS/jquery-1.12.0.js"></script>
    <script type="text/javascript" src="JS/etch.js"></script>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
    <div id="wrapper">
        <div class="button">
            <button type="button" id="clear-button">Clear Board</button>
        </div>
        <div id="game"></div>
    </div>
</body>
</html>

My CSS looks like this:

#wrapper {
    width: 1000px;
    height: 760px;
    background-color: gray;
}

.grid {
    width: 10px;
    height: 10px;
    float: left;
}

.draw {
    background-color: black;
}

.button {
    width: 1000px;
    height: 40px;
}

#clear-button {
    margin: auto;
    display: block;
}

Finally, here's my JavaScript code:

var count = 0;

$(document).ready(function() {

    while(count < 7200){
        $('#game').append('<div id="grid-"'+ count +' class="grid"></div>');
        count++;
    };

    $('.grid').on({
        mouseenter: function () {
            $(this).addClass('draw');
        },
        mouseleave: function () {
            $(this).addClass('draw');
        }
    });

    $('button').click(function() {
        $('#game').empty();
        count = 0;

        var size = prompt("What size would you like the new etch-a-sketch to be?");

        while(count < size){
            $('#game').append('<div id="grid-"'+ count +' class="grid"></div>');
            count++;
        };
        $('.grid').on({
            mouseenter: function () {
                $(this).addClass('draw');
            },
            mouseleave: function () {
                $(this).addClass('draw');
            }
        });
    });
});

Answer №1

It may be of interest to delve into the realm of flexbox layouts. Here’s a simple example showcasing how you can incorporate numerous elements in a single row.

.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 100%;
    height: 250px;
    background-color: lightgrey;
}
.flex-item {
  -webkit-flex: 1;
    flex: 1;
  margin: 10px;
  background: blue;
}
<div class="flex-container">
  <div class="flex-item item1">flex item 1</div>
  <div class="flex-item item2">flex item 2</div>
  <div class="flex-item item3">flex item 3</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

What is the best way to access POST request data in a Node JS controller?

Journey on the Path (routes/account.js) const express = require('express'); const router = express(); var account_controller = require('../controllers/accountController'); router.post('/account/Getuser', account_controller. ...

Can AngularJS filter be used with an array of strings for sorting?

Understanding how to implement an Angular filter to solve a problem I'm facing is proving to be quite challenging for me. Let's take a look at a simple example of my data structure, which consists of an array of tasks: var tasks = [ { Title ...

Assistance needed in retrieving an element using jQuery

After posting a similar question and receiving a correct answer that didn't quite meet my needs, I realized it was my mistake. Imagine having the following HTML: <TD nowrap="true" valign="top" width="190px" class="ms-formlabel"> <H3 class=" ...

Troubleshooting Issues with Uploading Images to Firebase Storage Using ReactJS, JavaScript, and Firebase

Despite watching countless tutorials and conducting thorough research, I continue to encounter errors in my code. One recurring error can be seen in the image below: https://i.sstatic.net/AmG5D.png In one file, I include the line import firebase from &apo ...

"splintering" HTTP requests

Currently, I am working on an Angular application that retrieves data from a REST server. Each piece of data we retrieve contains essential "core" information for basic representation, as well as additional "secondary" data like comments and other optional ...

Enhancing nested data in Firebase

According to the information from the Firebase note: When using a single key path such as alanisawesome, the updateChildren() method will only update data at the first child level. Any data passed in beyond the first child level will be treated as a setVa ...

Is it possible to utilize a WordPress page as a CSS stylesheet?

Is there a way to customize my WordPress theme's CSS through a Page in the backend rather than editing the default style.css file? I've seen it as a page template before but I'm having trouble figuring it out on my own. I'm currently r ...

Different ways to incorporate PHP into JavaScript

<script> $(document).ready(function(){ $("#receipt").click(function(){ var level_var = $("#receipt").val(); <?php date_default_timezone_set('Asia/Kolkata'); include ('config/conn.php'); $sql = "SELECT * FROM fixtt where acode ...

How can I prevent my JSON object from serializing .NET nulls as "null" for object members?

I am currently utilizing WebMethods to retrieve an array of a custom class. When this array is returned from a Jquery .ajax call, it gets serialized into a JSON object that can be utilized with Javascript in my ASP.NET application. The issue I am facing is ...

The navigation tabs in Bootstrap are not functioning properly and are failing to show the corresponding content. Is anyone else facing this issue with Electron.js

I'm having trouble understanding why The tabs aren't changing when clicked The tabs aren't showing any content I'm working with electron and bootstrap, so I'd really appreciate any recommendations or solutions. I've looked ...

Bootstrap4: What could be causing the adjacent row cell to remain left-justified instead of being centered as intended with mx-auto?

I've been working on creating a responsive header using Bootstrap4. The main goal is to have the logo displayed as left-justified text and the menu items as right-justified when the viewport is large, then transition to both being centered in a "small ...

Leveraging the power of Bootstrap 4 to place the footer beneath all remaining content on the

I am currently working on a React application that consists of a header, container, and footer. To add animation to the route loading process, I had to apply position: absolute to the container section of the page. Within the container, there is a row wit ...

What is preventing me from accessing session data using nuxtServerInit?

When attempting to retrieve sessions, I encounter an issue. Is everything set up correctly in the following main files: server, config? store/index.js export const actions = { nuxtServerInit ({ commit }, { req }) { console.log(req); } The console log ...

Dynamically scrolling using SuperScrollorama and Greensocks

I'm currently facing a JavaScript animated scroll challenge that has me scratching my head. My approach involves using the SuperScrollorama jQuery plugin, which relies on the Greensock JS tweening library. The main effect I'm aiming for is to " ...

The website flickers in and out as it loads

My site keeps flashing whenever it loads. Despite trying the solutions recommended in this stackoverflow post, I have had no success. Every page on my website loads a nav.html file using this code: $.get("nav.html", function(data){     $("#nav-placeho ...

Using a `right: 0` value will not be effective when using absolute positioning

Currently, I am in the process of developing a website utilizing Bootstrap 3.3.6. After going through a tutorial on how to create a responsive banner slider, I found this resource helpful: http://www.jqueryscript.net/slider/Full-Width-Responsive-Carousel- ...

Exceeded maximum file size event in Dropzone

I am currently implementing dropzone in my form to allow users to upload images. In the event that a user selects a file that exceeds the configured limit, I want to display an alert message and remove the file. Below is my current configuration: Dropzone ...

Can you provide instructions on how to use JavaScript to click and hold a button for a specific duration?

Is it possible to use jQuery to create a button that, when guest button number 1 is clicked, will automatically click and hold down button number 2 for about 3 seconds? I have tried using the mousedown() and click(), but they only register a click, not a ...

Retrieve a solitary row of information from a MySQL column

Here is an example of a MySQL database with the main table containing fields such as id, name, age, and photos: Table main: 3 John 22 photo1.jpg photo2.jpg photo3.jpg ph ...

tips for efficiently loading JSON files using Angular

I currently have a situation where multiple controllers are calling the method getData() from a service. To prevent unnecessary duplicate http calls for the same json file, I have implemented the following approach: QuizApp.service('quizService' ...