将输入放置在标签下

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

Putting inputs under labels

问题

我已经努力尝试了几天,但我无法使其工作,我该如何实现这个?我弄不清楚,如何将两个标签(名字 姓氏)放在同一行上,并在它们下面放置相应的输入?这是我想做的草图

我知道对你们来说这是一件容易的事,但我卡在这里,我可以做到,但它们最终会呈现在不同的行上,或者我只能完成第一个,但第二个标签会消失。

英文:

I've been trying for days but I couldn't make this work, how do I achieve this? I can't figure it out, how do I put two labels (fname lname) on the same line and under them the corresponding inputs?here's a sketch of what i wanna do

I know it's an easy task for you all but I'm stuck on it, I can do it but they end up on separate lines or I only get the first one done but the label on the second one disappears

答案1

得分: 1

这里是翻译好的部分:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    label {
      display: inline-block;
    }

    input {
      display: block
    }

<!-- language: lang-html -->

    <label>
      名字
      <input/>
    </label>
    <label>
      姓氏
      <input/>
    </label>

<!-- end snippet -->

请注意,HTML中的"First Name"和"Last Name"已被翻译为"名字"和"姓氏"。

英文:

Here you go, no need for flex/grid:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

label {
  display: inline-block;
}

input {
  display: block
}

<!-- language: lang-html -->

&lt;label&gt;
  First Name
  &lt;input/&gt;
&lt;/label&gt;
&lt;label&gt;
  Last Name
  &lt;input/&gt;
&lt;/label&gt;

<!-- end snippet -->

答案2

得分: 1

如评论者所指出,CSS Grid 将是您在这种设置中首选的方法。以下是一个可行的示例

以下是您的HTML

<div class="container">
    <div><label for="fname">First Name</label></div>
    <div><label for="lname">Last Name</label></div>
    <div><input type="text" name="fname"></div>
    <div><input type="text" name="lname"></div>
</div>

然后是设置网格的CSS:

.container {
  display: grid; 
  grid-auto-rows: 1fr; 
  grid-template-columns: 1fr 1fr; 
  grid-template-rows: 1fr 1fr;
}

那么这是做什么的呢?CSS 中有不同类型的 display 值。许多 HTML 元素具有默认值,例如 div 的显示为 block,而我们的 input 元素具有 inline-block。这告诉浏览器如何布局该元素。

因此,在我们的情况下,我们首先要设置它以利用一个相对较新的布局范例,称为 grid。CSS Grid 强大之处在于它允许您在两个维度上定义布局。CSS Grid 项目是其直接子代,这就是为什么我为每个项目添加了看似毫无意义的 div 作为容器元素的原因。

让我们深入了解每一行都在做什么。

display: grid - 这告诉浏览器在布局该元素时使用 Grid

grid-template-columns: 1fr 1fr; - 这定义了我们希望网格有多少列以及如何调整它们的大小。我们将使用灵活长度单位(fr)来调整两列的大小。

grid-template-rows: 1fr 1fr; - 与行一样,这定义了我们希望网格有多少行以及如何调整它们的大小。

grid-auto-rows: 1fr; - Grid 强大,即使我们已经定义了我们将有一个 2x2 的网格,但也存在未经作者了解的新内容可能被插入的情况。如果没有可以放置未经考虑的项目的单元格,CSS Grid 将创建新的行以将内容插入其中,这些行称为隐式行和列。在这里,我们正在定义如果有超过两行内容,我们希望这些行有多大。

CSS Grid 非常强大,我强烈推荐查看 Rachel Andrew 的“Grid by Example”,并从第一个开始点击,直到最后,您将对如何使用 CSS Grid 有相当深入的了解。

玩得开心!

英文:

As the commenter noted, CSS Grid is going to be your preferred approach for this type of setup. Here is a working example.

Here is your HTML

&lt;div class=&quot;container&quot;&gt;
    &lt;div&gt;&lt;label for=&quot;fname&quot;&gt;First Name&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;label for=&quot;lname&quot;&gt;Last Name&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type=&quot;text&quot; name=&quot;fname&quot;&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type=&quot;text&quot; name=&quot;lname&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

Then the CSS to setup your grid:

.container {
  display: grid; 
  grid-auto-rows: 1fr; 
  grid-template-columns: 1fr 1fr; 
  grid-template-rows: 1fr 1fr;
}

So what is this doing? There are different types of display values within CSS. Many HTML elements have default values, such as a div has a display of block whereas our input elements have an inline-block. This tells the browser how to do layout of that element.

So in our case, we're first going to set it to leverage a relatively new layout paradigm called grid. CSS Grid is powerful because it allows you to define layouts in two dimensions. CSS Grid items are its direct descendants which is why I added seemingly pointless divs as container elements for each item.

So let's dig into what each line is doing.

display: grid - This tells the browser to use Grid when laying out that element

grid-template-columns: 1fr 1fr; - This is defining how many columns we want our grid to have and how to size them. We're going to use the flexible length unit (fr) in order to size both columns.

grid-template-rows: 1fr 1fr; - Like rows, this is defining how many columns we want our grid to have and how to size them.

grid-auto-rows: 1fr; - Grid is powerful, and even though we've defined that we'll have a 2x2 grid there are scenarios where new content may be inserted that the CSS author wasn't aware of. If there isn't a cell that the un-accounted for item can be placed into then CSS Grid will create a new row to insert the content into, these are known as implicit rows and columns. Here we're defining how large we want those rows to be if there ends up being more than two rows of content.

CSS Grid is very powerful and I highly recommend Rachel Andrew's "Grid by Example" and just clicking from the first one until the end and you'll have a rather strong grasp on how to use CSS Grid.

Have fun!

huangapple
  • 本文由 发表于 2023年6月25日 21:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550577.html
匿名

发表评论

匿名网友

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

确定