Element 1 is positioned with a CSS top attribute of x% and a height of y%, while Element 2 has a CSS top value of (x+y)%. What is the reason behind the discrepancy in

I am encountering a problem with sizing.

Desired Outcome:

My goal is to use resizable and draggable functionality along with JavaScript code to position elements on the screen by setting their top, left, height, and width. I aim to resize an element until it touches another element without any gap between them.

The Issue with Sizing:

While resizing an element, it stops correctly when it collides with another element. However, occasionally a small gap appears between the elements (usually less than a pixel wide).

When calculating the position of the elements based on percentages, everything seems in order initially. For example, if the top of element 1 is set at 10% and its height at 10%, then the top of element 2 should be at 20%. But upon calculating the pixel values, discrepancies arise:

Element1.offset().top + Element1.height() != Element2.offset().top

Illustration of the Problem with Screenshots:

I am puzzled as to why the percentage values do not correspond to the pixel values. Below is an example with attached images to highlight this issue.

Element 1 (above element):

Top + Height = Bottom

13.64% + 13.68% = 27.32%

Element 1's top and height in percentages

Element 2 (below element):

Top = 27.32%

Element 2's top in percentages

Although the sum of top and height in percentages for element 1 matches the top of element 2, there is a discrepancy when comparing these values in pixels:

Element1.offset().top + Element1.height() = 175.5875015258789px

Element2.offset().top = 175.60000610351562px

Comparison of pixel values for Element 1 and Element 2

Why does Element1.offset().top + Element1.height() not equal Element2.offset().top despite having the same percentage values?

Click here for a JSFiddel demo of the software

Answer №1

Eliminate the border causing the issue. Cascading Style Sheets

* {border-style: none;}

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

Rotate the image using the handler, not by directly manipulating the image itself

I need help rotating the image using a handler instead of directly on the image itself. I do not want to rotate the image when clicking and rotating it directly. Please do not suggest using Jquery UI rotatable because resizing the image with Jquery UI resi ...

What is the process for adjusting the font in my VS Code editor?

Currently diving into an Angular2 development video course, I stumbled upon something intriguing: https://i.stack.imgur.com/CIBcx.jpg Yes, aesthetically pleasing HTML attributes won't make me a coding genius. But let's be honest, who doesn&apos ...

React (version 18.2.0) successfully updates the URL using the navigate function, however, the page itself fails to

While I navigate from page /orders/:id to another order (/orders/:anotherId) using the Link component, the URL changes but the view remains the same. When I try to change the URL in React (v18.2.0) using the navigate function, the page does not update or ...

The pop-up menu is obstructed by the dialog box

Within my dialog, there is a gridview that allows the user to input information. One of the textboxes within the grid has a button adjacent to it. Upon clicking this button, a menu appears with a selection of items for the user to choose from, and the sele ...

Responsive carousel with multiple items

I am in the process of developing a website that requires the implementation of a carousel feature. Since the website is based on AngularJS, I initially considered using Angular's Bootstrap Carousel. However, I encountered an issue where this carousel ...

An error occurred due to a state update being attempted on an unmounted component. The solution is to properly cancel all subscriptions and asynchronous tasks in a

Currently, I am attempting to utilize ListEmptyComponent within a FlatList. If there is no data present, I intend to display ListEmptyComponent={} However, in the Loadingsecond component, I am using useEffect to render when loading is true; if there is s ...

Steps to display an angled bracket to indicate a line break

I have a div that is editable, allowing me to display the entered text. To indicate each new line, I would like to show a ">" symbol at the beginning of the line. HTML <body> <div> <span> &gt; </span> ...

Global variables become undefined after utilizing jQuery's getScript() method for assignment

I have a few global variables named var1, var2, and so on... Instead of immediately initializing these variables, I wait until later to instantiate them using jQuery from a constructor located in a separate javascript file. However, after this instantiat ...

A guide on transferring radio button values to another program and saving them into a database with the help of PHP and jQuery

I have encountered an issue with storing radio button values in a database. Specifically, I want to assign the value of 1 for "Yes" and 0 for "No". To accomplish this, I am utilizing two scripts - a.php and b.php. The former obtains the radio button values ...

Using jQuery for dynamic string interpolation in input selection

Currently, I am attempting to choose an input within my dom and I am aware that it can be accomplished as follows: $("input[value='foo']") ...but, what if we were to use a variable instead? For example, if we have a variable x = 'foo' ...

What could be causing the misalignment of these two divs?

I'm struggling to horizontally align these two divs using percentages for width. Even with padding and margin set to 0, they refuse to line up properly (the second one ends up below the first). This is the code I have been using: <div style="widt ...

vue.js and the various elements that make up its architecture

My component content is not appearing!!! I have checked the router multiple times and it seems to be functioning correctly, but when I run the project, the components are empty. Even though I have added a lot of content to them, they appear to be blank. ...

Is there a way to print the full page including scroll tab content?

https://i.sstatic.net/FJt7P.png I am facing an issue where I want to print the entire page including the scrollable content within a tab. Currently, only the visible screen content is being printed. ...

Efficient techniques for developing lazy-loading ajax services

What are some efficient ways to create lazy ajax services in angular-js? For instance, I need a resource that returns a promise such as: angular.module('MyModule',[]) .factory('myService', function() { return { getData: fun ...

JavaScript code using jQuery's ajax method is sending a request to a PHP server, but

Attempting to utilize jQuery ajax for PHP call and JSON return. The test is quite simple, but only receiving an empty object in response. No PHP errors appearing in the LOG File. jqXHR is recognized as an object with 'alert', yet not displayin ...

I'm currently in the process of building a brand new React.js application by utilizing the command "npx create-react-app". However, upon completion, I encounter an error while attempting to run it with the command "npm start"

Below is the complete error message Error message from ./src/index.js at line 1, character 58 Failed to parse module: Unexpected token found at (1:58) The following loaders were used to process the file: * ./node_modules/@pmmmwh/react-refresh-webpack-pl ...

Emulating Data in Angular 2 Using Configuration Similar to Ember Mirage

Is there a way to mock data through configuration in Angular 2 similar to how it's done in Ember Mirage? I'm aware that I can create my own solution using Dependency Injection and MockBackend to intercept HTTP calls and provide dummy data. Howeve ...

Using Fullcalendar Moment to retrieve the current time plus an additional 2 hours

Trying to tackle this seemingly simple task using moment.js in conjunction with Fullcalendar. My goal is to retrieve the current time and then add two hours to it. Within the select method (or whatever terminology you prefer), I currently have: select: f ...

Unit testing in Typescript often involves the practice of mocking

One challenge with mocking in Typescript arises when dealing with complex objects, as is the case with any strongly-typed language. Sometimes additional elements need to be mocked just to ensure code compilation, such as using AutoFixture in C#. In contras ...

Updating React component props

After updating the state in a component and passing the new props into the child, I noticed that the child is not updating correctly and the defaultValue of the input is not changing. My initial thought was that using this.props could be the issue, so I sw ...