【发布时间】:2017-09-27 18:01:32
【问题描述】:
我正在尝试将图像定位在 div 中。它应该居中。 div 应该有一个最小宽度,并且只有在图像下方的文本需要它时才应该增长。
以下代码演示了我在 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>
但是,在 IE11 中,它看起来像这样:
我做错了吗?或者这是 IE11 中的错误?
我能做些什么来解决这个问题?
我尝试按照一些谷歌结果的建议在img 标签上设置max-width: 100% 和flex-shrink:0,但这没有帮助。
【问题讨论】:
-
您是在添加正确的供应商前缀,还是使用 Autoprefixer 之类的工具自动添加? autoprefixer.github.io
-
@TedWhitehead:不,我没有,但据我所知,不需要这样的前缀。我用 Autoprefixer 的 CSS 创建了一个小提琴,它遇到了同样的问题:jsfiddle.net/DHilgarth/hterd97n
-
我认为这是placehold.it的问题,IE无法确定图像的固有大小。它似乎可以与 satyr.io/40x40 一起正常工作,或者如果您将内联
width属性添加到 img 标记。 -
@TedWhitehead:嗯,我已经根据我的真实应用构建了这个示例。在我的应用程序中,我使用的是 gif,而不是 placehold.it 中的图像......此外,使用占位符时没有区别。这是完全相同的问题。
-
@TedWhitehead:将 img 标签的宽度和高度显式设置为图像大小(以像素为单位)将解决问题,对。但这不是一个真正的解决方案,因为图像可能会被另一个大小不同的图像替换。
标签: html css flexbox internet-explorer-11