What is the best way to align dynamically generated divs seamlessly together?

After creating a function that generates a grid of divs placed in a container div upon document load or user reset, I noticed a gap between each row of divs. I aim for a flawless grid where each square aligns perfectly with the next. Despite trying various adjustments to borders, padding, and outlines without success, I believe there must be a simpler solution. Here's an example on jsfiddle: https://jsfiddle.net/psyonix/1g9p59bx/84/

var d = ("<div class='square'></div>");

function createGrid(numSquares) {
    var area = $('#g_area');
    var n = 0;
    var squareSize = (area.innerWidth() / numSquares);
    for (var i = 0, len = (numSquares * numSquares); i < len; i++) {
        area.append(d);        
    }

    $('.square')
        .height(squareSize)
        .width(squareSize)

#g_area {
    background-color: #C8C8C8;
    position: relative;
    width: 580px;
    height: 600px;
    border-radius: 5px;
    margin: 0 auto;
    overflow: hidden;
}
.square {
    display: inline-block;
    position: relative;
    background-color: #C8C8C8;
    outline-color: #000000;
    outline-width: 1px;
    outline-style: solid;
}

Answer №1

To solve the issue, you can simply eliminate the outline-width property from the .square class or assign it a value of 2px or 3px.

.square {
    display: inline-block;
    position: relative;
    background-color: #C8C8C8;
    outline-color: #000000;
    outline-width: 3px; //or 2px or remove completely like in my sample
    outline-style: solid;
}

SEE DEMO HERE

Answer №2

When you apply the inline-block property to square elements, they behave similarly to words within a paragraph. This means they are arranged in lines like a sentence, with the same line-height as regular text. One solution is to reset the line-height of the parent element containing these square "words."

#g_area {
    background-color: #C8C8C8;
    position: relative;
    width: 580px;
    height: 600px;
    border-radius: 5px;
    margin: 0 auto;
    overflow: hidden;
    line-height: 0px;
}

This adjustment allows you to compress the lines, eliminating the normal spacing between them.


Alternatively, you can achieve the same result by avoiding inline-block and using block and float. By changing the display property to block and floating the elements, you remove concerns about line-height and white spaces.

.square {
    display: inline-block;
    position: relative;
    background-color: #C8C8C8;
    outline-color: #bc0000;
    outline-width: 1px;
    outline-style: solid;
    display:block;
    float:left;
}

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

A tool developed in Javascript that allows for the conversion of .ini files to .json files directly on the client

Does anyone know of a JavaScript library that can convert .ini files to .json files on the client-side? I checked out this library, but it doesn't meet my requirements. Here is an example of an .ini file: [Master_Settings:1] Model Name=RC-74DL IP Ad ...

The Bootstrap form validation success message is failing to appear

I'm struggling with bootstrap form validation as the success message does not appear after submitting the form. I have a PHP mail function script that displays 'thank you' in a new page upon submission, even though I have included return fal ...

Issue with Angular ui-select causing repeated $http requests in ui-select-choices

I'm currently working on integrating ui-select into my project, and this time I need to pass a controller function as options to ui-select-choices. Here's how it's set up: HTML: <ui-select ng-model="selectedItem" theme="selectize" ng-di ...

"Troubleshooting: Why Won't insertAfter Function Work with

I have a p Element that I want to wrap inside a span tag. Then, I need to insert it after another p tag with the id output. Despite my attempts, the insertAfter function is not working as expected. $mytext = $("p#test").html(); $myspan = "<span styl ...

What is the proper way to implement if-else statements in JSX?

Is there a way to implement if else statements in JSX? I am currently using the following code snippet <h2 className='font-bold text-lx'>$ {item?.price == null && item?.type =='Sell' ? 'Please contact agency' : ...

Resolve problems with implementing dynamic routes in Next.js

I have been learning about Next.js and I am struggling with understanding how to set up dynamic routing. I have the following setup: https://i.stack.imgur.com/uBPdm.png https://i.stack.imgur.com/YYSxn.png "use client" import React from "reac ...

Switching Image Values in JavaScript: A Quick Guide

After successfully using a JavaScript function to swap the src of two images, I encountered an issue. I also needed to switch two values associated with the images so that I could calculate them later. For example: img src="ble.jpg" id="F" value="" onclic ...

Retrieve SQL data and store it in a JavaScript variable

Need assistance with fetching data from SQL and storing it in a JavaScript variable. I have already connected PHPMyAdmin to my website. I am attempting to retrieve two variables (date) from my SQL table. JAVASCRIPT: var countdown_48 = new Date; countdow ...

Choose all items on each page with Material-UI's table pagination

Can items be selected solely on the current page or per page within the Table? Check out this demo for reference. ...

Modify how various classes are shown when hovering over a pseudo class

This is my customized scss code .image-container { position: relative; .delete-image { display: none; position: absolute; top: 0px; right: 0px; } .make-primary { display: none; position: absolute; ...

Creating a Clickable SVG Element in React

I've been attempting to set up an onClick event in React, specifically within an SVG that includes multiple circle objects. However, when using "onClick," I'm encountering a strange issue: Upon data load, the onClick event is triggered six times ...

Guide to incorporating navigation buttons (back and forward) in a modal box

I have successfully created image thumbnails and linked them using the provided code. <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> <script src="https://maxcdn.bootstra ...

The destroySlider() function of BxSlider fails to work due to either an undefined slider or a function that is not

I'm facing an issue with my carousel setup using bxslider. Here is the code snippet responsible for initializing the carousel: jQuery(document).ready(function() { var carouselWidth = 640; var carousel; var carousel_Config = { minSlides: 1, ...

Transform a single data point into pixels by converting its latitude and longitude coordinates

I am facing a specific challenge where I have hit a roadblock after conducting some research. The issue at hand is this: I am working with a raster image that has known dimensions (800 x 800 px) I have two points within this image with their pixel and g ...

What is the best way to split two sets of radio buttons with the same name into distinct blocks in an HTML form?

For my project, I am working with two sets of radio buttons where the values are stored in a Database. Depending on another result in the HTML form, I need to display one set of radio buttons or the other. The issue arises when using the same name for all ...

Troubleshooting Problem with Angular JS Ng-Repeat

I have a specific situation where I want to showcase the elements that exist in only one array. If an element is also present in another array, there is no need to display it. My HTML structure looks like this: <div ng-repeat="array1Value in array1"&g ...

What's the reason for the inability to resize fieldset using both?

Can someone explain why the resize control is not enabled on the fieldset element? The documentation mentions that resize does not work with: Inline elements Block elements that have overflow set to visible fieldset { resize: both; overflow: h ...

Ways to modify the end result using callback information

In my scenario, I am working with an object that has keys id and name as shown below. The goal is to retrieve the customer name based on the id and then update the object accordingly. Const arr=[{id:1,name:''},{id:2,name:''}]; ...

Looking for assistance in adding some animated flair to your website as users scroll

I don't have much experience in animation but I would like to create some animation effects on scroll down. Can anyone provide suggestions? I attempted using SVG paths, but it didn't work out as expected. What I am aiming for is that when a visi ...

Tips for implementing a scrolling renderer in ThreeJS?

I'm currently utilizing the OrtographicCamera. Within the HTML, a canvas is declared and then passed to the renderer like this: var canvas = document.getElementById("canvas") var width = canvas.clientWidth; var height = canvas.clientHeight; render ...