英文:
Aligning HTML form elements when flexbox
问题
在下面的代码中,我希望"Search By"显示在第1行,下一行就是2个单选按钮,"Search Text"和文本框显示在第3行,提交按钮显示在最后一行。我使用了flexbox。目前,所有内容都显示在一行上。
<br><br>
<h2>工具</h2>
<div class="mytabs">
<input type="radio" id="tabfree" name="mytabs" checked="checked">
<label for="tabfree">Tab1</label>
<div class="tab">
<form action="/submitu">
<div class="form-group">
<p>搜索方式:</p>
<input type="radio" id="user" name="srchusr" value="UserID" checked/>
<label for="user">选项1</label>
<input type="radio" id="group" name="srchusr" value="Group" />
<label for="group">选项2</label>
<label for="srchtxt">搜索文本:</label><br>
<input type="text" id="srchtxt" name="srchtxt" value=""><br>
<input type="submit" value="提交">
</div>
</form>
</div>
<input type="radio" id="tabsilver" name="mytabs">
<label for="tabsilver">Tab2</label>
<div class="tab">
<p>Tab2数据</p>
</div>
</div>
英文:
In below code, I want "Search By" in 1 line, the 2 radio buttons in next line just below, "Search Text" & text Box in 3rd line and Submit button in last line. I've used flexbox. Currently everything is coming in 1 single line.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background: #ccc;
font-family: 'Garamond', sans-serif;
color: #000080;
}
h2 {
width: 25%;
text-align: center;
margin: auto;
padding: 15px;
}
.mytabs {
display: flex;
/*property of flex container to use flexbox*/
flex-wrap: wrap;
max-width: 1000px;
margin: 25px auto;
}
.mytabs input[type="radio"] {
display: none
}
.mytabs label {
padding: 25px;
font-weight: bold;
background: #e2e2e2;
font-size: 20px;
}
.mytabs .tab {
width: 100%;
background: #fff;
order: 1;
display: none;
}
.mytabs input[type='radio']:checked+label+.tab {
display: block;
}
.mytabs input[type="radio"]:checked+label {
background: #fff;
}
.form-group {
display: flex;
align-items: center;
justify-content: center;
margin: 40px 40px;
font-size: 20px;
}
.form-group input[type="radio"] {
display: block;
}
.form-group label {
padding: 5px;
margin: 5px;
font-weight: normal;
background: #fff;
font-size: 20px;
}
input[type=text] {
padding: 6px 8px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid navy;
border-radius: 4px;
}
<!-- language: lang-html -->
<br><br>
<h2>Tool</h2>
<div class="mytabs">
<input type="radio" id="tabfree" name="mytabs" checked="checked">
<label for="tabfree">Tab1</label>
<div class="tab">
<form action="/submitu">
<div class="form-group">
<p>Search By :</p>
<input type="radio" id="user" name="srchusr" value="UserID" checked/>
<label for="user">Option1</label>
<input type="radio" id="group" name="srchusr" value="Group" />
<label for="group">Option2</label>
<label for="srchtxt">Search Text :</label><br>
<input type="text" id="srchtxt" name="srchtxt" value=""><br>
<input type="submit" value="Submit">
</div>
</form>
</div>
<input type="radio" id="tabsilver" name="mytabs">
<label for="tabsilver">Tab2</label>
<div class="tab">
<p>Tab2 Data</p>
</div>
</div>
<!-- end snippet -->
答案1
得分: 3
使用 flex-direction: column
来使元素在垂直方向排列,而不是水平方向,并将某些元素包裹在带有 display: flex
的 div 中,以使它们在同一行内。
在下面的代码片段中,我用 "!!!" 注释突出显示了您对代码所做的更改。
.form-group {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column; /* !!! */
margin: 40px 40px;
font-size: 20px;
}
.flex-wrapper { /* !!! */
display: flex; /* !!! */
} /* !!! */
希望这对您有所帮助。
英文:
Use flex-direction:column
to have the elements vertically instead of horizontally, and wrap some elements in a div with display:flex
, so that they are in 1 line.
In the snippet below, i highlighted the changes I made to your code with "!!!" comments.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background: #ccc;
font-family: 'Garamond', sans-serif;
color: #000080;
}
h2 {
width: 25%;
text-align: center;
margin: auto;
padding: 15px;
}
.mytabs {
display: flex;
/*property of flex container to use flexbox*/
flex-wrap: wrap;
max-width: 1000px;
margin: 25px auto;
}
.mytabs input[type="radio"] {
display: none
}
.mytabs label {
padding: 25px;
font-weight: bold;
background: #e2e2e2;
font-size: 20px;
}
.mytabs .tab {
width: 100%;
background: #fff;
order: 1;
display: none;
}
.mytabs input[type='radio']:checked+label+.tab {
display: block;
}
.mytabs input[type="radio"]:checked+label {
background: #fff;
}
.form-group {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column; /* !!! */
margin: 40px 40px;
font-size: 20px;
}
.form-group input[type="radio"] {
display: block;
}
.form-group label {
padding: 5px;
margin: 5px;
font-weight: normal;
background: #fff;
font-size: 20px;
}
input[type=text] {
padding: 6px 8px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid navy;
border-radius: 4px;
}
.flex-wrapper { /* !!! */
display: flex; /* !!! */
} /* !!! */
<!-- language: lang-html -->
<br><br>
<h2>Tool</h2>
<div class="mytabs">
<input type="radio" id="tabfree" name="mytabs" checked="checked">
<label for="tabfree">Tab1</label>
<div class="tab">
<form action="/submitu">
<div class="form-group">
<p>Search By :</p>
<div class=flex-wrapper> <!-- !!! -->
<input type="radio" id="user" name="srchusr" value="UserID" checked/>
<label for="user">Option1</label>
<input type="radio" id="group" name="srchusr" value="Group" />
<label for="group">Option2</label>
</div> <!-- !!! -->
<div class=flex-wrapper> <!-- !!! -->
<label for="srchtxt">Search Text :</label><br>
<input type="text" id="srchtxt" name="srchtxt" value="">
<br>
</div> <!-- !!! -->
<input type="submit" value="Submit">
</div>
</form>
</div>
<input type="radio" id="tabsilver" name="mytabs">
<label for="tabsilver">Tab2</label>
<div class="tab">
<p>Tab2 Data</p>
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
你可以使用网格布局(grid)来构建页面或表单,网格布局非常有用。
body {
background: #ccc;
font-family: 'Garamond', sans-serif;
color: #000080;
}
h2 {
width: 25%;
text-align: center;
margin: auto;
padding: 15px;
}
.mytabs {
display: flex;
flex-wrap: wrap;
max-width: 1000px;
margin: 25px auto;
}
.mytabs input[type="radio"] {
display: none;
}
.mytabs label {
padding: 25px;
font-weight: bold;
background: #e2e2e2;
font-size: 20px;
}
.mytabs .tab {
width: 100%;
background: #fff;
order: 1;
display: none;
}
.mytabs input[type='radio']:checked+label+.tab {
display: block;
}
.mytabs input[type="radio"]:checked+label {
background: #fff;
}
.form-group {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(4, 1fr);
gap: 10px;
align-items: center;
justify-content: center;
margin: 40px 40px;
font-size: 20px;
}
.form-group>div:nth-child(even) {
text-align: right;
}
.form-group>div:nth-child(odd) {
text-align: left;
}
.form-group>div:first-child,
.form-group>div:last-child {
grid-column: span 2;
text-align: center;
}
.form-group input[type="radio"] {
display: inline-block;
}
.form-group label {
padding: 5px;
margin: 5px;
font-weight: normal;
background: #fff;
font-size: 20px;
}
input[type=text] {
padding: 6px 8px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid navy;
border-radius: 4px;
}
<br><br>
<h2>Tool</h2>
<div class="mytabs">
<input type="radio" id="tabfree" name="mytabs" checked="checked">
<label for="tabfree">Tab1</label>
<div class="tab">
<form action="/submitu">
<div class="form-group">
<div>Search By :</div>
<div>
<input type="radio" id="user" name="srchusr" value="UserID" checked/>
<label for="user">Option1</label>
</div>
<div>
<input type="radio" id="group" name="srchusr" value="Group" />
<label for="group">Option2</label>
</div>
<div>
<label for="srchtxt">Search Text :</label>
</div>
<div>
<input type="text" id="srchtxt" name="srchtxt" value="">
</div>
<div>
<input type="submit" value="Submit">
</div>
</div>
</form>
</div>
<input type="radio" id="tabsilver" name="mytabs">
<label for="tabsilver">Tab2</label>
<div class="tab">
<p>Tab2 Data</p>
</div>
</div>
英文:
You can use grid. To structure a page, or a form, grid is really useful.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background: #ccc;
font-family: 'Garamond', sans-serif;
color: #000080;
}
h2 {
width: 25%;
text-align: center;
margin: auto;
padding: 15px;
}
.mytabs {
display: flex;
/*property of flex container to use flexbox*/
flex-wrap: wrap;
max-width: 1000px;
margin: 25px auto;
}
.mytabs input[type="radio"] {
display: none
}
.mytabs label {
padding: 25px;
font-weight: bold;
background: #e2e2e2;
font-size: 20px;
}
.mytabs .tab {
width: 100%;
background: #fff;
order: 1;
display: none;
}
.mytabs input[type='radio']:checked+label+.tab {
display: block;
}
.mytabs input[type="radio"]:checked+label {
background: #fff;
}
.form-group {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(4, 1fr);
gap: 10px;
align-items: center;
justify-content: center;
margin: 40px 40px;
font-size: 20px;
}
.form-group>div:nth-child(even) {
text-align: right;
}
.form-group>div:nth-child(odd) {
text-align: left;
}
.form-group>div:first-child,
.form-group>div:last-child {
grid-column: span 2;
text-align: center;
}
.form-group input[type="radio"] {
display: inline-block;
}
.form-group label {
padding: 5px;
margin: 5px;
font-weight: normal;
background: #fff;
font-size: 20px;
}
input[type=text] {
padding: 6px 8px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid navy;
border-radius: 4px;
}
<!-- language: lang-html -->
<br><br>
<h2>Tool</h2>
<div class="mytabs">
<input type="radio" id="tabfree" name="mytabs" checked="checked">
<label for="tabfree">Tab1</label>
<div class="tab">
<form action="/submitu">
<div class="form-group">
<div>Search By :</div>
<div>
<input type="radio" id="user" name="srchusr" value="UserID" checked/>
<label for="user">Option1</label>
</div>
<div>
<input type="radio" id="group" name="srchusr" value="Group" />
<label for="group">Option2</label>
</div>
<div>
<label for="srchtxt">Search Text :</label>
</div>
<div>
<input type="text" id="srchtxt" name="srchtxt" value="">
</div>
<div>
<input type="submit" value="Submit">
</div>
</div>
</form>
</div>
<input type="radio" id="tabsilver" name="mytabs">
<label for="tabsilver">Tab2</label>
<div class="tab">
<p>Tab2 Data</p>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论