【发布时间】:2019-03-12 07:33:23
【问题描述】:
编辑:这是一个代码笔和一个展示问题的屏幕截图
我正在尝试使用 flexbox 使图像居中对齐,保持其纵横比,并在其下方具有透明背景(检查下面的图像)。 我通过创建一个全高宽度的父容器来解决这个问题,它包含 4 行(第一行是标题,第二是徽标,第三是社交链接,第四是 chevron-down-icon) 所以父容器有 flex-direction: row 包含这 4 个。 在徽标容器中,我有一个带有 flex-direction: 列的父列容器,其中包含 2 个带有 flex: 1 的空 div 和我的应用程序徽标。
混合
=full-height-width
width: 100%
height: 100%
=fill($background-color: null)
flex: 1
display: flex
height: 100%
=flex-center-contents
display: flex
justify-content: center
align-items: center
// grid mixins
=row($z-index: auto)
display: flex
flex-direction: column
z-index: $z-index
flex-wrap: nowrap
=column($z-index: auto)
display: flex
flex-direction: row
z-index: $z-index
flex-wrap: nowrap
样式
// Grid implementation
.fill
+fill($logo-background-color)
z-index: $not-animated-background-z-index
.parent-row-container
+full-height-width()
+row($not-animated-background-z-index)
.parent-column-container
+full-height-width()
+column($not-animated-background-z-index)
#parent-container
+full-height-width()
#social-container, #name-container, #logo-container
flex: 1
#social-container, #name-container
background-color: $logo-background-color
+flex-center-contents()
z-index: $not-animated-background-z-index
#name-container
> #name
color: $home-primary-text-color
font-family: $font-stack-sci-fi
font-size: $name-font-size
text-align: center
user-select: none
#logo-container
display: flex
> #logo-img-wrapper
> #logo
max-height: 100%
max-width: 100%
object-fit: contain
#social-container
display: flex
flex-wrap: wrap
> .social-icon
font-size: $social-icon-font-size
min-width: 85px
+media("<=tablet")
min-width: 45px
text-align: center
#bottom-arrow-container
background-color: $logo-background-color
padding-bottom: 1rem
+flex-center-contents()
z-index: $not-animated-background-z-index
HTML
<div class="parent-row-container">
<!-- Row -->
<div class id="name-container">
<p id="name">
Artist name
</p>
</div>
<!-- End Row -->
<!-- Row -->
<div id="logo-container" class="parent-column-container">
<!-- Column -->
<div class="fill"></div>
<!-- Column -->
<div id="logo-img-wrapper">
<img id="logo" src="@img/home/logo.png" />
</div>
<!-- Column -->
<div class="fill"></div>
</div>
<!-- End Row -->
<!-- Row -->
<div class="fill">
<!-- Column -->
<div id="social-container">
</div>
</div>
<div id="bottom-arrow-container">
<font-awesome-icon class="selectable" :icon="['fa', 'chevron-down']" :style="{ color: 'white'}" />
</div>
</div>
在 Chromium/opera 上运行时会导致以下结果(预期)
这与 Firefox 如何处理百分比宽度有关吗? 如果我修改 img 容器并将 flex: 1 放在图像容器上,firefox 是固定的,但是 chromium/opera 中断,并且它们使图像居中(因为 object-fit:contain)在边缘留下空间(也是预期的行为) ,如果我从 html 中完全删除 img,则网格与 chrome/opera 相同
你们有没有遇到过类似的事情?
【问题讨论】:
-
也许您只需要使用autoprefixer 使您的样式css 与其他浏览器兼容
-
我以为 vue-cli 已经使用了 autoprefixer。我可能错了,因为没有迹象表明它确实如此(通过快速 grep),尽管导出的 css 确实有一些属性前缀(参见我的第二个屏幕截图 -o-object-fit)
-
试试这个:把你导出的 css 和 autoprefixe here 然后我们将获取另一个解决方案!或者你的 Firefox 没有更新
-
This image 在 Chrome 和 Firefox 中的呈现方式不同。这是某种魔法吗?
-
@rv7 完全是我的想法!!
标签: css firefox flexbox cross-browser scale