CSS divs grid - eliminating unnecessary gaps between rows

Creating a basic grid layout with div elements is my current challenge. You can view the live demo here.

Here's the HTML structure:

<div class="board"></div>

For JavaScript, I'm using the following snippet:

$(function() {
    var boardHTML = '';

    for (var r = 0; r < 2; r++) {
        var row = '<div class="row">';

        for (var c = 0; c < 5; c++) {
            row += '<div class="cell"></div>';
        }

        row += '</div>';
        boardHTML += row;
    }

    $('.board').append(boardHTML);
});

The CSS styles are as follows:

.cell {
    width: 30px;
    height: 30px;
    outline: 1px solid black;
    display: inline-block;
}
.row {
    background-color: red;
}

Despite this setup, there seems to be some spacing between the rows.

QUESTION: What is causing this undesirable space?

A suggested solution involves setting the row height to 30 pixels:

.row {
    height: 30px;
}

To eliminate additional space on the right side, adjusting the board's width may help:

.board {
    width: 150px; /*   5 * 30px   */
}

However, I'm curious if there's a more flexible approach that doesn't rely on fixed pixel values. Any thoughts or alternative solutions?

Answer №1

To achieve vertical alignment to the top in your CSS for the .cell class, use the following code:

.cell {
    display: inline-block;
    height: 30px;
    outline: 1px solid black;
    vertical-align: top;
    width: 30px;
}

For an example, you can view this demo

Answer №2

Everything is functioning as intended with these configurations, so it's uncertain what outcome you are seeking.

Instead of adjusting the row height, consider experimenting with margins. Margins determine the spacing between block elements.

As for the final suggestion, attempt setting the div width to 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

Building a set of multiple choice questions with the flexibility to include two or more options using bootstrap

If you visit https://getbootstrap.com/docs/4.0/components/forms/, you can find code that allows you to create a multiple-choice question with only one choice allowed using the following snippet: <div class="form-check"> <input clas ...

XPath: Begin from a specific node and retrieve all the furthest descendants starting with the second child

<?xml version="1.0"?> <shipTo country="US"> <name><strong>Alice Smith</strong></name> <street>123 Maple Street</street> <city><hi>Mill Valley</hi></city> ...

Turn the flipcard hover effect into an onclick action

After successfully creating interactive flip cards using CSS hover effects, I am now faced with the challenge of adapting them for mobile devices. I'm struggling to figure out how to translate my hover effects into onclick events for a mobile-friendl ...

Tips for modifying the table structure while utilizing datalist

Is there a way to adjust the table layout when using datalist? The current result, as shown in Example 1, does not properly display all the data in a continuous manner due to the table layout. How can I achieve a layout similar to Example 2? I must use th ...

If the Ajax request is successful, scroll to the bottom of a Div using jQuery

An interesting scenario arises with this Div that contains user messages, where the newest message always appears at the bottom of the Div and triggers a scroll bar if the message count exceeds the height of the div: <div class="list-group-message" s ...

Is there a way to validate td content without considering spaces or new lines?

I am working with a table that has dynamic values in its td cells. Some td cells will be empty while others will have values. I want the empty td cells to have a red background, and the filled td cells to have a green background. I attempted to achieve thi ...

The Font Awesome icons are not appearing on the cypress runner container

Currently, I am diving into the world of Cypress by working on my own React todo app. During integration testing, I encountered an issue where my UI icons (sourced from Font Awesome via CDN) were not displaying. As a result, the delete button for my todos ...

Trouble with the jQuery sliding animation speed

One of the challenges I'm facing involves two divs: left and right. I want to slide the left div, making the right div's width 100% when the left div is hidden. This setup works well, but when I try to adjust the speed (slow or fast), it doesn&ap ...

Fixing issues with internal web page connections (#x) present in the HTML code (href="#x" id="x")

Can anyone come up with a brilliant solution as to why my internal links are not functioning properly on a webpage? I've coded the HTML with the usual <a href="#link"><button>Some Text</button></a> .... bunch of content .... & ...

Changing the text during a reset process

I've been grappling with this issue, but it seems to slip through my fingers every time. I can't quite put my finger on what's missing. My project involves clicking an image to trigger a translate effect and display a text description. The ...

Simultaneously opening the second submenu and closing the previous submenu with a height transition results in the second submenu being positioned off-screen

Sample: https://codesandbox.io/p/sandbox/navbar-k2szsq (or refer to the code snippet below) In my navbar, I have implemented multiple submenus with a height transition when the user opens or closes a submenu. However, only one submenu can be open at a tim ...

How can I ensure that changes made to a div in ASP.NET remain persistent during postback?

My current setup involves using jquery to toggle a div on and off successfully. However, I am encountering an issue where any changes made are not saved when a postback occurs on the page. Can anyone provide guidance on how to save these changes? Any assis ...

What is the best method to extract the text inside a <p> tag within an iframe using Selenium in Python?

Examining a section of code within the webpage I am currently working on, I came across the following snippet - <div class="A"> <iframe id="frameA"> #document <html style> <head></head> ...

Is there a way to implement a scrollbar within a DIV element?

I'm working on a webpage that includes several customized buttons within multiple DIVs. Take a look at this example on the fiddle. Since I have a lot of buttons, I need to find a way to add a scrollbar so users can easily navigate to the desired butt ...

Tips for aligning a div containing an image in the center of another div

Trying to perfectly center an image inside a div both vertically and horizontally using the display inline-block property. HTML <div class="center-wrapper"> <div class="image-holder"> <img src="picture.jpeg" alt="Centered Image"> ...

Is it feasible to incorporate the CSS and Bootstrap (CDNs) specific to an HTML component without affecting the styling of other pages?

One way I am able to recycle the HTML component is using this code: $.get("nav.html", function(data) { $("#fulloptions").replaceWith(data) }) However, there’s a conflict because nav.html uses a different version of Bootstrap than my index file does ...

Customize the text displayed on the select box button in a React application

My task involves working with an array of JSON objects structured like this - [ { "country_code": "AF", "country_name": "Afghanistan", "country_flag": " ...

Creating a dynamic video background using Html5 and CSS with a scrolling attachment

video { width: 100%; z-index: 1; position: fixed; } I tested this code and it works well, however, the video remains fixed across the entire body of the page... ...

Is it possible to modify the CSS of a single class when hovering over a child class of a different and unrelated class?

I am struggling with CSS combinators, especially when dealing with nested div, ul, and li elements. My issue involves changing the CSS of a div with the class "H" when hovering over li elements with the class "G". Since all of this is contained within a s ...

What could be the reason for an element with a width of 100% not recognizing the width of its parent?

One issue I'm currently experiencing is my inability to match the width of the parent within a flexbox item. Below is the code snippet, where the span element does not have the same width as its parent. .container { display: flex; } .item1 { ...