Spacing issues with inline-block elements when dealing with a large quantity of items

Currently, I am tackling the JS/jQuery Project for The Odin Project and I am confident that my solution is working exceptionally well.

However, the issue I am facing is that when dealing with larger amounts of elements (around 40-45 in JSFiddle and 50-52 in my Chrome Browser per line), there seems to be a vertical gap between the elements on two consecutive rows. Initially, I tried setting vertical-align: top to eliminate these gaps, which did work up until the mentioned number of elements per row.

You can view the JSFiddle example here.

If you increase the number of elements per row in the JS file (to at least 50), you will observe the issue I am describing.

This behavior is not what I desire. My goal is to have a seamless grid without any gaps between the cells. Can anyone shed light on why vertical-align: top breaks?

Edit: Upon reflection, I believe the problem might be linked to the percentage-based width/height, as it also malfunctions with numbers below 40 due to complex fractions being the result of division.

Answer №1

When it comes to inline boxes, they inherit inheritable properties from their block parent box. This means that your grids may be inheriting the line-height property of the .container element. If the .container overflows, the vertical-align: top; property may stop working. To avoid this issue, it is recommended to apply a line-height: 0; style to the parent element (.container).

Source: https://www.w3.org/TR/CSS2/visuren.html#inline-boxes

$(document).ready(function() {
    createGrid(48);
    $(".cell").mouseenter(function() {
        $(this).css("background-color", "green");
    });
    $(".cell").mouseleave(function() {
        $(this).css("background-color", "white");
    });
});

function createGrid(n) {
    var container = $(".container");
    container.empty();

    var sizeP = 100 / n;

    var cell = $('<div/>', {
        class: 'cell',
        style: 'width: ' + sizeP + '%; height: ' + sizeP + '%;'
    });

    for(i = 0; i < n*n; i++) {
        container.append(cell.clone());
    }
}
.container {
    border: 5px solid black;
    border-radius: 5px;
    padding: 10px;
    margin: 10px;
    width: 800px;
    height: 800px;
    line-height:0;
}

.cell {
    display: inline-block;
    box-sizing: border-box;
    border: 1px solid black;
    vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<body>
  <div class="container"></div>
</body>

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

Is it possible to turn off the fallback color for bourbon radial gradients?

Is there a way to prevent the fallback from being applied when using the radial gradient mixin in Bourbon? I've consulted the documentation at , which states that the fallback is optional. However, no matter what I attempt, it seems that the primary ...

Implementing the Applet Param Tag using JavaScript or jQuery

<applet id="applet" code="myclass.class"> <param name="Access_ID" value="newaccessid"> <param name="App_ID" value="newappid"> <param name="Current_Url" value="newticketurl"> <param name="Bug_ID" value="newbugid"&g ...

Automotive Dealership API by Edmunds

I am having some trouble with the Edmunds API data. I can't seem to get the JSON to display when using the example code provided. It just shows up as a blank page. I am quite new to working with API's and would really appreciate some guidance on ...

Combining strings using the PHP preg_replace function

I'm looking for assistance with using Ajax to send JS variables to my PHP script in order to modify the background color. Can you provide guidance on how to achieve this? I am struggling with how to concatenate strings and utilize the $mavariable vari ...

Is there a method to determine if localForage/indexedDb is currently handling data operations?

Currently, I am working on a webapp that utilizes async localForage (specifically angular localForage). My goal is to alert users if they attempt to close the browser window while there are ongoing localForage operations. I have observed that some browser ...

Ways to set the input=text field as read-only in JSP when receiving information from the backend

For a project I am working on, I need to implement a feature where users can view and edit their personal details on a JSP page. If a user is logged in, their information should be fetched from the session and displayed automatically. However, even if they ...

Guide to organizing the legend section into two columns within Apache Echarts

I'm looking to customize the legend area layout in Apache Echarts for a two-column display. Can anyone provide guidance on achieving this using legend options? I have been unable to locate any examples demonstrating this particular layout. For refere ...

A vertical divider within an ion-item collection

Currently, I am using Ionic and HTML to implement a vertical line within an ion-item in an ion-list. The desired outcome is to have something similar to this (the colored line after 'all-day'): I attempted the following approach: <ion-list&g ...

The i18n feature in node.js seems to be malfunctioning as the setLocale function is not working properly. Regardless

Something seems off with my usage of the i18n library, but I believe I'm following the correct steps. I've been attempting to switch the locale. Here's my current code. Despite calling setLocale on the global i81n variable, it continues to ...

Is there a way to avoid reaching the limit of 150 JSON calls per hour allowed by the Twitter API and prevent Twitter from dropping unnecessary cookies?

I currently have a Twitter feed integrated on my website using the jquery tweet plugin from tweet.seaofclouds.com along with other json plugins that fetch data from a third-party website. The issue I'm facing is that the Twitter API restricts the num ...

Obtaining an identification using JQuery for content that is constantly changing

I am currently developing dynamic content tabs using PHP, with one of the objects being a datatable within the tab. In order to define the ID via PHP, I use the following code: PHP: echo '<table class="table table-striped table-bordered table-hov ...

How to troubleshoot Javascript code that fails to identify if a color is white

What could be causing my code to malfunction? I am attempting to determine if the paragraph text color is white (as determined by CSS settings). If it is indeed white, I want to change it to black for better visibility. function adjustColor() { if (d ...

JavaScript error: "null or not an object" bug

I have implemented the JavaScript code below to display horizontal scrolling text on the banner of my website. Surprisingly, it works perfectly fine on one server but throws an error on another server with the message: Error: 'this.mqo' is nul ...

`Issue with Firefox's rendering of HTML tables`

I have noticed that the table appears differently in Firefox compared to IE8/Chrome. You can view the website here: I would like the table to look more like it does in IE8/Chrome, where the lines are a light gray color instead of completely black. Any s ...

What is the best way for me to make sure the element overflows properly?

How can I make the contents of the <div class="tags> element cause overflow so that one can scroll its contents instead of the element simply extending in height? I've experimented with different combinations of overflow-y: scroll and min- ...

Display HTML content retrieved from a string saved in a React database

I am currently in the process of creating a React page and facing a challenge with incorporating HTML content from a RTDB that has been styled using "use styles". Here is an example of the styling: import { makeStyles } from '@material-ui/core/style ...

How to access the public folder in React from the server directory

I am having an issue with uploading an image to the react public folder using multer in NodeJs. Everything was working fine during development, but once I deployed it, the upload function stopped working. It seems like the multer is unable to reference or ...

JavaScript function is not identifiable

I am currently developing a tic-tac-toe game with 9 buttons that will display either an X or O image depending on the boolean value of the variable isX. The main container for these buttons is a div element with the id 'stage' and each button ha ...

There appears to be a syntax error in the Values section of the .env file when using nodejs

I have been working on implementing nodemailer for a contact form. After researching several resources, I came across the following code snippet in server.js: // require('dotenv').config(); require('dotenv').config({ path: require(&apos ...

I built a custom Angular application integrated with Firebase, and I'm looking for a way to allow every user to have their individual table for managing tasks

I am looking to enhance my code so that each user who is logged in has their own tasks table, which they can update and delete. Additionally, I need guidance on how to hide menu items tasks, add-tasks, logout for users who are not logged in, and display th ...