在 CSS 中对齐表单元素

2024-01-18

我是 CSS 新手,并且有一个简单的登录表单,我正在尝试正确对齐。基本上是两列,带有标签和Login按钮位于一列中,文本框位于另一列中。我如何在 CSS 中做到这一点?

HTML 代码是:

<body>
  <form action="" method="post">
    <label> Username </label>
    <input type="text"/>

    <label> Password </label>
    <input type="password"/>

    <input type="submit" value="submit"/>
  </form>
</body>

这是一种有效的方法:

form {
  width: 80%;
  margin: 0 auto;
}

label,
input {
  /* In order to define widths */
  display: inline-block;
}

label {
  width: 30%;
  /* Positions the label text beside the input */
  text-align: right;
}

label+input {
  width: 30%;
  /* Large margin-right to force the next element to the new-line
           and margin-left to create a gutter between the label and input */
  margin: 0 30% 0 4%;
}


/* Only the submit button is matched by this selector,
       but to be sure you could use an id or class for that button */

input+input {
  float: right;
}
<form action="#" method="post">
  <!-- note that I've added a 'for' attribute to
       both of the <label> elements, which is
       equal to the 'id' attribute of the relevant
       <input> element; this means that clicking
       the <label> will focus the <input>: -->
  <label for="username">Username</label>
  <input id="username" type="text" />

  <label for="password">Password</label>
  <input id="password" type="password" />

  <input type="submit" value="submit" />
</form>

JS 小提琴演示 http://jsfiddle.net/davidThomas/4wPPZ/.

当然,调整尺寸和边距以适合您的用例和审美。

当然,目前还有其他方法可以实现这一点,例如 CSS Grid:

*,
::before,
::after {
  /* selecting all elements on the page, along with the ::before
     and ::after pseudo-elements; resetting their margin and
     padding to zero and forcing all elements to calculate their
     box-sizing the same way, 'border-box' includes the border-widths,
     and padding, in the stated width: */
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

form {
  /* Using CSS Grid to lay out the elements in two-dimensions: */
  display: grid;
  /* specifying a 0.5em gutter/gap between adjacent elements: */
  gap: 0.5em;
  /* declaring a number of named grid areas in order to lay out
     the child elements; the areas identified with a period (.)
     are 'empty' areas, whereas the areas named by strings are
     used, later, to place elements according to those names: */
  grid-template-areas:
    "usernameLabel . usernameInput"
    "passwordLabel . passwordInput"
    ". . submit";
  /* declaring the size of each of the three columns; 1fr is
     one fractional unit of the available space, and is the
     size of the first and last of the columns. The central
     column is declared as being 0.5em in width: */
  grid-template-columns: 1fr 0.5em 1fr;
  margin: 1em auto;
  width: 80%;
}

label {
  /* placing all <label> elements in the grid column 1 (the first): */
  grid-column: 1;
  /* aligning text-content to the right in order to position the label
     text near to the relevant <input>: */
  text-align: right;
}

label::after {
  content: ':';
}

input {
  grid-column: 3;
}

button {
  /* positioning the <button> element in the grid-area identified
     by the name of 'submit': */
  grid-area: submit;
}
<form action="" method="post">
  <label for="username">Username</label>
  <input id="username" type="text" />

  <label for="password">Password</label>
  <input id="password" type="password" />

  <input type="submit" value="submit" />
</form>

JS 小提琴演示 https://jsfiddle.net/davidThomas/4wPPZ/1215/.

参考:

  • 相邻兄弟姐妹 (+) 组合器 https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator.
  • box-sizing https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing.
  • display https://developer.mozilla.org/en-US/docs/Web/CSS/display.
  • float https://developer.mozilla.org/en-US/docs/Web/CSS/float.
  • gap https://developer.mozilla.org/en-US/docs/Web/CSS/gap.
  • grid-area https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area.
  • grid-template-areas https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas.
  • grid-template-columns https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns.
  • grid-template-rows https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-rows.
  • margin https://developer.mozilla.org/en-US/docs/Web/CSS/margin.
  • width https://developer.mozilla.org/en-US/docs/Web/CSS/width.

参考书目:

  • "网格布局的基本概念 https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout."
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 CSS 中对齐表单元素 的相关文章

随机推荐