【发布时间】:2017-01-16 08:41:30
【问题描述】:
我有一个 HTML 结构,其中有几个使用 flexbox 布局的列表项,其中有几个我正在使用嵌套的 flexbox 布局的元素。
在 Chrome、IE11 和 Edge 中,由于 Flexbox 和 height: 100%,所有列表项的高度都得到了有效匹配。提交按钮(“添加到购物篮”)因此是垂直对齐的。
但是,在 Safari(桌面版,版本 9.1.2)中,height: 100% 不适用(它可能没有看到作为基础的父高度值)。因此,框的高度不相等,并且我的元素未对齐。
如何修改 CSS 以使 Safari 与 Chrome 的呈现行为匹配?
body {
padding: 0;
}
ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
}
li {
padding: 0 10px;
flex: 1 1 25%;
}
figure {
margin: 0;
background-color: rgba(255, 0, 0, 0.25);
height: 100%; // this isn't working in safari
display: flex;
flex-direction: column;
a {
flex: 1;
flex-shrink: 0;
}
img {
display: block;
width: 100%;
}
}
figcaption {
height: 100%; // this isn't working in safari
flex: 1;
display: flex;
flex-direction: column;
}
.itemname {
flex: 1;
flex-grow: 1;
margin-bottom: 10px;
}
.price {
float: right;
font-weight: bold;
}
form {} select {
display: block;
width: 100%;
margin-bottom: 10px;
}
button[type='submit'] {
display: block;
width: 100%;
}
<ul>
<li>
<figure>
<a href="#">
<img src="http://placehold.it/400" />
</a>
<figcaption>
<div class="itemname"><span class="price">$10</span>Product</div>
<form>
<select>
<option selected>Please select</option>
</select>
<button type="submit">Add to Basket</button>
</form>
</figcaption>
</figure>
</li>
<li>
<figure>
<a href="#">
<img src="http://placehold.it/400" />
</a>
<figcaption>
<div class="itemname"><span class="price">$10</span>Product Two With A Much Longer Name That Could Wrap</div>
<form>
<select>
<option selected>Please select</option>
</select>
<button type="submit">Add to Basket</button>
</form>
</figcaption>
</figure>
</li>
<li>
<figure>
<a href="#">
<img src="http://placehold.it/400" />
</a>
<figcaption>
<div class="itemname"><span class="price">$10</span>Product Three No Select</div>
<form>
<button type="submit">Add to Basket</button>
</form>
</figcaption>
</figure>
</li>
<li>
<figure>
<a href="#">
<img src="http://placehold.it/400" />
</a>
<figcaption>
<div class="itemname"><span class="price">$10</span>Product</div>
<form>
<select>
<option selected>Please select</option>
</select>
<button type="submit">Add to Basket</button>
</form>
</figcaption>
</figure>
</li>
</ul>
【问题讨论】:
-
哪个版本的 safari?
-
@Dekel Safari 版本 9.1.2 (11601.7.7) 在 OS X 上
-
@Michael_B 我查看了那个问题和 Paulie_D 提到的那个问题,并首先查看了答案,但是在尝试了针对我的代码 sn-p 的解决方案后,我找不到使用绝对定位的解决方案包装器,我无法指定固定的高度值(加上 Chrome 表现良好,所以这不是一般的 Webkit 问题)。