In Internet Explorer, auto margins in Flexbox do not function properly when using justify-content: center

My table has cells that can contain both icons and text, with the icons appearing to the left of the text. There are different alignment scenarios to consider:

  1. Only an icon is present: The icon should be centered
  2. Only text is present: The text should be left aligned
  3. Both icons and text are present: Both the icon and text should be left aligned

I had the idea of using a flexbox within the table-cell, setting justify-content: center;, and adding margin-right: auto; to the text div for achieving various alignments.

If there is text, the auto margin will shift everything to the left.

If not, the icons will be centered due to the justify-content style.

You can view my codepen example here.

.flexbox {
  display: flex;
  flex-direction: row;
  justify-content: center;
  border: 2px solid black;
  width: 300px;
  height: 17px;
}
.icon {
  height: 17px;
  width: 17px;
  background-color: red;
}
.text {
  margin-right: auto;
}
<div class="flexbox">
  <div class="icon"></div>
  <div class="text">asdf</div>
</div>

This method works well in Chrome, but I encountered issues with IE 11 where the right auto margin isn't applied correctly. I'm trying to understand this behavior and find a workaround.

Additional Information

It seems like IE 11 calculates auto margins first, aligns flex items without considering these margins, and then applies the margins as best as it can afterwards.

For instance, when justify-content: flex-end; is used with margin-left: auto; on the text div, the icon gets right aligned within the flexbox while the text extends beyond the flexbox boundaries by nearly the entire width (which should have been the auto margin).

Answer №1

According to the flexbox specification:

Before alignment using justify-content and align-self, any remaining space will be allocated to auto margins in that direction.

In simpler terms, auto margins take precedence over justify-content.

In the given scenario, Chrome seems to adhere to the specification (as does Firefox).

However, IE11 - when the parent has justify-content - fails to recognize margin-right: auto on the flex item. This behavior appears to be a bug.

Removing justify-content resolves the issue, allowing auto margins to function as intended.

One potential workaround is eliminating justify-content entirely and solely relying on auto margins:

  1. If only an icon is present: Center the icon
.icon { margin: 0 auto; }

.flexbox {
  display: flex;
  flex-direction: row;
  border: 2px solid black;
  width: 300px;
  height: 17px;
}
.icon {
  height: 17px;
  width: 17px;
  background-color: red;
}
.icon {
  margin: 0 auto;
}
<div class="flexbox">
  <div class="icon"></div>
</div>

  1. If only text is present: Align the text to the left
.text { margin-right: auto; }

.flexbox {
  display: flex;
  flex-direction: row;
  border: 2px solid black;
  width: 300px;
  height: 17px;
}
.icon {
  height: 17px;
  width: 17px;
  background-color: red;
}
.text {
  margin-right: auto;
}
<div class="flexbox">
  <div class="text">asdf</div>
</div>

  1. If both icons and text are present: Align both the icon and text to the left
.text { margin-right: auto; }

.flexbox {
  display: flex;
  flex-direction: row;
  border: 2px solid black;
  width: 300px;
  height: 17px;
}
.icon {
  height: 17px;
  width: 17px;
  background-color: red;
}
.text {
  margin-right: auto;
}
<div class="flexbox">
  <div class="icon"></div>
  <div class="text">asdf</div>
</div>

Answer №2

I've tried removing justify-content as a workaround, but unfortunately it doesn't seem to solve the issue for me. If you take a look at this specific example in IE11, you'll notice the problem persists: Codepen example

.wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    width: 200px;
    height: 200px;
    background-color: white;
}

.item {
    margin: auto;
    width: 50px;
    height: 50px;
    background-color: red;
}

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

I'm currently working on a course assignment and I've encountered an issue where nothing happens when I try to execute node

