What is the best way to dynamically adjust the size of a grid of divs to perfectly fit within the boundaries of a container div without surpassing them?

Currently, I am working on a project for "The Odin Project" that involves creating a web page similar to an etch-a-sketch. I have made some progress, but I am facing a challenge with dynamically resizing a grid of divs.

The issue lies with the container div that I have set to 500x500px. Despite several attempts, the divs within the container do not fill up the space as intended. It seems like a simple CSS adjustment is needed, but I can't seem to pinpoint the exact solution.

CSS:

 .outer {
    width: 500px;
    height: 500px;
    position: absolute;
}

#gridtable {
    width: 100%;
    height: 100%;
}

.outtable {
    background: #CCC;
    min-height: 1%;
    height: auto;
    min-width: 1%;
    width: auto;
    position: absolute;
    display: inline-block;
}

.newcolor {
    background: blue;
    min-height: 1%;
    height: auto;
    min-width: 1%;
    width: auto;
    position: absolute;
    display: inline-block;
}

You can find the JSFiddle here.

Answer №1

Ensure your table elements are set to occupy the full available width (i.e. 100%), as they currently default to the minimum required space (this is the default behavior for tables).

.outer {
    width: 500px;
    height: 500px;
   position: relative;
    overflow: hidden;
}

#gridtable {
    width: 100%;
    height: 100%;
}

table, tbody, tr, td{
    width: 100%;  

}
.intable{display: flex; }
.intable td{}
.outtable{
    background: #CCC;
    min-height: 1%;
    height: auto;
    min-width: 100%;
    width: auto;
    position: absolute;
    display: inline-block;
}

.newcolor {
    background: blue;
    min-height: 1%;
    height: auto;
    min-width: 100%;
    width: auto;
    position: absolute;
    display: inline-block;
}

jsfiddle: http://jsfiddle.net/o4poarey/6/

Answer №2

Avoid using pixel values, go with percentages

<...width="100%" height="100%"...>

Instead of

<...width="100px" height="100px"...>

Additionally, enclose in a table and make the table width 100%

<table style="width:100%">
    <tr>
        <td><...width="100%" height="100%"...></td>
    </tr>
</table>

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 with HTML5 video tags on iPhone Safari is that 'a' links are not functioning correctly

For my mobile website in Safari browser, I am utilizing the html5 video tag. While the video is functioning properly, I have encountered an issue with 'a' links overlapping on top of the video. These links have been added using absolute positioni ...

Incorporating form fields for editing into Symfony 2's database

Can someone help me troubleshoot my issue with Symfony2? I'm new to this framework and struggling with a problem in my twig file. I have a form where I enter a test name and the names of its categories, along with a color for each category. However, w ...

The Angular Google Maps Directive zooms in too much after the "place_changed" event has fired

Currently, I am developing a store locator app for DHL accessible at storefinder.hashfff.com/app/index.html For this project, I decided to utilize the angular-google-maps library for its features. However, in hindsight, working directly with the Google Ma ...

Ar.js results in objects experiencing deformation or modification when manipulated

Recently, I started experimenting with Ar.js and encountered an issue where the objects displayed on the screen seemed distorted. To illustrate this problem, let me share a basic A-Frame example featuring a perfectly round sphere: <!DOCTYPE> <html ...

InvalidSelectorError: The specified selector is not valid //button[contains(., 'buttonText')] while running JavaScript code

Here's an example of HTML code that I am working with: <button id="toolbar-item-17-update-id" type="submit" name="action" value="update" class="button-action-update btn btn-secondary"> Ed ...

Skrollr's transformation effect makes the text tremble

Using skrollr.js, I am implementing transition effects on an inner div nested inside another div. The inner div is positioned relatively. Here are my codes: HTML Structure <div class="outer"> <div class="inner-div" style="transition:transform ...

Incorrect Matching with Regular Expressions

I'm working on a regular expression to validate a URL. var url = "https://www,google.com"; var urlRegex = /(https|http)?:\/\/(?:\w[\-\w.]+)(?:\/[\-\w+&@#\/%=~_|!:,.;]*)?(?:\?[\-A-Z0-9+&@# ...

Using ASP.NET to validate a radiobutton list using JavaScript, followed by invoking a C# function

After experiencing some errors, I have refined my question to better articulate my objective. By revising the logic of my previous issue - where no text appeared in a pop-up dialog triggered by a C# script utilizing a JavaScript method - I present my updat ...

Using ReactJS, Document.ready functions are essential for initializing the application

Exploring ReactJS for the first time, working on a project and encountering a challenge. I've created an app with a user authentication feature. Once the user logs in, they should be able to access a slide-up navigation menu that is controlled by ex ...

Add a script tag to every image in my HTML at once using C#

Managing a site with a thousand images can be overwhelming, especially when trying to add a script tag like <%=Settings.MyDomain%> to each one. I'm looking for a more efficient way to accomplish this task without spending hours on manual labor. ...

Having trouble integrating a custom dialog with DirtyForms plugin

I have successfully implemented DirtyForms, a plugin that triggers a confirmation dialog when a user tries to navigate away from a page. The base functionality of the plugin is working as intended. Now, I am trying to integrate jQuery UI's Dialog for ...

Add a dynamic widget to a specific element in the document object model in real time

Is there a way to dynamically create a jQuery UI widget using a string? You can easily do this by following the example below: $(function() { $("selector").widget() }) However, what I am trying to accomplish is slightly different. Here is an examp ...

Text fields do not show a cursor icon

On the website http://www.legrandclub.net, there are two text fields. Everything works fine in all web browsers except for Internet Explorer. When I click on one of the text fields, the cursor is not displayed, although it is possible to write text. What c ...

A guide on updating various states using React Hooks

Creating a background component with the use of Vanta in NextJS, here's the code snippet: import { useEffect, useRef, useState } from "react"; import * as THREE from "three"; import FOG from "vanta/dist/vanta.fog.min"; im ...

Experiencing varying outcomes from a single form in Laravel

I am attempting to build an ajax form with Laravel. The interface consists of a table displaying names with buttons next to each name that are enclosed within forms to trigger certain actions. Below is the HTML code: <div style="margin-top: 100px;"> ...

Adjusting the transparency of the background without impacting the text itself

I've been struggling to create a loading screen with no luck. The animation itself looks fine, but the background is causing issues. Every time I try to adjust the opacity, it affects the text as well. @import url('https://fonts.g ...

What steps can be taken to avoid special characters in ion-input fields?

When inputting special characters into the field used for storing the alphanumeric serial number, they are accepted. I need to prevent special characters from being entered in the input field. <ion-input [(ngModel)]="serial_number" (ngModelCha ...

Using JavaScript to Access PHP Files

Would like to understand the process of calling a php file (test.php) from either "x:" or "y:" in the code below. var example1 = { x: ['giraffes', 'orangutans', 'monkeys'], y: [20, 14, 23], name: 'Most read', ...

Limit the maximum column gap on the grid and stop it from expanding further

I'm currently facing an issue with some columns/rows on my website that are keeping content locked in the top left corner, reminiscent of early 00's web design. The site was originally designed for smaller screens like 1024x764, so when viewed on ...

With vuejs, only one place can control the changing of v-model

Hello, I am currently working on integrating VueJS2 code with Laravel Blade. However, I have encountered an issue with the following VueJS2 code: new Vue({ el:'.add_item_to_price_menu', data:{ percentage:null, }, methods: ...