Flexbox:如何编写简单的响应式并排文章布局?



简单的想法:2篇文章并排,除非它们的宽度低于阈值(例如400px(,在这种情况下,文章垂直排列。无论哪种方式,内容都应填充视区的宽度。

下面的代码段部分正确。每篇文章是视口宽度的 49%,最小宽度为 400px,因此如果视口低于 2x400px(给或取几个像素(,文章将垂直排列。但是,它们只能获得视口宽度的 49%,这在垂直排列时很糟糕。这就是我卡住的地方。思潮?

谢谢!

* {
box-sizing: border-box;
}
.main {
border: 1px solid blue;
display: flex;
flex-wrap: wrap;
}
.contentPanel {
border: 1px solid goldenrod;
min-width: 400px;
width: 49vw;
}
<html lang="en">
<body>
<!-- Main Area -->
<div class='main'>
<div class="contentPanel">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<div class="contentPanel">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
in section 1.10.32.
</div>
</div>
</body>
</html>

flex:1 1 200px;这意味着:

  1. flex-grow:1;有空间就成长
  2. flex-shrink:1;收缩以适合
  3. flex-basis:200px;如果容器宽度低于400px则换行

* {
box-sizing: border-box;
}
body {
margin: 0;
}
.main {
display: flex;
flex-wrap: wrap;
}
.contentPanel {
border: 1px solid ;
flex: 1 1 200px;
}
<!-- Main Area -->
<div class='main'>
<div class="contentPanel">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<div class="contentPanel">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
1.10.32.
</div>
</div>

最新更新