gpt4 book ai didi

javascript - 具有右对齐第二列的 CSS Flex 换行到左侧而不是在小窗口之间 float

转载 作者:行者123 更新时间:2023-12-04 03:37:32 34 4
gpt4 key购买 nike

我有三个输入。我希望顶部与内容一样宽,而后两列与第二列右对齐(其标签与其输入框左对齐)。

我想出了这个布局,效果很好,但如果内容收缩太多而不是 float 在两者之间,我希望第二列(标签 3)与浏览器窗口的左侧对齐。

这是适合窗口的样子:

enter image description here

这是它环绕并漂浮在中间时的样子(不是我想要的):

enter image description here

这就是我希望它在包装时的样子:

enter image description here

flex box 和 CSS 能做到吗?

body {
font-family: monospace;
line-height: 200%;
max-width: 600px;
}

.align-right {
margin-left: auto;
}

.flex-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}

.flex-column {
display: flex;
flex-direction: column;
flex: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Shrink</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<div class="flex-row">
<div class="flex-column">
<label>Label 1</label>
<input/>
</div>
</div>
<div class="flex-row">
<div class="flex-column">
<span>
<label>Label 2</label>
<br />
<input/>
</span>
</div>
<div class="flex-column">
<div class="align-right">
<label>Label 3</label>
<br />
<input/>
</div>
</div>
</div>

</body>
</html>

最佳答案

所以这完全取决于您的 CSS。在 .flex-row 类中,我添加了 justify-content: space-between; 声明来隔开元素

因此不再需要 .margin-align-right 类。

然后我删除了 flex-colum-1 类并从您想要间隔开的元素中添加了 flex-colum 以便它们不会有相等的空间他们的 parent

对于要填充空间的元素,我向其添加了 .flex-column-1 类以使其占据最大宽度

body {
font-family: monospace;
line-height: 200%;
max-width: 600px;
}

.flex-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
}

.flex-column {
display: flex;
flex-direction: column;
}

.flex-column-1 {
flex: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Shrink</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<div class="flex-row">
<div class="flex-column flex-column-1">
<label>Label 1</label>
<input/>
</div>
</div>
<div class="flex-row">
<div class="flex-column">
<span>
<label>Label 2</label>
<br />
<input/>
</span>
</div>
<div class="flex-column">
<span>
<label>Label 3</label>
<br />
<input/>
</span>
</div>
</div>

</body>
</html>

关于javascript - 具有右对齐第二列的 CSS Flex 换行到左侧而不是在小窗口之间 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66620795/

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