differences in rendering of flexbox align-items: center between Chrome and Firefox

As I was attempting to center a div inside another div using flexbox, an inconsistency in rendering between Firefox and Chrome caught my attention.

In the scenario, there are 3 divs, each with properties display:flex, justify-content:center, and align-items:center.

Chrome View: The innermost div appears slightly off-center to the right rather than perfectly aligned at the center.

Firefox View: In Firefox, the div is accurately positioned at the center of its parent div.

If you wish to view the code for this issue, you can access it here: https://codesandbox.io/s/nice-pine-q63b5?file=/src/App.js

Firefox version being used: 69.0 (64 bit)

Version of Chrome utilized: 84.0.4147.105 (64 bit)

Operating system in use: MacOS Catalina 10.15.6

Answer №1

UPDATE: It appears that Chrome has resolved the issue and it is no longer present.

The root cause of this issue lies in pixel precision discrepancies between Chrome and Firefox. Chrome struggles to accurately render positions with pixel decimals, resulting in rounding errors. For instance, if your child element has a width of 7px within a parent element of 14px, the side margins of the child will be 3.5px but are rounded up to 4px by Chrome. To rectify this in Chrome, consider adjusting the child's width to 6px or 8px, or increasing the parent's width to 15px.

FURTHER INSIGHT: Some cases have shown that forcing Chrome to calculate decimal pixels can be achieved by specifying decimal pixels in the element's width attribute. Keep in mind that there may still be some rounding discrepancies to watch out for, but percentages and relative units like rem, em, vw, and vh tend to yield more consistent results. Here are some examples:

  • Yellow exhibits issues in Chrome.
  • Orange displays incorrectly in Firefox.
  • Teal encounters problems in both browsers.
  • White seems to function accurately?

