为什么 W3C 建议将输入元素包装在

中?标签?

发布于 2024-11-27 19:32:16 字数 571 浏览 0 评论 0原文

我在网上看到了很多例子,其中表单的布局如下:

<form>
    <p><input></p>
</form>

令我惊讶的是,这也在 规范

任何表单都以表单元素开头,其中放置 控制。大多数控件由输入元素表示,该元素由 默认提供单行文本字段。要为控件添加标签,请使用标签 使用元素;标签文本和控件本身位于 标签元素。表格的每个部分都被视为一个段落,并且是 通常使用 p 元素与其他部分分开。把这个 综上所述,以下是询问客户姓名的方式:

尽管本节是非规范性的,但在我看来,这是一种不好的做法,而不是语义。我想目的是将输入放在自己的行上,但是这些元素的显示不应该使用 CSS 来控制吗?

W3C 建议以这种方式布局表单是否有原因?我错过了什么吗?

I've seen a lot of examples on the web where forms are laid out like so:

<form>
    <p><input></p>
</form>

To my surprise, this is also described in the specification:

Any form starts with a form element, inside which are placed the
controls. Most controls are represented by the input element, which by
default provides a one-line text field. To label a control, the label
element is used; the label text and the control itself go inside the
label element. Each part of a form is considered a paragraph, and is
typically separated from other parts using p elements. Putting this
together, here is how one might ask for the customer's name:

Though this section is non-normative, it still seems to me that this is bad practice and not semantic. I suppose that the purpose is to put inputs on their own line, but shouldn't the display of these elements be controlled using CSS?

Is there a reason why the W3C advises forms be laid out this way? Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

债姬 2024-12-04 19:32:16

如果您以有意义(读:语义)的方式编写表单,您将希望文本流通向该元素:

<form>
 <p><label for="firstName">Please enter your first name:</label><input id="firstName" type="text" /></p>
</form>

更好的方法是将您的表单视为 mad-libs 脚本:

<form>
  <p>Hello. My <label for="firstName">name</label> is <input id="firstName" type="text" />...</p>
</form>

p 元素并不是标记表单的唯一方法。如果要添加/编辑数据矩阵,则使用表在语义上是有效的。

在其他情况下,您可能不想使用包装元素。这取决于您想要提供的内容。整理好内容后,再担心如何设计所有内容。

If you are writing a form in a meaningful (read: semantic) way, you will want the flow of text to lead to the element:

<form>
 <p><label for="firstName">Please enter your first name:</label><input id="firstName" type="text" /></p>
</form>

An even better way is to treat your form like a mad-libs script:

<form>
  <p>Hello. My <label for="firstName">name</label> is <input id="firstName" type="text" />...</p>
</form>

A p element isn't the only way to mark-up a form. If a matrix of data is being added/edited, it's semantically valid to use a table.

In other cases, you might not want to use a wrapping element. It depends on what content you want to be serving up. Worry about styling it all after you've got your content sorted out.

尘世孤行 2024-12-04 19:32:16

INPUT 元素是内联的,因此将它们包装在某种块元素中是有意义的,以便它们之间有自然的分离。由于 DIV 默认情况下没有边距,因此使用段落是有意义的。

INPUT elements are inline, and therefore it makes sense to wrap them in some sort of block element, so that there is a natural separation between them. Since the DIV has no margins by default, it makes sense to use a paragraph.

燕归巢 2024-12-04 19:32:16

领先的 CSS、HTML 和网格框架均未使用 P 标签。

请参阅此处:Bootstrap基金会

恕我直言,使用 fieldset 和 div。

虽然 W3C 建议使用段落标签

,但遵循他们的建议几乎没有任何意义。原因是:你限制了自己。也就是说:如果您使用 p 标签编写组件,您将无法在其中放置 div。

那么,为什么要将 div 放在 p 标记内呢?好吧...为什么不呢?例如,您想以某种方式设置内容的样式,或添加一些信息。使用 p 标签,您现在陷入困境。对我来说这似乎也是个糟糕的建议。

答案:

整理好内容后,再担心样式问题

它有点假设你知道从现在起一年后你会需要什么。

我的建议:你不想这样:

“我希望我没有使用限制性 P 标签”

None of the leading CSS, HTML, and grid frameworks is using P tags.

See here: Bootstrap or Foundation.

IMHO, use fieldset and divs.

While W3C recommend using paragraph tags <p>, it makes little to no sense to follow their advice. The reason is: you are restricting yourself. That is: if you write your components using p tags you won't be able to put a div inside it. Putting <div> inside <p> is adding an extra <p>

well, why would you want to put a div inside a p tag? well... why not? for example you want to style your content in a way, or add some information. Using p tags you are stuck now. It seems to be bad advice as well to me.

The answer:

worry about styling once you got your content sorted out

It kind of assumes that you know what you will need in, say, one year from now.

My advice: you don't want to be like:

"I wish I hadn't used the restrictive P tags"

橘亓 2024-12-04 19:32:16

这适用于 HTML 4,但可能不适用于所请求的 HTML 5。

参考:17.3 FORM 元素

form 需要一个块级元素作为子元素。 input 是一个内联元素。 p 就可以解决这个问题。

This goes for HTML 4, but maybe not for the requested HTML 5.

Ref.: 17.3 The FORM element

form needs a block-level element as child. input is an inline element. The p does the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文