I have a container that contains both an image, and some text. I want the text to be vertically and horizontally centered in the middle of the image.
It seems that the easiest, forward-thinking method is to use Flexbox and absolute positioning of the text:
.container {
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.text {
position: absolute;
}
<div class='container'>
<div class='text'>
This should be centered
</div>
<img src="https://placehold.it/250x250" />
</div>
The text is centered in both axes. This seems to work in all modern browsers..... except Safari.
Safari appear to not center the text at all. It's just sitting at the top/left of the container like a normal absolutely positioned element would be in a non-flexbox layout.
Safari (wrong):
All other browsers (correct):
I thought Flexbox was really ready for primetime, but this seems like a deal-breaker considering how many people using Safari on iOS. Centering content horizontally and vertically is something Flexbox should be great at.
My only real alternative here is to not use Flexbox and then use:
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
But I would much rather be using Flexbox for progressive reasons.
Is there anything I'm missing here? As much as I hate to ask for it, is there some of Safari-only work-around here? Is there any solution to this besides just "not using flexbox" because if that is the answer in mid-2017 that is going to be disappointing.
JSFiddle for reference: https://jsfiddle.net/z7kh5Laz/4/