我正在尝试使用flexbox实现以下响应式网格布局,但是如果没有固定的高度,我将无法正确获得“桌面”版本。我希望#1靠左靠自己,而#2和#3靠右靠在一起(#1不必与右边的col高度相同)
HTML(简体)
<div class="wrapper">
<div class="inner">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
</div>
CSS(简体)
.wrapper {
max-width: 600px;
margin: 0 auto;
border: 1px solid #c4c4c4
}
.col1, .col2, .col3 {
background: #fe4c4c;
position: relative;
margin: 10px;
padding: 5px;
color: #FFF;
font-weight: bold;
font-size: 40px;
font-family: sans-serif;
min-height: 100px;
&:after {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
.col1 {
&:after {
content: '1';
}
width: 100px;
}
.col2 {
&:after {
content: '2';
}
width: calc(100% - (100px + 60px))
}
.col3 {
&:after {
content: '3';
}
width: calc(100% - 30px);
}
.inner {
width: 100%;
display: flex;
flex-wrap: wrap;
}
@media only screen and (min-width: 768px) {
.inner {
flex-direction: column;
}
.col1 {
width: 300px;
}
.col2 {
width: auto;
}
.col3 {
width: auto;
}
}
jsFiddle https://jsfiddle.net/pcwudrmc/28053/
我一直在寻找类似的问题,但是没有固定的高度找不到答案。
谢谢!
答案 0 :(得分:1)
使用CSS网格的解决方案:
https://codepen.io/seanstopnik/pen/bd511c728bb79d02d11ae04edbfe9a33
* {
box-sizing: border-box;
}
.wrapper {
max-width: 600px;
margin: 0 auto;
border: 1px solid #c4c4c4;
padding: 10px;
}
.col1,
.col2,
.col3 {
background: #fe4c4c;
position: relative;
color: #fff;
font: {
family: sans-serif;
size: 40px;
weight: 700;
}
min-height: 100px;
&:after {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
.col1:after {
content: '1';
}
.col2:after {
content: '2';
}
.col3:after {
content: '3';
}
// Inner
.inner {
display: grid;
grid: {
template-columns: 100px auto;
template-rows: 100px;
gap: 10px 10px;
}
@media (min-width: 768px) {
grid-template-columns: 300px auto;
}
}
.col1 {
@media (min-width: 768px) {
grid: {
column: 1 / span 1;
row: 1 / span 2;
}
}
}
.col3 {
grid: {
column: 1 / span 2;
row: 2 / span 2;
}
@media (min-width: 768px) {
grid: {
column: auto;
row: auto;
}
}
}