div {
  width: 13px;
  height: 13px;
  background: black;
  margin: 5px;
  display: flex;
  float: left;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
div:after{
  content: '';
  background: white;
}
div:nth-child(1):after {
  width: 7px;
  height: 7px;
}
div:nth-child(2):after {
  width: 6px;
  height: 6px;
  background: teal;
}
div:nth-child(3):after {
  width: 6.1px;
  height: 6.1px;
  background: orange;
}
div:nth-child(4):after {
  width: 42%;
  height: 42%;
}
div:nth-child(5):after {
  width: 50.6%;
  height: 50.6%;
  background: orange;
}
div:nth-child(6):after {
  width: .43em;
  height: .43em;
  background: orange;
}
div:nth-child(7) {
  width: 12.5px;
  height: 12.5px;
}
div:nth-child(7):after {
  width: 6px;
  height: 6px;
  background: yellow;
}
div:nth-child(8) {
  width: 1.34%;
  height: 12.5px;
}
div:nth-child(8):after {
  width: 5.5px;
  height: 5.5px;
  background: yellow;
}
<div title="pixels if subtraction equals integer works"></div>
<div title="pixels if subtraction equals decimal is bugged"></div>
<div title="if you use decimal pixels in the child bugged in Firefox"></div>
<div title="percentage seems to work"></div>
<div title="decimal percentage might fail in Firefox"></div>
<div title="em also works"></div>
<div title="decimal pixels in the parent is bugged"></div>
<div title="parent's % + child decimal pixels also bugged"></div>

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

Items within a shared container are currently displaying stacked on top of each other

I am facing an issue with my bike images and manufacturer names appearing on top of each other instead of next to each other. Despite using "col-md-12", the grid columns are not aligning horizontally as expected. How can I adjust the code so that each imag ...

Challenges with z-index in Chrome/Safari and Firefox when nesting elements

In the current setup, it seems that .second needs to be positioned above .third, which is only happening in Firefox. Unfortunately, I am facing difficulty as I cannot separate .second from .fifth. Just to provide some context: .third is intended to act as ...

The header menu is not appearing on the gray bar in Internet Explorer

Having some issues with IE8, specifically on the website . The website header is not displaying correctly in IE8, but it works fine in Chrome and Firefox. Another issue is that the white area of the site is not 960px width as intended... ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

Extracting the href attribute from a child <a> tag

I am struggling to retrieve the value of the href within the code structure below: <div class=""> <div class=""> <div class=""> <a href=""><img src=""></a> </div> </div> </div> The m ...

Ways to ensure a <div> element adjusts its height based on inner content dimensions

When using buttons in a chatbot that have text which changes based on selected options, I've encountered an issue where the text length sometimes exceeds the button's space. I'm looking for a way to make the containing div automatically adj ...

The 3D Circle Flip feature on a certain webpage designed by Nordstrom is experiencing issues when viewed on Firefox

Click here to see a 3D Circle Flip effect. It works correctly in Chrome, but in Firefox the text does not change when flipping. ...

Issue with submitting forms in modal using Bootstrap

In the model box below, I am using it for login. When I click on either button, the page just reloads itself. Upon checking in Firebug, I found something like this: localhost\index.php?submit=Login <div class="modal fade" id="loginModal" tabindex= ...

Transform the functionality of DIV and UL to mimic that of a SELECT element

I am attempting to update the text of the <span class="hideSelect"> element to match the text of the selected <li>, so that it functions like a <select> element. CSS: .myDrop { border: 1px solid #ddd; border-radius: 3px; padding: ...

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The server is unable to process the request sent by the browser or proxy. This error occurred in a Flask web application

Can someone guide me on troubleshooting this issue in Flask? I am still learning. Server running at (Press CTRL+C to exit) 127.0.0.1 - - [26/Jul/2020 11:19:45] "GET /predict HTTP/1.1" 500 - Traceback (most recent call last): raise exceptions. ...

The Jquery code for Fullpage.js is causing unexpected behavior on the HTML page

While following a tutorial at the given link, I encountered an issue. Around the 9:08 mark, the instructor adds some JavaScript and demonstrates that fullpage.js is working. However, when I refresh the page after implementing the new code, it doesn't ...

Resetting the countdown timer is triggered when moving to a new page

In my current project, I am developing a basic battle game in which two players choose their characters and engage in combat. The battles are structured into turns, with each new turn initiating on a fresh page and featuring a timer that counts down from ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

My Drop Down menu is not displaying the <a> in two lines, and I'm having trouble getting it to show correctly

Hello there! I've encountered an issue with my drop-down menu. The text within the <a> tags is too long to fit on a single line, and I'm struggling to make it display in two lines instead. I've been experimenting for the past couple o ...

Can someone guide me on the process of assigning multiple classes in my situation?

Can someone assist me with combining multiple classes in an ng-class statement? I am attempting to utilize multiple classes for my element using ng-class. Here is what I have: <div id='test' ng-class="{blue: test, scale: scale, 'non-sca ...

Obtain all text within a <div> element by utilizing the agility html package

When attempting to retrieve all <p> tags from within a <div> using the Agility HTML package, I encountered an issue where only the first <p> tag was obtained. <div id='bodayDiv'> <p> hi </p> <p> what is ...

Enhancing Material UI List Items with Custom Styling in React.js

My website's navigation bar is created using material-ui components, specifically the ListItem component. To style each item when selected, I added the following CSS code: <List> {routes.map(route => ( <Link to={route.path} key={ro ...

Generate a Selectpicker dynamically when a button is clicked

Wanting to insert a new row in an HTML table dynamically by clicking on an add button. The new row includes a select element with the selectpicker class. Interestingly, when I remove the selectpicker class in my script to append a new row, the select eleme ...

Ways to divide the value of an input text box using Angular

Given the value "<HR>*10/100", how can I split it into three parts: "HR", "*", and "10/100"? HTML <input type='text' ng-model='value' ng-change="GetAngValue(value)"> {{Result}} Angular $scope.GetAngValue = function (val ...

Boosting your website with a slick Bootstrap 4 responsive menu that easily accommodates additional

I have incorporated a main menu in my website using the Bootstrap 4 navbar. However, I also have an additional div (.social-navbar) containing some extra menu items that I want to RELOCATE and INSERT into the Bootstrap menu only on mobile devices. Essentia ...