gpt4 book ai didi

html - 使所有 flex-items 环绕(如 flex-direction : column) when any one flex-item too wide for flex-container

转载 作者:行者123 更新时间:2023-12-03 20:56:56 28 4
gpt4 key购买 nike

其用例是带有单选按钮的响应式表单。当在大 PC 屏幕上时,所有单选按钮通常都在屏幕上的一行中(就像带有 flex-direction: row 的非包装 flex 容器中的 flex 元素)。在电话上,并非所有单选按钮通常都适合在一条线上。如果任何一个单选按钮不适合,我希望它们每行显示一个(就像 flex 容器中的 flex 元素 flex-direction: column )。
如果我知道单选按钮标签的长度,这将很容易,但我不知道(它们来自客户端可配置的数据库)。
enter image description here
所以我想要的是这样的:

<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>
flex :1 0 0
这用于使所有 flex 元素的主轴大小相等。但是当包装开始时,主轴尺寸的相等性下降。如果 flex-items 都保持相同的大小,那么它们对于父 flex-container 来说都太长了,所以都会换行。
但是,这不会发生,因为每个 flex-item 的最小尺寸是不同的,所以尽管 flex 元素永远不会缩小,但它们都开始增长以从不同的尺寸开始填充剩余空间。
就像我想要一个 flex-basis: largest-sibling ,我的意思是最小的 flex-item 与最大的 flex-item 大小相同,并且永远不会小于最大的 flex-item。

最佳答案

这是我从你的编码中得到的。看看对你有没有帮助

<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;
}
@media only screen and (max-width: 600px) {
.flexContainer {
flex-flow: column;
}
}

</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>

关于html - 使所有 flex-items 环绕(如 flex-direction : column) when any one flex-item too wide for flex-container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com