An oversized image creates additional room

I currently have a board that consists of 3 blocks, with each block containing a grid of 25 squares. These squares are 40x40px in size with a margin around them, resulting in an overall size of 220px x 220px. Each square also has some space above and below it.

Now, if I decide to change the first block to just one large square measuring 220px x 220px which contains a single image of the same dimensions, there appears to be extra space added below this square and above the other two squares.

The first square now has 7px below it instead of the original 5px, while the remaining two squares have 7px above them instead of 5px. I've tried various methods including setting font-size to 0 to resolve this spacing issue with no success.

Even when I place all the HTML code on a single line without any spaces, the additional space persists.

Below is my code. When you run it in full screen mode and pay close attention to the pink box, you will notice that it does not align perfectly with the rest of the boxes. There seems to be an extra 2px gap below the pink box and 2px above each of the other two boxes. Where is this unwanted space coming from and how can it be rectified?

It has been observed in other related Stack Overflow threads that inline images (and by extension, inline blocks) tend to have space underneath them by default. Changing these small images to display as blocks solves the problem, but the reason behind this fix remains unclear. Measurements reveal no white space beneath the images initially. The excess spacing seems to be generated outside of the outermost blocks.

.board {
        background-color: #05a;
font-size: 0;
}

.block {
        padding: 5px;
        border: inset #000  1px;
        display: inline-block;
        width: 220px;
        height: 220px;
        margin: 5px;
        font-size: 0;
        background-color: green;
}

.square {
        margin: 2px;
        width: 40px;
        height: 40px;
        background-color: yellow;
        display: inline-block;
        font-size: 0;
}

.image40 {
width: 40px;
height: 40px;
display: inline-block;
background-color: white;
font-size: 0;
}

.large_square {
        margin: 0px;
        width: 220px;
        height: 220px; 
        background-color: yellow;
        display: inline-block;
        font-size: 0;
}