Having trouble with the readline for input execution. Any helpful hints or tips? I've been using it in the same file for all exercises, checked stackoverflow too but realized I'm on Windows. This is my code const { rejects } = require('asse ...

Using a regular expression to replace occurrences of a string in Objective-C

Can someone help me figure out how to use a regex expression to split my string that contains HTML code? NSString* regex = @"<.*?>"; NSString* html = @"<span class="test">Test1</span><span class="test">Test2</span><span cl ...

Issue with submitting a form within a React modal - lack of triggering events

I am utilizing the npm package react-modal (https://www.npmjs.com/package/react-modal) in my project. The issue I am facing is that when I click on 'Submit', nothing happens. The function handleSubmit</a> is not being triggered, as no conso ...

Block entry to HTML files unless the user has logged in utilizing PHP sessions

I have been working on implementing a verification process to check if a user is logged in when accessing a specific HTML file. When a user tries to access something.html without being logged in, I would like to redirect them to index.php and restrict acc ...

Header becomes distorted while scrolling down, then returns to normal when scrolling back up

Something strange is happening on my client's website. Whenever I scroll down and then back up, the header displays a large white area where the address/contact bar should be. You can see it in action on the site: rijschool-wolvega.nl I'm not w ...

Creating and adding nested div elements with the power of JavaScript

My goal is to utilize JavaScript for updating the CSS layout as the webpage loads. Below is the code I have been working on: var container = 0; // Total UI Container var containerTitle = 0; // Title Container var article = 0; var articleTitle = 0; var ...

Adjusting the background color of <tr> depending on the number of <tr> elements in the HTML using CSS

When working on my HTML page, I have utilized multiple <tr> tags with different bgcolor values such as: <tr bgcolor="#000000"> <tr bgcolor="#E5F1CC"> <tr bgcolor="#D30A0A"> <tr bgcolor="#656766"> I am aiming to assign unique ...

choose the li element that is located at the n-th position within

Need help with HTML code <div class="dotstyle"> <ul> <li class="current"><a href="javascript:void(0)"></a></li> <li><a href="javascript:void(0)"></a></li> <li><a href="javasc ...

How can I alter the text color on hover within a Material UI card? My goal is to have the text color change when hovering over the card itself, rather than just the text

When I apply CSS to the card hover effect, it works fine. However, I also want to change the text color along with the card color on hover. card: { maxWidth: 350, height: 300, '&:hover': { backgroundColor: '#373737 !impor ...

Adjusting various settings on breakpoints in MUI v5

Previously in MUI version 4, I was able to apply multiple style parameters within a single media query using the following code: [theme.breakpoints.up('xs')]: { width: 100px, color: green, }, [theme.breakpoints.up('md' ...

Go back to the top by clicking on the image

Can you help me with a quick query? Is it feasible to automatically scroll back to the top after clicking on an image that serves as a reference to jQuery content? For instance, if I select an image in the "Portfolio" section of , I would like to be tak ...

Different CSS values can be applied for specific versions of Internet Explorer by using conditional comments

I am dealing with a CSS class named "myclass" that requires a z-index of 5 specifically for IE8. I have attempted to achieve this using the following methods: .myclass{ z-index: 5\9; } ---> Interestingly, this method applies from IE10 onwards a ...

Creating responsive modal windows in Vue and Laravel to dynamically adjust to the screen size

I am currently working with a modal window code snippet. <transition name="modal" @close="showModal = false"> <div class="modal-mask" style=" width: 90% !important;"> <div class="modal-wrapper"> < ...

Aligning content within an <a> container

I am in the process of creating a square box that is clickable and will redirect users to another page. To achieve this functionality, as well as implement hover effects later on, I am utilizing the < a > element. Below is the HTML markup: <a id ...

What causes the HTML element's X position value to double when its X position is updated after the drag release event in Angular's CDK drag-drop feature?

I am facing a challenge with an HTML element that has dual roles: Automatically moving to the positive x-level whenever an Obsarbalve emits a new value. Moving manually to both positive and negative x-levels by dragging and dropping it. The manual drag a ...

What is the best way to extract the frameset from a frame window?

Here is a code snippet to consider: function conceal(frameElem) { var frameSet = frameElem.frameSet; //<-- this doesn't seem to be working $(frameSet).attr('cols', '0,*'); } //conceal the parent frame conceal(window.pa ...

Having trouble with Safari on your iPhone? Seeing image outlines peeking through radius borders?

Has anyone encountered a similar issue before? I'm experiencing this problem specifically on Safari for iPhone. Problem: https://i.sstatic.net/ffqWz.jpg Solution: https://i.sstatic.net/z1bUB.png Below is the HTML code snippet: <div class="row"& ...

Should the username be specified but the password left blank, then an error message will be shown

<div class="form"> <h1>Sign In</h1> <form action="" method="post" name="login"> <input type="text" name="username" placeholder="Enter username" required /> <input type="password" name="password" placeholder="Enter password" ...

PHP - the button remains unchanged after submission

My Objective: Upon button press, I want to update the database table. Additionally, the button should change its color and text. Issue at Hand: The button fails to change unless I manually refresh the page. I attempted using echo "<meta http-eq ...

Easy way to eliminate empty elements following a class using jQuery

I'm encountering a situation where I have a group of elements following a specific class that are either empty or contain only white space. <div class="post-content"> <div class="slider-1-smart"> --- slider contents --- < ...