Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't justify-content: center work in IE?

I have this simple div with a button inside of it. justify-content: center; works fine using Firefox and Chrome, but does not work on IE 11:

#div {    height: 200px;    width: 50px;    border: 1px solid black;    display: flex;    flex: 0 0 auto;    align-items: center;    justify-content: center;  }  #button {    height: 50px;    width: 200px;    min-width: 200px;    border: 1px solid black;    background-color: red;  }
<div id="div">    <button id="button">HELLO</button>  </div>

My goal is that, when I use transform with rotate(90deg) or rotate(270deg), the button will fit into the div:

#div {    height: 200px;    width: 50px;    border: 1px solid black;    display: flex;    flex: 0 0 auto;    align-items: center;    justify-content: center;  }  #button {    height: 50px;    width: 200px;    min-width: 200px;    border: 1px solid black;    background-color: red;    transform: rotate(90deg);  }
<div id="div">    <button id="button">HELLO</button>  </div>

The height and width of the div and button are always the same, but are customizable.

As much as possible, I prefer not wrapping elements.

like image 660
KaeL Avatar asked Mar 26 '15 03:03

KaeL


People also ask

How do you apply justify content?

The justify-content property aligns the flexible container's items when the items do not use all available space on the main-axis (horizontally). Tip: Use the align-items property to align the items vertically.

Does justify content work without display flex?

justify-content only has an effect if there's space left over after your flex items have flexed to absorb the free space. In most/many cases, there won't be any free space left, and indeed justify-content will do nothing.

What is the difference between align-items Center and justify Content Center?

Properties that control alignment The properties we will look at in this guide are as follows. justify-content — controls alignment of all items on the main axis. align-items — controls alignment of all items on the cross axis. align-self — controls alignment of an individual flex item on the cross axis.

What does justify Content Center do?

The CSS justify-content property defines how the browser distributes space between and around content items along the main-axis of a flex container, and the inline axis of a grid container.


2 Answers

IE11 needs the parent to have flex-direction: column.

This example has your button rotated:

#div {    height: 200px;    width: 50px;    border: 1px solid black;    display: flex;    align-items: center;    justify-content: center;    flex-direction: column  }  #button {    height: 50px;    width: 200px;    min-width: 200px;    border: 1px solid black;    background-color: red;    transform: rotate(90deg);  }
<div id="div">    <button id="button">HELLO</button>  </div>
like image 165
misterManSam Avatar answered Oct 02 '22 18:10

misterManSam


In my case I had to make the flex container's height 100%. justify-content worked without a problem after that.

I also had to make the (first level) children's max-width 100% to fix some content overflowing horizontally.

like image 28
Rafalfaro Avatar answered Oct 02 '22 18:10

Rafalfaro