【发布时间】:2020-06-16 12:05:05
【问题描述】:
这个用例是一个带有单选按钮的响应式表单。当在 PC 大屏幕上时,所有单选按钮通常会在屏幕上排成一行(例如带有 flex-direction: row 的非包装 flex 容器中的 flex 项)。在电话上,并非所有单选按钮通常都适合一条线。如果任何一个单选按钮不适合,我希望它们都每行显示一个(例如带有flex-direction: column 的弹性容器中的弹性项目)。
如果我知道我的单选按钮标签将有多长,这将很容易,但我不知道(它们来自客户端可配置数据库)。
所以我想要的是这样的:
<html>
<head>
<title>Responsive Radio Button</title>
<style type="text/css">
@media (min-width: 652px) {
.flexContainer {
display:flex;
flex-direction: row;
}
}
@media (max-width: 652px) {
.flexContainer {
display:flex;
flex-direction: column;
}
}
</style>
</head>
<body>
<div class='flexContainer'>
<div>Medium Length div</div>
<div>Short div</div>
<div>Longest div in this entire html page!</div>
</div>
</div>
</body>
</html>
我事先不知道@media (min-width: /max-width:) 断点是什么。
我尝试了什么
这是我正在进行的代码
<html>
<head>
<title>Responsive Radio Button</title>
<style type="text/css">
.flexContainer {
display: flex;
flex-wrap: wrap;
justify-content:stretch;
/** border so I can see what is going on */
border: 1px dotted lightblue;
}
.flexItem {
/** white-space and min-width stop text wrapping */
white-space: nowrap;
min-width: max-content;
/** next three lines could be shortened to flex: 1 0 0px; (grow shrink basis) */
flex-basis: 0;
flex-grow: 1;
flex-shrink: 0;
/** border and margin so I can see what is going on */
border: 1px solid black;
margin: 2px;
}
</style>
</head>
<body>
<div class='flexContainer'>
<div class='flexItem'>Medium Length div</div>
<div class='flexItem'>Short div</div>
<div class='flexItem'>Short div</div>
<div class='flexItem'>Short div</div>
<div class='flexItem'>Longest div in this entire html page!</div>
</div>
</div>
</body>
</html>
弹性:1 0 0 这用于使所有弹性项目的主轴大小相等。但是当包裹开始时,主轴尺寸的相等性下降。如果弹性项目都保持相同的大小,那么它们对于父弹性容器来说都太长了,所以都会换行。 但是,这不会发生,因为每个弹性项目的最小尺寸是不同的,所以虽然弹性项目永远不会缩小,但它们都会开始增长以从不同的尺寸开始填充剩余的空间。
就像我想要一个flex-basis: largest-sibling,我的意思是最小的弹性项目开始时的大小与最大的弹性项目相同,并且永远不会小于最大的弹性项目。
【问题讨论】:
-
无法使用 flexbox 或任何其他布局方法。需要Javascript