Challenges with CSS dimensions

Hey everyone, I have a question that I've been struggling to find an answer to. Imagine you have something like this:

<div className='container'>
    <div className='wrapper' style={{
        width: 40,
        height: 40,
        backgroundColor: 'red'
    }}>
        <div
            className='item'
            style={{
                width: 18,
                height: 18,
                backgroundColor: 'black',
            }}
        />
    </div>
</div>

Essentially, there's a square with another square inside it in a top-level container.

If I add padding to the container, the size and position of the item change:

https://i.sstatic.net/aMmAQ.gif

In the GIF example(if it's would be positioned to center then the position would change too..., as the Chrome's inspector says it's remains at 18px) As shown, the item's size changes when padding on the container is adjusted.

P.S.: If the size of the item is an even percentage (or px) such as 10%, 20%, 30% ... 90%, then the size and position will remain consistent. The same applies if using PX values such as

10% ... 90%.. eg.: 4px, 8px, 12px...
)

Answer №1

This issue occurs as a result of sub-pixel rendering in combination with the way CSS pixels are rendered.

According to the linked article:

... A CSS ‘px’ unit (as it is 1/96 of an inch) can translate to a fractional number of device pixels. For instance, on a screen with 300dppi (device pixel per inch), the ratio of device pixels to CSS pixels is 300/96 = 3.125.

In essence, when your elements are placed at precise positions on your screen, the browser displays them with the specified dimensions but they may seem to spill over by one pixel or fall short by one pixel due to the unusual CSS-pixel-to-screen-pixel ratio of your monitor.

If you were to conduct the same test on a different screen, the elements would exhibit the same "issue" but likely in different locations on the screen.

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

Obtain all data through Prisma Findmany Many to Many connections

When looking at the Prisma FindMany Include example (https://www.prisma.io/docs/getting-started/quickstart), the data model is defined as follows: model User { id Int @id @default(autoincrement()) email String @unique name String? posts Po ...

When using ReactJS, hovering over a row in a table should trigger a change in the background color of the corresponding row in another table with the same index

I need to create a feature where hovering over a row in the first table will highlight a corresponding row in the second table. The highlighting should be based on the index of the hovered row. Here is an example: <div> <table> < ...

Inquiry on ReactJS conditional rendering concept

Attempting to utilize the <Conditional if={condition}> component in React, which will only render its content if the condition evaluates to true. Referencing the code found on this page, as mentioned in Broda Noel's response to this inquiry. Th ...

Adjustable dimensions for a collection of squares based on screen size

I am currently designing a grid composed of 1:1 squares and allowing the user to continually add more squares. My main goal is to ensure that the size of each square maintains its aspect ratio while being able to resize accordingly. The challenge lies in k ...

What is the best way to completely uninstall Froala editor from your system?

As a beginner with the froala editor, I am looking for a solution to remove all previous instances created for the editor. Is there a way to completely destroy all instances of the froala editor? Please assist me with my requirement. Thank you in advance ...

Issues with responsiveness in Flexbox layout

My goal is to create a responsive design where both elements scale down when the screen size changes. However, I'm having trouble with alignment and can't figure out what the issue might be. #noteUIContainer { display: flex; position: abso ...

Guidance on initiating an Ajax script automatically when an HTML page is loaded

Is there a way to execute myfile.php first in myfile.html upon loading using an ajax script without the use of include and require functions? If so, how can this be done? ...

What steps should I take to ensure that my Ngrok domain is functioning properly?

Screenshot of Endpoints I've been facing challenges while attempting to get my NGROK domain operational. Despite completing all the necessary setup steps and successfully running multiple tunnels simultaneously with NSSM, as well as having a business ...

The card header in Bootstrap card grid does not have the ability to extend all the way to the edge of

Utilizing bootstrap 4 within Laravel, I create blade.php template files. Here is a snippet of my code. The card grid layout appears satisfactory, however, there seems to be some empty space between the card titles and the edges of the cards. <div id=" ...

Why is CSS styled by jQuery not working on hidden elements?

My challenge involves applying CSS to hidden elements and revealing them when a user interacts with a radio button. The issue arises when using CSS through jQuery to dynamically set the width of these elements. Because the elements are initially set to dis ...

Creating a WordPress widget that links to a local file

Hey there, I've been attempting to utilize the text widget in order to link to a local file, but unfortunately, it's not working as expected, and I'm unsure of the reason why: Here is what I am using: <a href="file:///D:/" target="_blan ...

Accordion Caption Image Gallery powered by Jquery

I have been searching for a jQuery image gallery plugin with an accordion caption on the right side, similar to this (please check it out). Despite my efforts, I haven't been able to find one that meets my needs. I came across some relevant plugins li ...

Scaling CSS Transform to Match Window Size

I have tried many solutions, but none seem to work for me. Here is the code I am using: $(function() { $(".zoom-close").on("click", function() { $(".zoom-close").toggleClass("transform", 1000); $(".img").toggleClass("active", 1000); }); }); ...

Is there a way to calculate the height of a component that is only rendered conditionally?

I have a scenario where I need to dynamically adjust the height of a component that is conditionally rendered without explicitly setting it. The height should automatically match the size of its content. To achieve this, I am using a reference to determin ...

Reverse all elements on the page except for ::selection

Is there a way to invert the page while keeping text selection unchanged? I attempted to use ::selection to exclude it, but was unsuccessful. Below is the CSS code I used for inverting the page: html { background-color: #131313 !important; filter: cont ...

Concealing and wrapping text using CSS in a table structure

My table is displayed below: <table style="width: 100%; white-space: nowrap; overflow: hidden;"> <tbody><tr> <th> Department </th> <th> Function </th> ...

After executing 'npx create-react-app todo --typescript', it was noticed that the .tsx files were missing from the project

I recently started learning TypeScript and came across some React articles using TypeScript. I tried running the following command to install a boilerplate with TSX files, but after running it, I didn't see any TSX files or even a tsconfig json file. ...

How I am able to access this.state in React without the need for binding or arrow functions

Understanding the concept of arrow functions inheriting the parent context is crucial in React development. Consider this React component example: import React, { Component } from 'react'; import { View, Text } from 'react-native'; i ...

Exploring the Spacing Between ng-repeat Items in Angular

Encountering an issue with multiple ng-repeat elements displaying div elements as inline-block. A strange gap appears between the elements when transitioning from one ng-repeat to the next, which is visible in the provided image: A demonstration of this p ...

What could be causing the bootstrap 4 col-md-3 block to shrink when using position fixed?

When working with Bootstrap 4, I encountered an issue where changing the block position from relative to fixed using a script caused the block to decrease in size. The script I used includes a .sticky class: .sticky { position: fixed !important; top: ...