Two sections that are supposed to have the same height, but one seems to be slightly taller than the other

Upon closer inspection: At first glance, they may appear identical at certain zoom levels. However, a more detailed examination through zooming in and out reveals a noticeable discrepancy. Firefox's default zoom may make them seem incorrect, while Chrome's default view appears fine but exposes the difference upon zooming.

For demonstration purposes, here is a live example on jsfiddle: http://jsfiddle.net/eHRkb/

The scenario at hand involves my base "unit" being a 50px x 50px square.

I have three main divs: 1. A div sized 48x98 with a border of 1, totaling 50x100 (1x2). 2. A div sized 48x48 with a border of 1, also totaling 50x50 (1x1). 3. A div simply sized at 50x100 without any border, serving as a 1x2 container meant to accommodate two 1x1 divs inside it.

The objective is to stack two 1x1 divs vertically and place a 1x2 div immediately adjacent to them. This layout is achieved by placing the 1x1 divs within the container, both set to float:left, resulting in this formation:

|[X]||   |
|[X]||   |

An issue arises where the leftmost div, the container, seems to be one pixel shorter than its counterpart on the right side. Both are intended to be 100px in height including borders, so the unequal heights pose a puzzling concern. Here is a breakdown of the CSS and HTML utilized:

CSS

.tile1x1
{
    width:48px;
    height:48px;
    float:left;
}

.tile1x2
{
    width:48px;
    height:98px;
    float:left;
}

.tile1x2Container
{
    width:50px;
    height:100px;
    float:left;
    border:none;
}

.border1
{
    border:1px solid black;
}

HTML

<div class="tile1x2Container">
<div class="tile1x1 border1"></div>
<div class="tile1x1 border1"></div>
</div>
<div class="tile1x2 border1"></div>

Thank you for taking the time to review!

Answer №1

The discrepancy in pixel count is a result of rounding errors during zoom adjustments.

For example, at a 1:1 zoom level, adding 50 + 50 equals 100 pixels.

However, with a 5:3 zoom ratio, the calculation becomes 83.33 + 83.33 = 166.66 pixels.

When rounded to whole pixels, this result changes to 83 + 83 = 167 pixels, resulting in a discrepancy of 1 pixel.

Answer №2

If we want to ensure that it fits properly, we can utilize the box-sizing property. To achieve this, a few modifications need to be made.

The height of .tile1x1Thumb should be adjusted to 49px instead of 48px.

Next, insert the following code into .border1:

box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-webkit-box-sizing: border-box;

Check out an example here!

For more information:
box-sizing guide

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 there a way to conceal a div class on the Payment page in Shopify?

I have encountered an issue with the code on the Shopify checkout (Payment) Page. Unfortunately, I am unable to directly edit this page within Shopify, but I have discovered that it is possible to add custom CSS or HTML code within the checkout settings of ...

What is the best way to incorporate two banners on the Magento homepage alongside my flexslider?

I recently made the switch from using a camera.js slider on my website to a Flexslider. The reason for this change was that I couldn't make the camera.js slider clickable, and I wanted to eliminate the 'Click here' button. However, now that ...

Attempting to generate a cost estimator, however, no results are showing up upon clicking the calculate button

Having based my code on a template and being fairly new to Javascript, I expected everything to work smoothly. However, upon testing it, I encountered an issue where nothing was displayed in the results boxes. My goal is to develop a pricing calculator th ...

What is the best way to align all menu items horizontally with the logo?

I'm facing a small issue that I can't seem to resolve on my own. I recently added a links menu to the header of my website, and it looks like the menu has fixed dimensions, causing the Login URL to get pushed down due to space constraints. I&apos ...

Empty space to the left of the vertical navigation bar

I'm struggling to get rid of the white space on the left side of my vertical navigation bar. I've attempted setting padding-left to 0 on my main navigation bar. This is my first time creating a navigation bar, so if you notice any semantic issu ...

Attempting to select an input field

