Flexbox: justify-content property in Internet Explorer [duplicate] - html

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.

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>

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.

Related

Why do I cannot center elements if i scale them

I am using this to center things in CSS:
.testclass {
display: flex;
justify-content: center;
}
but when i want to scale elements using width and height, it doesn't work and my elements are not centered.
Like this:
.testclass {
display: flex;
justify-content: center;
width: 200px;
height: 200px;
}
What's the problem?
This looks like the expected behavior.
Remember that in this case justify-content: center; centers what is inside the container - not the container itself.
EDIT:
I added margin: 0 auto; to center the container.
#container1 {
display: flex;
justify-content: center;
border: 1px solid red;
}
#container1 > div {
border: 1px solid blue;
background: yellow;
}
#container2 {
display: flex;
justify-content: center;
border: 1px solid red;
width: 200px;
height: 200px;
margin: 0 auto;
}
#container2 > div {
border: 1px solid blue;
background: yellow;
}
<div id="container1">
<div>test 1</div>
</div>
<div id="container2">
<div>test 2</div>
</div>
display: flex; and justify-content: center;
works for parent elements. That is, child elements of that particular parent will be centered, not the parent.
To center .testclassHTML
<div class="parent">
<div class="testclass"></div>
</div>
CSS
.parent {
background-color: red;
display: flex;
justify-content: center;
}
.testclass {
background-color: green;
width: 200px;
height: 200px;
}
If you want full center (horizontal vertical) you can use this code:
.container {
position: relative;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
<div class="container">
<div class="testclass">Content</div>
</div>

Flexbox centering not working in IE 11

justify-content and align-items using center seems to not be working in IE 11. In other browsers it works just as I would expect. Does anybody know a workaround?
.box {
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
.score-wrapper {
align-items: center;
display: flex;
justify-content: center;
min-height: 280px;
}
.overlay-circle {
border-radius: 100px;
border: 1px solid red;
fill: transparent;
height: 200px;
position: absolute;
width: 200px;
}
.center-circle {
border-radius: 90px;
border: 1px solid blue;
height: 180px;
position: absolute;
width: 180px;
}
<div class="box">
<div class="score-wrapper">
<div class="overlay-circle"></div>
<div class="center-circle">
<div class="score">
<p>800</p>
<p>Excellent</p>
</div>
</div>
</div>
Flexbox is pretty buggy when it comes to IE per CanIUse, 2 of which that are mentioned:
In IE10 and IE11, containers with display: flex and flex-direction: column will not properly calculate their flexed childrens' sizes if the container has min-height but no explicit height property. See bug.
IE 11 does not vertically align items correctly when min-height is used see bug
This being said, add explicit heights as a fallback on .score-wrapper for IE11.
.box {
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
.score-wrapper {
align-items: center;
display: flex;
justify-content: center;
min-height: 280px;
height: 280px;
}
.overlay-circle {
border-radius: 100px;
border: 1px solid red;
fill: transparent;
height: 200px;
position: absolute;
width: 200px;
}
.center-circle {
border-radius: 90px;
border: 1px solid blue;
height: 180px;
position: absolute;
width: 180px;
}

Why is these flex items not wrapping?

I have a flex item that is also a flex container .sub-con, problem is the flex item of .sub-con is refusing to wrap, even after adding : flex-flow: row wrap.
Can anyone fix this for me, or point out what I'm doing wrong.
.container {
display: flex;
justify-content: center;
align-items: center;
flex-flow: row wrap;
height: 100vh;
}
.sub-con {
margin-right: 50px;
margin-left: 50px;
height: 500px;
background-color: #fff;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.col-one {
height: 100px;
border: 1px solid lightgreen;
flex-grow: 2;
}
.col-two {
height: 100px;
border: 1px solid red;
flex-grow: 1;
}
<div class="container">
<div class="sub-con">
<div class="col-one"></div>
<div class="col-two"></div>
</div>
</div>
Your flex items in the nested container are sized with percentages.
.col-one{
width: 40%;
height: 100px;
border: 1px solid lightgreen;
}
.col-two{
width: 40%;
height: 100px;
border: 1px solid red;
}
Because percentage lengths are based on the length of the parent they have no reason to wrap. They will always be 40% of the parent, even if the parent has a width of 1%.
If you use other units for length, such as px or em, they will wrap.
jsFiddle demo
.container {
display: flex;
justify-content: center;
align-items: center;
flex-flow: row wrap;
height: 100vh;
}
.sub-con {
flex: 1; /* for demo only */
align-content: flex-start; /* for demo only */
margin-right: 50px;
margin-left: 50px;
height: 500px;
background-color: #fff;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.col-one {
width: 200px;
height: 100px;
border: 1px solid lightgreen;
}
.col-two {
width: 200px;
height: 100px;
border: 1px solid red;
}
<div class="container">
<div class="sub-con">
<div class="col-one"></div>
<div class="col-two"></div>
</div>
</div>

How can I align 2 buttons on the middle of the left and right sides of a div

I have a frame containing a picture, I would like to add 2 buttons on the left and right side of it so users can click on those to view different pictures. I can handle JavaScript but I can't figure out a way to align these 2 buttons in proper positions. Thank you.
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft {
float: left;
background: red;
}
.btnRight {
float: right;
background: blue;
}
<div class="frame">
<div class="content"></div>
</div>
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
You were on the right track but needed justify-content: space-between; not justify-content: center; and you needed to put .btnLeft and .btnRight inside .frame.
Here's what different values of justify-content do:
Image from CSS-Tricks
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: space-between; /* not center */
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
background: red;
}
.btnRight {
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="content"></div>
<div class="btn btnRight"></div>
</div>
You may want to put the buttons inside the frame so you can reference the left and right positions. Then make those buttons position:absolute then set left and right position for each buttons
Code:
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.btn{
position: absolute;
top: 50%;
transform:translateY(-50%);
}
.btnLeft{
left: 0;
}
.btnRight{
right: 0;
}
Updated fiddle:
https://jsfiddle.net/y0ty7t80/3/
First you could set the position of the frame to relative so it gets set as a root for following positionings. You could then set the positions of the two buttons both to "absolute" and put them inside of your frame so they get taken out of the text flow. By setting both to a left/right property of 0 and a top property of 50% they get placed exactly in the middle of the frame. Heres an example of what i mean:
.frame {
position:relative;
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
position:absolute;
top:50%;
left:0;
background: red;
}
.btnRight {
position:absolute;
top:50%;
right:0;
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
<div class="content"></div>
</div>

align span in the middle of a fixed size div

I have a fixed size div with a background image, and I want text to be aligned directly in the middle. Vertically and horizontally.
I'm able to get the horizontal alignment, but can't for the life of me get it to be vertically aligned. I've seen answers on here, but couldn't find anything that worked for a fixed size div.
You can see the blue border around the span. If I could just move that down to be always centered for any text(as long as it doesn't get too big) I'd be there.
div {
background-image: url(http://i.imgur.com/I86rTVl.jpg);
width: 500px;
height: 300px;
border: 1px solid red;
}
span {
position: absolute;
text-align: center;
width: 500px;
border: 1px solid blue;
font-size: 72px
}
<body>
<div>
<span>Testing for fiddle</span>
</div>
</body>
Out of many possible solutions, here is a modern flexbox technique.
Check the browser compatibility table for Flexbox
div {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center; /* Center vertically */
background-image: url("http://i.imgur.com/I86rTVl.jpg");
border: 1px solid red;
height: 300px;
width: 500px;
}
span {
/* position: absolute; Remove */
white-space: nowrap; /* Avoid text wrapping */
text-align: center;
width: 500px;
border: 1px solid blue;
font-size: 72px
}
<body>
<div>
<span>Testing for fiddle</span>
</div>
</body>
Absolute positioning
With the parent set to position:relative set the child's position to top:50% and the translate it back up by half it's own height.
div {
background-image: url(http://i.imgur.com/I86rTVl.jpg);
width: 500px;
height: 300px;
border: 1px solid red;
position: relative;
}
span {
position: absolute;
text-align: center;
width: 500px;
top: 50%;
left: 0%;
transform: translateY(-50%);
border: 1px solid blue;
font-size: 72px
}
<body>
<div>
<span>Testing for fiddle</span>
</div>
</body>
Try the good old table/table-cell technique. Works down to IE9 :)
div {
background-image: url(http://i.imgur.com/I86rTVl.jpg);
width: 500px;
height: 300px;
border: 1px solid red;
display:table;
}
span {
display:table-cell;
text-align: center;
width: 100%;
vertical-align:middle;
border: 1px solid blue;
font-size: 72px
}
<body>
<div>
<span>Testing for fiddle</span>
</div>
</body>
https://jsfiddle.net/kvqLnchw/1/
div {
background: url(http://i.imgur.com/I86rTVl.jpg) no-repeat;
width:500px;
height: 300px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
text-align:center;
}
span {
border: 1px solid blue;
font-size: 72px
}