我正在尝试使用标签和输入在同一行创建一个flexbox表单。使用flex-basis和其他flex属性可以得到预期的和期望的结果。
但是,如果必须将flexbox嵌套在另一个具有设定宽度的元素中,整个事物就会崩溃。
E.g。看到附带的小提琴并使用和不带div {width:30%}
我怎样才能解决这个问题?
答案 0 :(得分:1)
您正试图让flexbox
表现为网格/表格,并且因为它不是为此而设置的,您需要通过设置一些约束来帮助它,例如min-width
,尽管如此声明你不想要它,使用适当的显示类型。
由于CSS Grid仍然没有合理的浏览器支持,因此这是第二好的CSS表
div.lesswide {
width: 30%;
margin-top: 20px;
}
form {
display: table;
width: 100%;
}
form div {
display: table-row;
}
form label {
display: table-cell;
width: 30%;
}
form input {
display: table-cell;
width: 60%;
}

<div>
<form>
<div>
<label>A</label>
<input type="text">
</div>
<div>
<label>LONG</label>
<input type="text">
</div>
<div>
<label>SOMEWHAT LONGER</label>
<input type="text">
</div>
<div>
<label>INTHEMIDDLE</label>
<input type="text">
</div>
</form>
</div>
<div class="lesswide">
<form>
<div>
<label>A</label>
<input type="text">
</div>
<div>
<label>LONG</label>
<input type="text">
</div>
<div>
<label>SOMEWHAT LONGER</label>
<input type="text">
</div>
<div>
<label>INTHEMIDDLE</label>
<input type="text">
</div>
</form>
</div>
&#13;
基于评论的更新版本
form {
display: table;
width: 100%;
}
form > div {
display: table-row;
}
form label {
display: table-cell;
width: 30%;
}
form input {
width: 100%;
box-sizing: border-box;
}
form div div:first-child {
display: table-cell;
width: 30%;
}
form div div:last-child {
display: table-cell;
width: 70%;
}
form div .side-by-side input[type=button] {
width: 30%;
}
form div .side-by-side input[type=submit] {
width: 70%;
}
@media screen and (max-width: 550px) {
form div div:first-child,
form div div:last-child,
form label {
display: block;
width: auto;
}
}
&#13;
<div>
<form>
<div>
<label>A</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>LONG</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>SOMEWHAT LONGER</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>INTHEMIDDLE</label>
<div>
<input type="text">
</div>
</div>
<div>
<div>
<input type="button" value="Button">
</div>
<div>
<input type="submit">
</div>
</div>
</form>
</div>
<br>
<div>
<form>
<div>
<label>A</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>LONG</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>SOMEWHAT LONGER</label>
<div>
<input type="text">
</div>
</div>
<div>
<label>INTHEMIDDLE</label>
<div>
<input type="text">
</div>
</div>
<div>
<div>
</div>
<div class="side-by-side">
<input type="button" value="Button"><input type="submit">
</div>
</div>
</form>
</div>
&#13;
答案 1 :(得分:0)
您可以添加width:100%;
以形成div
答案 2 :(得分:0)
尝试这种灵活的flexbox Form
@import url("https://fonts.googleapis.com/css?family=Open+Sans:300,800");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-family: "Open Sans", helvetica, sans-serif;
font-size: 24px;
color: rgba(0, 0, 0, 0.5);
}
body {
background: #f9f9f9;
}
main {
position: relative;
width: 100%;
}
h1 {
padding: 20px 0 0;
font-weight: 600;
font-size: 4rem;
}
h1,
h5 {
text-align: center;
}
h5 {
margin: 0 0 20px;
}
.ruler {
position: relative;
display: block;
left: 50%;
margin: 0 0 30px -320px;
width: 640px;
height: 6px;
line-height: -20px;
background: rgba(0, 0, 0, 0.1);
}
.ruler span {
display: block;
position: absolute;
padding: 8px;
width: 80px;
top: -22px;
left: 280px;
text-align: center;
background: #efefef;
}
textarea,
input {
display: inline-block;
font-family: "Open Sans", helvetica, sans-serif;
margin: 10px;
padding: 10px;
background: rgba(255, 255, 255, 0.8);
border: 2px solid rgba(0, 0, 0, 0.1);
border-radius: 4px;
font-size: 1rem;
font-weight: 300;
color: #454545;
}
textarea:focus,
input:focus {
outline: 0;
}
textarea {
height: 300px;
}
.flexy-parent {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
max-width: 800px;
margin: 0 auto;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-ms-flex-line-pack: start;
align-content: flex-start;
}
.flexy-item {
-webkit-box-flex: 1;
-ms-flex: 1 0 300px;
flex: 1 0 300px;
}
<main>
<h1>Flex Form</h1>
<h5>SIZE WINDOW</h5>
<div class="ruler"><span>640px</span></div>
<section class="flexy-parent">
<input class="flexy-item" type="text" placeholder="Responsive input fields...">
<input class="flexy-item" type="text" placeholder="will break at 640px...">
</section>
<section class="flexy-parent">
<input class="flexy-item" type="text" placeholder="flex-grow: 1">
<input class="flexy-item" type="text" placeholder="flex-basis: 300px">
</section>
<section class="flexy-parent">
<textarea class="flexy-item" placeholder="And thanks to flexbox, everything stays nice and tidy."></textarea>
</section>
</main>