【发布时间】:2021-03-08 17:59:33
【问题描述】:
我想用flex来实现这个结构:
这是响应式的,在较小的屏幕上它看起来像这样:
请注意,移动设备上的第一个 div 是第二个。
这是我的代码:
.card {
padding: 24px;
display: flex;
width: 400px;
background-color: #D9FBFF;
border-radius: 8px;
}
.right {
margin-left: auto;
display: flex;
flex-direction: column;
align-items: flex-end;
}
.div3 {
margin-top: auto;
}
.price {
display: flex;
align-items: baseline;
font-size: 20px;
font-weight: bold;
}
.unit {
margin-left: 6px;
font-size: 16px;
font-weight: normal;
}
.tag {
background: #D9DEEA;
border-radius: 7.5px;
height: 15px;
line-height: 15px;
text-align: center;
display: inline-block;
padding: 0 6px;
letter-spacing: 0.5px;
text-transform: uppercase;
font-size: 10px;
font-weight: bold;
}
.div3 {
display: flex;
align-items: flex-end;
flex-direction: column;
}
ul {
list-style: none;
padding: 0;
margin: 0;
margin-top: auto;
display: flex;
}
li:not(:last-child)::after {
content: "";
border-right: 1px solid #D9DEEA;
height: 14px;
margin: 0 12px;
}
h2 {
margin:0;
margin-bottom: 8px;
}
@media screen and (max-width: 800px) {
.card {
flex-direction: column;
}
.card > div {
width: 100%;
}
.right {
align-items: flex-start;
}
.div2 {
margin-bottom: 6px;
}
.div1 {
margin-bottom: 12px;
}
}
<article class="card">
<div class="div1">
<h2>This is a title</h2>
<div class="price">
<div>800</div>
<div class="unit">EUR</div>
</div>
</div>
<div class="right">
<div classs="div2">
<span class="tag">40% discount</span>
</div>
<div class="div3">
<ul>
<li>
<a href="#">LINK 1</a>
</li>
<li>
<a href="#">LINK 2</a>
</li>
</ul>
</div>
</div>
</article>
我可以实现第一个结构的唯一方法是在容器 div 中添加第二个和第三个 div,但我不知道如何解决这个问题,以便在较小的屏幕上将第二个 div 显示为第一个,我认为所有三个 div 应该处于同一级别,但是,这样做我不知道如何实现第一个结构。
我该如何解决这个问题?
【问题讨论】:
-
grid 是为此而设计的,而不是 flex :)
-
您可以在媒体查询中使用 order 属性。见这里:developer.mozilla.org/en-US/docs/Web/CSS/…
-
@G-Cyrillus 我不认为 IE11 完全支持它
-
IE11 需要告诉每个元素必须站在哪个单元格中,即使是 10 年前的浏览器,IE11 也可以这样做。
-
这里是 IE11 的示例,jsbin.com/sejazibipu/1/edit?css,output(jsbin 与 IE11 一起运行)
标签: html css flexbox internet-explorer-11