I'm having trouble clicking on the Select Files button that is nested within a SPAN and Input Tag. Despite using Xpath, Id, and Name, I am unable to successfully click on the button. <span> Select <u>a</u> Files... </span> & ...

Does the 'span' element negatively impact the vertical alignment of text?

While working on my code, I noticed an issue with vertical alignment when using span tags to change the background of the text based on its content. The problem occurs specifically when viewing the code in the Android Chrome browser. You can view the initi ...

Can a div's style be modified without an id or class attribute using JavaScript or jQuery?

Is it possible to change the style of a div that doesn't have an id or class assigned to it? I need some assistance with this. Here is the div that needs styling: <div style="display:inline-block"> I would like the end result to look somethin ...

Adjustable centralized menu with buttons that resize accordingly

I am currently working on making a webpage responsive. I have successfully created a logo div that resizes accordingly. Now, I am facing a challenge with centering the buttons in the menu list. Despite my efforts, the buttons are not aligning in the center ...

The custom pagination feature in Addsearch UI always default to displaying the first page of search

I am currently using addsearch-ui version 0.7.11 for my project. I have been attempting to integrate a customized pagination function based on this example template. However, I have encountered an issue where the arrow buttons always navigate to the first ...

What are the steps to adjust the width of a website from the standard size to a widescreen

Is it possible to create a "floating" screen adjustment on websites? I know you can set the standard size of pixels, but how do sites adjust for different screen sizes like wider laptop screens? Does it automatically detect the reader's screen size an ...

jQuery Issue - Clash between multiple menus sharing the same class

Hey there! I'm currently diving into the world of jQuery and encountering an issue with my menu. Whenever I click on a menu item with either the "tm-nav-vertical" or "tm-nav-horizontal" class, it removes the .active class from the initial menu item. I ...

Ensuring that two columns in Bootstrap 3 have equal heights while including padding

I would like to achieve this design without using JavaScript. Here is a screenshot for reference: This is what I have created so far using Bootstrap 3. The CSS is inline due to the use of less, which makes it easier :) HTML <section class="container ...

Troubleshooting: Difficulty with jQuery script to change images

I am trying to modify the src attribute of an img tag using the code below, but it doesn't seem to be working. Can anyone help me figure out what's wrong? <img id='logo_image'/> <span onclick='$(logo_image).attr("src", "i ...

Guide on adjusting the CSS styling of elements in real-time from the backend using a user customization panel to modify the appearance of various web pages

Imagine a scenario where we have a website consisting of multiple pages, including a user account page. The user has the ability to modify the color, font size, and style of certain elements on other pages for their own viewing preferences. How can this fu ...

Is there a way to adjust the transparency of specific text when clicking on a different div?

How can I display a line of text when the "Contact Us" button is clicked on a practice website, similar to this: I have set the current opacity of the submit message to 0 so it is hidden, but is there a way to change its opacity to 1 when the submit link ...

Discover the secret to extracting a unique style from inspect mode and incorporating it into your CSS design

I'm currently working on my angular project and I've imported the ngx-editor. My goal is to reduce the height of the editor, but I'm facing some challenges. After inspecting the element, I was able to adjust the height successfully within th ...

Having trouble importing from the public folder in CSS with Create React App?

While working on a project initialized with Create React App, in the public folder there is an assets directory containing a file named logo512.jpg. When I use this file in a component like so: <div> <img src='/assets/logo512.jpg'/& ...

Incorporating TinyMCE into numerous dynamically generated text areas

I am facing an issue with dynamically created textareas. The content in these textareas is generated dynamically. This is how I retrieve the data and create the textareas dynamically: $(document).ready(function(){ $('#btn').click(function(){ ...

Encountering an unexplained JSON error while working with JavaScript

I'm trying to retrieve data from a JSON API server using JavaScript AJAX and display it in an HTML table. However, I keep encountering an undefined error with the data displaying like this: Name id undefined undefined This is my code snippe ...