在使用 flexbox 对齐 HTML 表单元素时

huangapple go评论66阅读模式
英文:

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: &#39;Garamond&#39;, 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=&quot;radio&quot;] {
  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=&#39;radio&#39;]:checked+label+.tab {
  display: block;
}

.mytabs input[type=&quot;radio&quot;]:checked+label {
  background: #fff;
}

.form-group {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 40px 40px;
  font-size: 20px;
}

.form-group input[type=&quot;radio&quot;] {
  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 -->

&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Tool&lt;/h2&gt;
&lt;div class=&quot;mytabs&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;tabfree&quot; name=&quot;mytabs&quot; checked=&quot;checked&quot;&gt;
  &lt;label for=&quot;tabfree&quot;&gt;Tab1&lt;/label&gt;
  &lt;div class=&quot;tab&quot;&gt;
    &lt;form action=&quot;/submitu&quot;&gt;
      &lt;div class=&quot;form-group&quot;&gt;
        &lt;p&gt;Search By :&lt;/p&gt;
        &lt;input type=&quot;radio&quot; id=&quot;user&quot; name=&quot;srchusr&quot; value=&quot;UserID&quot; checked/&gt;
        &lt;label for=&quot;user&quot;&gt;Option1&lt;/label&gt;
        &lt;input type=&quot;radio&quot; id=&quot;group&quot; name=&quot;srchusr&quot; value=&quot;Group&quot; /&gt;
        &lt;label for=&quot;group&quot;&gt;Option2&lt;/label&gt;
        &lt;label for=&quot;srchtxt&quot;&gt;Search Text :&lt;/label&gt;&lt;br&gt;
        &lt;input type=&quot;text&quot; id=&quot;srchtxt&quot; name=&quot;srchtxt&quot; value=&quot;&quot;&gt;&lt;br&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
      &lt;/div&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  &lt;input type=&quot;radio&quot; id=&quot;tabsilver&quot; name=&quot;mytabs&quot;&gt;
  &lt;label for=&quot;tabsilver&quot;&gt;Tab2&lt;/label&gt;
  &lt;div class=&quot;tab&quot;&gt;
    &lt;p&gt;Tab2 Data&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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: &#39;Garamond&#39;, 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=&quot;radio&quot;] {
  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=&#39;radio&#39;]:checked+label+.tab {
  display: block;
}

.mytabs input[type=&quot;radio&quot;]: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=&quot;radio&quot;] {
  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 -->

&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Tool&lt;/h2&gt;
&lt;div class=&quot;mytabs&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;tabfree&quot; name=&quot;mytabs&quot; checked=&quot;checked&quot;&gt;
  &lt;label for=&quot;tabfree&quot;&gt;Tab1&lt;/label&gt;
  &lt;div class=&quot;tab&quot;&gt;
    &lt;form action=&quot;/submitu&quot;&gt;
      &lt;div class=&quot;form-group&quot;&gt;
        &lt;p&gt;Search By :&lt;/p&gt;
        &lt;div class=flex-wrapper&gt; &lt;!-- !!! --&gt;
          &lt;input type=&quot;radio&quot; id=&quot;user&quot; name=&quot;srchusr&quot; value=&quot;UserID&quot; checked/&gt;
          &lt;label for=&quot;user&quot;&gt;Option1&lt;/label&gt;
          &lt;input type=&quot;radio&quot; id=&quot;group&quot; name=&quot;srchusr&quot; value=&quot;Group&quot; /&gt;
          &lt;label for=&quot;group&quot;&gt;Option2&lt;/label&gt;
        &lt;/div&gt; &lt;!-- !!! --&gt;
        &lt;div class=flex-wrapper&gt; &lt;!-- !!! --&gt;
          &lt;label for=&quot;srchtxt&quot;&gt;Search Text :&lt;/label&gt;&lt;br&gt;
          &lt;input type=&quot;text&quot; id=&quot;srchtxt&quot; name=&quot;srchtxt&quot; value=&quot;&quot;&gt;
          &lt;br&gt;
        &lt;/div&gt; &lt;!-- !!! --&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
      &lt;/div&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  &lt;input type=&quot;radio&quot; id=&quot;tabsilver&quot; name=&quot;mytabs&quot;&gt;
  &lt;label for=&quot;tabsilver&quot;&gt;Tab2&lt;/label&gt;
  &lt;div class=&quot;tab&quot;&gt;
    &lt;p&gt;Tab2 Data&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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: &#39;Garamond&#39;, 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=&quot;radio&quot;] {
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=&#39;radio&#39;]:checked+label+.tab {
display: block;
}
.mytabs input[type=&quot;radio&quot;]: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&gt;div:nth-child(even) {
text-align: right;
}
.form-group&gt;div:nth-child(odd) {
text-align: left;
}
.form-group&gt;div:first-child,
.form-group&gt;div:last-child {
grid-column: span 2;
text-align: center;
}
.form-group input[type=&quot;radio&quot;] {
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 -->

&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Tool&lt;/h2&gt;
&lt;div class=&quot;mytabs&quot;&gt;
&lt;input type=&quot;radio&quot; id=&quot;tabfree&quot; name=&quot;mytabs&quot; checked=&quot;checked&quot;&gt;
&lt;label for=&quot;tabfree&quot;&gt;Tab1&lt;/label&gt;
&lt;div class=&quot;tab&quot;&gt;
&lt;form action=&quot;/submitu&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div&gt;Search By :&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;radio&quot; id=&quot;user&quot; name=&quot;srchusr&quot; value=&quot;UserID&quot; checked/&gt;
&lt;label for=&quot;user&quot;&gt;Option1&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;radio&quot; id=&quot;group&quot; name=&quot;srchusr&quot; value=&quot;Group&quot; /&gt;
&lt;label for=&quot;group&quot;&gt;Option2&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;label for=&quot;srchtxt&quot;&gt;Search Text :&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;text&quot; id=&quot;srchtxt&quot; name=&quot;srchtxt&quot; value=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;input type=&quot;radio&quot; id=&quot;tabsilver&quot; name=&quot;mytabs&quot;&gt;
&lt;label for=&quot;tabsilver&quot;&gt;Tab2&lt;/label&gt;
&lt;div class=&quot;tab&quot;&gt;
&lt;p&gt;Tab2 Data&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 00:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定