.image220 {
width: 220px;
height: 220px;
display: inline-block;
background-color: pink;
font-size: 0;
<div>Board #1:</div>

<div class="board">
<div class='block'><div class='square'><div class='image40'></div></div><div class='square'><div class='image40'></div></div>< ...

<div class="block"><div class="large_square"><div class="image220"></div></div></div>

<div class="block"><div class="square"><div class="image40"></div></div>< ...

Answer №1

By incorporating either overflow: hidden or vertical-align: top into the block element, you will make progress in resolving the issue. However, there is still some investigation needed to address the excess space beneath the small squares.

Answer №2

The excess space is due to the whitespace rendered by the browser between the inline-block elements. Eliminating the extra space can be achieved through various methods like removing or commenting out whitespace, using alternative CSS properties such as float:left, or ensuring the parent element has overflow:auto.

.board {
        background-color: #05a;
        font-size: 0;
        overflow:auto;
}

.block {
        padding: 5px;
        border: inset #000  1px;
        display: inline-block;
        float:left;
        width: 220px;
        height: 220px;
        margin: 5px;
        font-size: 0;
        background-color: green;
        overflow:auto;
}

.square {
        margin: 2px;
        width: 40px;
        height: 40px;
        float:left;
        background-color: yellow;
        display: inline-block;
        font-size: 0;
}

.image40 {
width: 40px;
height: 40px;
display: inline-block;
background-color: white;
font-size: 0;
}

.large_square {
        margin: 0px;
        width: 220px;
        height: 220px;
        background-color: yellow;
        display: inline-block;
        font-size: 0;
}

.image220 {
width: 220px;
height: 220px;
display: inline-block;
background-color: pink;
font-size: 0;
<div>Board #1:</div>

<div class="board">
<div class='block'><div class='square'><div class='image40'></div></div><div class='square'><div class='image40'></div></div><div class='square'><div class='image40'></div></div><div c... (truncated for brevity) .../div></div><div class='square'><div class='image40'></div></div><div class='square'><div class='image40'></div></div></div>

</div>

For a more detailed collection of solutions, refer to this response on another post.

Answer №3

To align the content at the top of the block, include vertical-align:top in the CSS for the block element.

.block {
    background-color: green;
    border: 1px inset #000;
    display: inline-block;
    font-size: 0;
    height: 220px;
    margin: 5px;
    padding: 5px;
    vertical-align: top;
    width: 220px;
}

Answer №4

To enhance the design, consider converting the img to a block-level element.

.image40 {
 width: 40px;
 height: 40px;
 display: block;
 background-color: white;
 font-size: 0;
}

Answer №5

Here is an alternative CSS code that could potentially resolve the issue at hand without any HTML problems:

.big_box {
    margin: 0;
    width: 220px;
    height: 218px;
    background-color: lightblue;
    display: inline-block;
    font-size: 0;
    clear: both;
}
.image220 {
    width: 220px;
    height: 218px;
    display: inline-block;
    background-color: lightcoral;
    font-size: 0;
    clear: both;
}

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

Combining rows and columns in Flexbox design

https://i.stack.imgur.com/tDLii.png I'm able to easily create this layout using the float property, but I'm having some difficulties achieving the same with flexbox. CSS: .a { background: red; float: left; width: 30%; ...

Update Input field by eliminating white spaces using jQuery version 3.2.1

I am currently developing an HTML/PHP form for user registration. Using the code below, I have managed to prevent any blank spaces from appearing in the input field: <!DOCTYPE html> <html> <head> <script> function stripspaces( ...

Entering text into the input field with the string "Foo"<[email protected]> is not functioning correctly

The text in my input is initially filled with this string "foo foo"<<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2920200f29202061263b">[email protected]</a>>. However, after an insert, the string now l ...

I can't seem to get the button to function properly, and it's really

I am having an issue with my "Let's Get Fit" button. When I hover over it or click it, the cursor doesn't change and it doesn't lead to index.html. I'm not sure why it's not working. .btn { display: inline-block; text-transf ...

"Showing Off Your Steam Inventory on Your Website: A Step-by-

My goal is to display a user's specific inventory on a webpage. I've tried using the following link: but it hasn't been helpful. ...

No matter how tall I make it, the scroll bar just won't stop showing

Check out this fiddle for more information: http://jsfiddle.net/LAueQ/ Even after increasing the height of the campaignDiv div, the scrollbar keeps showing up. If possible, I would like to avoid using overflow: hidden. ...

Exploring the different ways to compare data with the load() method

I am faced with a coding dilemma. I have two pages: data.php and index.html. The data.php page contains a single value (let's say 99), while the index.html page uses jQuery to dynamically load the data from data.php into a div called "data" every 2 se ...

Choosing bookmarkable views in Angular 5 without using routes

I'm currently working on a unique Angular 5 application that deviates from the standard use of routes. Instead, we have our own custom menu structure for selecting views. However, we still want to be able to provide bookmarkable URLs that open specifi ...

Tips for adjusting the size and positioning the ng bootstrap carousel in the center

My Angular project is using ng bootstrap, but I'm facing a styling issue. The content doesn't display in the center, rather it appears in the upper left corner: example I would like the content to be centered and wide under the blue headbar. W ...

How can I use CSS in Next.js to style a child element when its parent is being hovered over?

Here is the CSS code that specifically targets the child element "#overlay" when its parent, ".collection", is being hovered over: .collection { position: relative; overflow: hidden; } .collection:hover #overlay { position: absolute; opa ...

What is the method for identifying the name of a CSS Variable currently in use?

Consider this code snippet: /* css */ :root { --text-color: #666666; } input { color: var(--text-color); } In Javascript, how can I determine the name of the CSS custom property (variable) being utilized? // javascript console.log(document.querySel ...

Despite utilizing the .img-fluid class, the image remains incompatible with the parent div and does not fit properly

So, I'm currently working on a test code where my aim is to fit an image into the parent div using Bootstrap 5. However, I'm facing a challenge where the image leaves a noticeable gap width-wise, despite using the .img-fluid class. You can see th ...

Choosing a BeautifulSoup tag according to attribute value

Imagine we have an HTML page with an index and some content below. Each element in the index is linked to a related section in the document. Let's say our starting point is an anchor tag with an href value of "#001". We want to search through all the ...

The error message "email() is not a valid function when using the onclick attribute

Can anyone lend a hand? I feel like I must be overlooking something really obvious. I'm having trouble calling my function to execute my ajax call. Any assistance would be greatly appreciated. Thank you! Here is an excerpt of the HTML code: $(docu ...

Display excerpts of code within an Angular application within the HTML segment

I'm currently developing a tutorial page using Angular where I intend to display code snippets in various programming languages such as 'Java', 'TypeScript', 'C#', and 'Html'. Although there is a snippet called ...

AngularJS button click not redirecting properly with $location.path

When I click a button in my HTML file named `my.html`, I want to redirect the user to `about.html`. However, when I try using `$location.path("\about")` inside the controller, nothing happens and only my current page is displayed without loading `abou ...

Adjust the baseline of the text within the <li> element by moving it 2 pixels upwards

My webpage menu is set up with inline lis within a ul. Each li has a colored border and contains an a element. I want to change the color of the text inside the a element and move it 2 pixels up when hovering over it without affecting the li border. How ...

Creating a responsive layout with Bootstrap using fluid rows and columns of varying heights

Using dynamic creation, I am tasked with generating a set of boxes each with slightly varying heights and ensuring that 2, 3, or 4 of them (based on screen size) appear on the same row. Here is an example of my attempt using Bootstrap: <div class="cont ...

Transform HTML elements within an *ngFor iteration based on a specific variable in Angular 4

In my current project using Angular 4, I am faced with the task of dynamically modifying HTML tags within an *ngFor loop based on a variable. Here is the code snippet that represents my approach: <mat-card-content *ngFor="let question of questionGrou ...

Issue with nivo-lightbox not opening upon clicking image

I have diligently followed the instructions to set up this JavaScript plugin, but unfortunately it doesn't seem to be functioning properly. The plugin I'm using can be found here: All the links to the CSS, theme, and JavaScript files are display ...