Incorrect image sizing in IE11 in flexbox - html

I am trying to position an image in a div. It should be centered. The div should have a minimum width and it should grow only if text below the image requires it.
The following code demonstrates what I want in Chrome:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
background-color: aliceblue;
}
.loading-spinner-overlay-1 {
left: 0;
top: 0;
right: 0;
bottom: calc(100% - 300px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-overlay-2 {
left: 0;
top: 300px;
right: 0;
bottom: calc(100% - 600px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-background {
min-width: 100px;
min-height: 100px;
max-width: 50%;
background-color: white;
border-radius: 10px;
display: flex;
flex-direction: column;
z-index: 1;
padding: 20px;
}
.loading-spinner-container {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.loading-spinner-container > img {
margin: auto;
}
.loading-spinner-container > p {
text-align: center;
margin: 0;
}
<img src="http://placehold.it/40x40">
<div class="loading-spinner-overlay-1">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
</div>
</div>
</div>
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
<p>
Some long text
</p>
</div>
</div>
</div>
However, in IE11, it looks like this:
Am I doing something wrong? Or is this a bug in IE11?
What can I do to fix this?
I have tried setting max-width: 100% and flex-shrink:0 on the img tag as some google results suggest, but this didn't help.

Updated
Adding align-items: flex-start to the loading-spinner-container fixes the issue, which kind of make sense, since align-items default is stretch and works cross axis (in this case horizontal) for flex column direction.
Updated, 2nd revision
Additionally, to fix the vertical centering, and since IE11 again has some issues when it comes to min-height, remove flex-direction: column from the loading-spinner-background and move min-height: 100px to loading-spinner-container.
Stack snippet
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
background-color: aliceblue;
}
.loading-spinner-overlay-1 {
left: 0;
top: 0;
right: 0;
bottom: calc(100% - 300px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-overlay-2 {
left: 0;
top: 300px;
right: 0;
bottom: calc(100% - 600px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-background {
min-width: 100px;
max-width: 50%;
background-color: white;
border-radius: 10px;
display: flex;
/* flex-direction: column; removed */
z-index: 1;
padding: 20px;
}
.loading-spinner-container {
min-height: 60px; /* added/value changed (moved from *-background class) */
display: flex;
flex-direction: column;
flex-grow: 1;
align-items: flex-start; /* added */
}
.loading-spinner-container > img {
margin: auto;
}
.loading-spinner-container > p {
text-align: center;
margin: 0;
}
<img src="http://placehold.it/40x40">
<div class="loading-spinner-overlay-1">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
</div>
</div>
</div>
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
<p>
Some long text
</p>
</div>
</div>
</div>
Also align-items: center can be used, and if combined with justify-content: center, you can drop the margin: auto on the img.
Fiddle demo
Updated, 3rd revision, based on a comment
Longer text appears to not wrap on IE.
As shown in this post, IE need to have the width set explicit on the flex item, here p, and since also loading-spinner-container is a flex column item (for row item flex-grow is enough), it needs one too (or overflow: hidden).
Fiddle demo

Try this:
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<div class="img-container"> <!-- added this -->
<img src="http://placehold.it/40x40">
<p>
Some long textwrwerwer
</p>
</div>
</div>
</div>
</div>
Then added new class:
.loading-spinner-container .img-container {
clear:both;
text-align:center;
}
Basically I added a wrapping div.
Hope it helps.

Related

how to center div in the middle of the page?

After googling for hours, I am still stuck at this problem. Since I used flex-direction:column, to align the child divs hence, I used justify-content:center but the child divs look aligned to the left.
<div class=container>
<div class= "wrapper">
<div class = "image-float">
<img class = "profile-picture" src = "{% static 'images/image.jpg'%}">
</div>
<div>
<p>Welcome to Sparison...</p>
</div>
<div class="intro">
<h1>Copy code below and share with friends</h1>
</div>
<div class="url-container input-group">
<input type="text" id="random" class="url input-border form-control" value="">
<div class="input-group-append">
<span class="input-border input-group-append input-group-text">
<i class="far fa-copy url-copy-icon"></i></span>
</div>
</div>
</div>
</div>
The relevant CSS is below:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: Montserrat;
font-size: 50px;
font-weight: bold;
background-color: var(--very-pale-blue);
width:100%;
height:100vh;
}
.container{
max-width: 100%;
height: 90vh;
margin-top: 0;
}
.wrapper{
display: flex;
flex-wrap: wrap;
max-width: 100%;
height: 90vh;
justify-content: center;
flex-direction: column;
}
.url-container .input-group {
border-radius: 10px;
width: 50%;
}
.input-group {
position: relative;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-align: stretch;
align-items: stretch;
width: 50%;
/* background-color: #1DB954; */
border-radius: 2px;
}
Above is how the page renders. every element under the wrapper div should be in the middle of the page centered vertically.
You can find more information about flex here:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
.wrapper{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: 100%;
height: 90vh;
}
Link to jsfiddle:
https://jsfiddle.net/y4qra692/
Change the container width
.container {
width: 100%;
}
Then for the wrapper as you have changed the flex-direction to column you now need align-items rather than justify-content as (confusingly) when you change the direction these properties swap around:
.wrapper {
width: 100%;
display: flex;
align-items: center;
}
To make div centered you can add this code to .wrapper
.wrapper{
width: 50%;
margin:auto;
}
To make a div centered:
<section class="middle">
<div class="div">
<p>Hi</p>
</div>
</section>
Style.css
.middle {
height: 100vh; /* or height you want */
width: 100%;
position: relative;
}
.div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

Eliminate "padding" (visual space) with object-fit, or, how do you scale a row of images to fit a div properly?

I have a fixed-height interface I'm styling with CSS. I want it to be responsive to browser height (and, eventually, width... but one problem at a time) and I have a fiddle in which the interface operates almost exactly as I'd like it to with respect to browser height... with one exception.
I use a flexbox layout with object-fit: scale-down to force the row of images in the green div to shrink when their containing div is not tall enough to fit the images at native dimensions. This results in some "padding," the existence of which is perfectly well explained here. I've made the background color of the relevant div blue so that you can clearly see the visual space I'm talking about. I do not want this space to appear at all.
So, what is the proper way to make a row of images responsive in the way I'd like without introducing additional visual space between the images if object-fit cannot do this? Thank you for the input.
body {
font-family: arial;
font-size: 26px;
text-align: center;
color: black;
background-color: white;
}
.smallhint {
font-size: 16px;
color: #8c8c8c;
}
img {
padding: 0;
margin: 0;
font-size: 0;
display: block;
object-fit: scale-down;
min-height: 0;
}
.flex-column {
display: flex;
flex-direction: column;
padding: 0px;
margin: 0px;
height: 90vh;
flex-grow: 0;
min-width: 0;
min-height: 0;
}
.flex-row {
display: flex;
flex: 0 1.5 auto;
flex-direction: row;
justify-content: center;
align-items: center;
min-height: 0;
background-color: green;
}
.context {
display: flex;
flex-direction: column;
max-height: 100%;
background-color: blue;
}
.primary {
position: relative;
z-index: 0;
right: 0;
bottom: 0;
padding: 0;
font-size: 0;
min-height: 0;
align-items: end;
background-color: orange;
}
.primary img {
margin-left: auto;
margin-right: auto;
border-style: solid;
border-width: 3px;
border-color: black;
height: calc(100% - 2*3px);
}
.mask {
position: absolute;
z-index: 1;
top: 0;
right: 0;
width: 100%;
height: 100%;
font-size: 0;
}
.nonimage {
padding-top: 5px;
display: inline;
}
<div class="container">
<div class="flex-column">
<div class="primary">
<img src="https://via.placeholder.com/200">
<div class="mask">
<img src="https://via.placeholder.com/200/FF000">
</div>
</div>
<div class="flex-row">
<div class = "context">
<img src="https://via.placeholder.com/75x250">
</div>
<div class = "context">
<img src="https://via.placeholder.com/150x75">
</div>
</div>
<div class="nonimage">
<div class="smallhint">Some Text<br>Other Text</div>
</div>
</div>
</div>

flex direction "row" inside a flexbox with flex-direction "column"

I am trying to make a navbar using flexbox. In my code I have the actual navbar wrapped with flex- direction:"row" to align the logo and the button.
Now I want to have the nav-inner (the beige div) under the navbar (that should be 100vw wide), but actually it sits next to the navbar.
I have tried to change the flex-direction to "column" inside my nav-menu div, but the Hamburger button goes out of the screen. Am I doing something wrong?
body {
margin: 0;
overflow: hidden;
}
/* defaults */
.safe-view {
display: flex;
height: 100vh;
}
.hamburger {
height: 30px;
width: 30px;
}
/**/
/* navbar */
.navbar {
position: sticky;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 30px;
width: 100vw;
font-size: 1.2em;
height: 100px;
}
.nav-menu {
width: 100%;
display: flex;
flex-direction:row;
/*flex-direction:column;*/
position: absolute;
height: 100%;
overflow: hidden;
background-color: white;
}
.nav-inner {
height: 100%;
width: 100%;
background-color: blanchedalmond;
}
/**/
<div class="safe-view">
<div class="nav-menu">
<div class="navbar">
<h1>logo</h1>
<button class="hamburger"></button>
</div>
<div class="nav-inner"></div>
</div>
</div>
This is a CSS box-model issue. You need to add box-sizing: border-box. This will ensure that padding is included in calculation of the width.
*{
box-sizing: border-box;
}
By default box-sizing is set to content-box. This will only care about the element content and shift padding and border outside of the element. That is why you saw the button push out to the right! This can also help you to understand further.
Also, flex-direction for .nav-menu needs to be set to column in order to position .nav-inner below.
Heres an alternative. I removed padding and just used calc() function to create padding. But always include box-sizing:border-box in your CSS :)
*{
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.safe-view {
display: flex;
height: 100vh;
}
.navbar {
position: sticky;
display: flex;
flex-direction:row;
align-items: center;
justify-content: space-between;
width: calc(100vw - 60px);
margin: 0 auto;
font-size: 1.2em;
height: 100px;
}
.nav-menu {
width: 100%;
display: flex;
flex-direction:column;
position: absolute;
height: 100%;
overflow: hidden;
background-color: white;
}
.nav-inner {
height: 50px;
width: 100%;
background-color: blanchedalmond;
}
<div class="safe-view">
<div class="nav-menu">
<div class="navbar">
<h1 class="logo">logo</h1>
<button class="hamburger">button</button>
</div>
<div class="nav-inner"></div>
</div>
</div>

CSS creating nested div box for parent div causes overlapping [duplicate]

This question already has answers here:
CSS: Width in percentage and Borders
(5 answers)
Closed 3 years ago.
I want to create a bar to go along the top of a box on a website that I am working on.
This is the desired outcome
Here's my code, I keep getting this overlap
.page {
display: flex;
flex-wrap: wrap;
position: relative;
}
.section {
border: 2px solid #FBA7FF;
width: 85%;
height: 30%;
margin: 1vw;
padding: 1vw;
display: flex;
align-items: center;
position: relative;
z-index: 1;
}
.section h1 {
position: relative;
}
.section_header {
border: 4px solid #FBA7FF;
position: absolute;
top: 0;
left: 0;
width: 100%;
bottom: 95%;
}
<div class='page'>
<div class='section'>
<div class="section_header"></div>
<h1>sample text</h1>
</div>
</div>
So far I've got the parent div with position: relative and the child element with position: absolute then setting top and left to 0 width to 100% and bottom to 95% to attempt the desired effect yet it creates an overlap.
I can see that 0 is within the div and doesn't take into account the border which is perhaps why this is happening.
* {
box-sizing: border-box;
margin: 0;
}
.page {
display: flex;
flex-wrap: wrap;
position: relative;
}
.section {
width: 100%;
display: inline-block;
text-align: center;
}
.section_header {
width: 100%;
background: #FBA7FF;
display: block;
height: 70px;
margin-bottom: 20px;
}
<div class='page'>
<div class='section'>
<div class="section_header"></div>
<h1>sample text</h1>
</div>
</div>
Remove the position:absolute and use flex-direction:column;
* {
margin: 0;
padding: 0;
}
.page {
display: flex;
flex-wrap: wrap;
flex-direction: column;
min-height: 100vh;
background: lightgrey;
position: relative;
}
.section {
border: 2px solid #FBA7FF;
width: 85%;
margin: 1vh auto;
height: 30%;
background: lightgreen;
display: flex;
flex-direction: column;
flex: 1;
align-items: center;
}
.section_header {
height: 50px;
width: 100%;
background: orange;
}
<div class='page'>
<div class='section'>
<div class="section_header"></div>
<h1>sample text</h1>
</div>
</div>

How to make image take maximum space in flex container?

I need to make an image take the maximum size (taking max width or max height) in a flex container.
Since the parent container doesn't have a fixed width and height, I can't use max-width and max-height and flex:1 is not working either.
Here is an example of the problem : https://jsfiddle.net/vb26u0e5/2/
I would like the image to take automaticaly all the available green space (remove the width: 40px; line 20).
#mainContainer {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
color: white;
}
#imageContainer {
flex: 1;
display: flex;
justify-content: space-between;
background-color: green;
}
#image {
width: 40px;
}
#previous,
#next {
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
padding: 0 10px;
background-color: purple;
}
#title,
#footer {
text-align: center;
}
#title {
background-color: blue;
}
#footer {
background-color: red;
}
<div id="mainContainer">
<div id="title">TITLE</div>
<div id="imageContainer">
<div id="previous"><</div>
<img id="image" src="https://via.placeholder.com/1920x1080" />
<div id="next">></div>
</div>
<div id="footer">FOOTER</div>
</div>
Add this to your code:
#image {
width: 40px;
flex-grow: 1; /* new */
}
Normally, you would be able to use flex-basis (which is equivalent to width, in this case), and do some like this:
#image {
flex: 1; /* fg:1, fs:1, fb:0 */
}
OR
#image {
flex: 1 0 40px;
}
However, some browsers have a bug which causes them to ignore flex-basis in nested flex containers. So the width / flex-grow combination is a clean workaround.
For more details see the "Browser Bugs" section in my answer here:
What are the differences between flex-basis and width?
The demo below covers the issue answered above, plus height issues – aspect ratio and vertical scroll – by wrapping the image in a div and using absolute positioning and object-fit on the image.
Tested in Chrome, Firefox and Edge.
#mainContainer {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
color: white;
}
#imageContainer {
flex: 1;
display: flex;
justify-content: space-between;
background-color: green;
}
#image {
flex-grow: 1;
position: relative;
}
img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
#previous,
#next {
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
padding: 0 10px;
background-color: purple;
}
#title,
#footer {
text-align: center;
}
#title {
background-color: blue;
}
#footer {
background-color: red;
}
<div id="mainContainer">
<div id="title">TITLE</div>
<div id="imageContainer">
<div id="previous"><</div>
<div id="image">
<img src="https://pixabay.com/get/52e3dc454f50a414f6d1867dda6d49214b6ac3e45657744e7d2b72dc90/oldtimer-4396528_1920.jpg" />
</div>
<div id="next">></div>
</div>
<div id="footer">FOOTER</div>
</div>
jsFiddle
use object-fit property to specify how the image should be resized to fit its container. I have set it to object-fit: cover which will cut off the sides of the image, preserving the aspect ratio, and also filling in the space. Also use flex-grow:1 to fill the 'green' space.
#mainContainer {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
color: white;
}
#imageContainer {
display: flex;
justify-content: space-between;
background-color: green;
}
#image {
object-fit: cover;
flex-grow: 1;
height: 100%;
overflow: hidden;
}
#previous,
#next {
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
padding: 0 10px;
background-color: purple;
}
#title,
#footer {
text-align: center;
}
#title {
background-color: blue;
}
#footer {
background-color: red;
}
<div id="mainContainer">
<div id="title">TITLE</div>
<div id="imageContainer">
<div id="previous"><</div>
<img id="image" src="https://via.placeholder.com/1920x1080" />
<div id="next">></div>
</div>
<div id="footer">FOOTER</div>
</div>
You can calc the width based on Vertical Width (100vw) & remove the padding of your PREV/NEXT
Something like this?
#mainContainer {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
color: white;
}
#imageContainer {
flex: 1;
display: flex;
justify-content: space-between;
background-color: green;
}
#image {
width: calc(100vw - 50px);
height: 100vh;
}
#previous,
#next {
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
padding: 0 10px;
background-color: purple;
}
#title,
#footer {
text-align: center;
}
#title {
background-color: blue;
}
#footer {
background-color: red;
}
<div id="mainContainer">
<div id="title">TITLE</div>
<div id="imageContainer">
<div id="previous"><</div>
<img id="image" src="https://via.placeholder.com/1920x1080" />
<div id="next">></div>
</div>
<div id="footer">FOOTER</div>
</div>