具有多列的表单,无表格

发布于 2024-07-17 06:22:13 字数 27 浏览 14 评论 0原文

如何在屏幕上定位具有多个字段的复杂表单?

How to position a complex form with multiple fields in line across the screen?

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

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

发布评论

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

评论(11

剧终人散尽 2024-07-24 06:22:13

为什么人们如此热衷于避开餐桌?

表格并未被弃用,在显示逻辑上属于表格的内容时应使用表格。

如果您的表单按逻辑分组以便表格直观,请使用表格。

始终思考:“实现这一结果的最干净、最简单、最可维护的方法是什么。”

如果您想要具有可变数量列的流体形式,请忽略这一点。

Why are people so hell-bent on avoiding tables?

Tables are not deprecated and should be used when displaying content which logically belongs in a table.

If your form is logically grouped such that a table would be intuitive, please use a table.

Always be thinking: "What's the cleanest, simplest, most maintainable way to achieve this result."

If you want a fluid form with a variable number columns, then disregard this.

奢欲 2024-07-24 06:22:13

喜欢稍微语义化的方式,使用 定义列表

<dl class="form">
    <dt><label for="input1">One:</label></dt>
    <dd><input type="text" name="input1" id="input1"></dd>
    <dt><label for="input2">Two:</label></dt>
    <dd><input type="text" name="input2" id="input2"></dd>
</dl>

我更 你的 CSS:

dl.form {
    width:100%;
    float:left;
    clear:both;
}
dl.form dt {
    width:50%;
    float:left;
    clear:left;
    text-align:right;
}
dl.form dd {
    width:50%;
    float:left;
    clear:right;
    text-align:left;
}

这应该会生成一个位于页面中心的表单,标签位于左列,输入位于右列

I prefer the slightly-more-semantic way, using a definition list:

<dl class="form">
    <dt><label for="input1">One:</label></dt>
    <dd><input type="text" name="input1" id="input1"></dd>
    <dt><label for="input2">Two:</label></dt>
    <dd><input type="text" name="input2" id="input2"></dd>
</dl>

Then your CSS:

dl.form {
    width:100%;
    float:left;
    clear:both;
}
dl.form dt {
    width:50%;
    float:left;
    clear:left;
    text-align:right;
}
dl.form dd {
    width:50%;
    float:left;
    clear:right;
    text-align:left;
}

This should produce a form centered in the page, with the labels in the left column and the inputs in the right

幻想少年梦 2024-07-24 06:22:13

有许多不同的方法可以做到这一点。 这都是一个偏好问题。 我通常做的是有一个包含所有行的包装器 div,然后每行有一个包含标签、输入和验证器的 div 块。 您可以使用 line-height CSS 属性来帮助您进行垂直对齐。 示例:

<div class="formWrapper">
<form>
   <div class="formItem">
      <label for="firstName">First Name:</label>
      <input name="firstName" id="firstName" class="required" type="text" />
      <span class="validator" style="display: none;">*</>
   </div>
   ... <!-- Rinse repeat -->
</form>
</div>

<style type="text/css">
   .formWrapper { width: 400px }
   .formWrapper .formItem { line-height: 35px; height: 35px; }
   .formWrapper label { width: 50px; }
   .formWrapper input { width: 100px; border: 1px solid #000; }
   .formWrapper .validator { padding-left: 10px; color: #FF0000; }
</style>

希望有帮助。

There are many different ways to do this. It's all a matter of preference. What I typically do is have a wrapper div that contains all of the rows, and then a div block per row that contains the label, input, and validator. You can use the line-height CSS property to help you with vertical alignment. Example:

<div class="formWrapper">
<form>
   <div class="formItem">
      <label for="firstName">First Name:</label>
      <input name="firstName" id="firstName" class="required" type="text" />
      <span class="validator" style="display: none;">*</>
   </div>
   ... <!-- Rinse repeat -->
</form>
</div>

<style type="text/css">
   .formWrapper { width: 400px }
   .formWrapper .formItem { line-height: 35px; height: 35px; }
   .formWrapper label { width: 50px; }
   .formWrapper input { width: 100px; border: 1px solid #000; }
   .formWrapper .validator { padding-left: 10px; color: #FF0000; }
</style>

Hope that helps.

心房敞 2024-07-24 06:22:13

在查看了许多不同的解决方案之后,我发现此页面上的示例(特别是“致命”中的示例?)是一些最有帮助的。 但广泛的

标签确实让我有点困扰。 所以这里有一些可能会喜欢的修改。 另外,您会发现某种“包装器”或“字段集”样式对于防止浮动影响其他 HTML 非常必要。 请参阅上面的示例。

<style>
.formcol{
float: left;
padding: 2px;
} 
.formcol label {
font-weight: bold;
display:block;} 
</style>

<div class="formcol">
    <label for="org">organization</label>
    <input type="text" id="org" size="24" name="org" />
  </div>
 <div class="formcol">
    <label for="fax">fax</label>
    <input type="text" id="fax" name="fax" size="2" />
  </div>
  <div class="formcol">
    <label for="3">three</label>
    <input type="text" id="3" name="3" />
    <label for="4">four</label>
    <input type="text" id="4" name="4" />

    <label for="5">five</label>
    <input type="text" id="5" name="5" />
  </div>
 <div class="formcol">
    <label for="6">six</label>
    <input type="text" id="6" name="6" />
  </div>

After looking at many many different solutions, I found the examples on this page (particularly the one from 'Fatal'?) some of the most helpful. But the extensive

and tags did bother me a bit. So here is a little bit of a modification that some may like. Also, you find some sort of 'wrapper' or 'fieldset' style very necessary to keep the float from affecting other HTML. Refer to examples above.

<style>
.formcol{
float: left;
padding: 2px;
} 
.formcol label {
font-weight: bold;
display:block;} 
</style>

<div class="formcol">
    <label for="org">organization</label>
    <input type="text" id="org" size="24" name="org" />
  </div>
 <div class="formcol">
    <label for="fax">fax</label>
    <input type="text" id="fax" name="fax" size="2" />
  </div>
  <div class="formcol">
    <label for="3">three</label>
    <input type="text" id="3" name="3" />
    <label for="4">four</label>
    <input type="text" id="4" name="4" />

    <label for="5">five</label>
    <input type="text" id="5" name="5" />
  </div>
 <div class="formcol">
    <label for="6">six</label>
    <input type="text" id="6" name="6" />
  </div>

享受孤独 2024-07-24 06:22:13

这可以使用 CSS 通过将“display”属性设置为“inline”来完成(因为默认情况下,表单元素是块级元素)。

That would be done using CSS by setting the "display" property to "inline" (since form elements are, by default, block level elements).

霞映澄塘 2024-07-24 06:22:13

搜索“没有表格的布局”。 许多网站都描述了使用 CSS 进行格式化。 这是一个简单的介绍: http://www.htmlgoodies.com/beyond/ css/article.php/3642151

Do a search for "layouts without tables". Many sites describe formatting with CSS. Here is a simple intro: http://www.htmlgoodies.com/beyond/css/article.php/3642151

陌生 2024-07-24 06:22:13

我建议您blueprint CSS 框架。 快速浏览一下演示页面

I suggest you blueprint CSS framework. Have a quick look at the demo page.

清引 2024-07-24 06:22:13

当我需要设计相当复杂的表单时,这就是我通常使用的方法。

HTML:

<fieldset>
<legend>Consent group</legend>
<form>
<fieldset class="nolegend">
<p><label><span>Title</span> <input type="text" name="title" size="40" value="" /></label></p>
<p><label><span>Short name</span> <input type="text" name="sname" size="20" value="" /></label></p>
<p><label><br /><input type="checkbox" name="approval"> This consent group requires approval</label></p>
</fieldset>
<fieldset class="nolegend">
<p><label><span>Data use limitations</span> <textarea name="dul" cols="64" rows="4"></textarea></label></p>
</fieldset>
<input type="submit" value="Submit" />
</form>
</fieldset>

CSS:

body, input, textarea, select {
font: 1em Arial, Helvetica, sans-serif;
}
input, textarea, select { font-size: .8em }
fieldset,
fieldset legend {
background-color: #EEE;
}
fieldset {
border: none;
margin: 0;
padding: 0 0 .5em .01em;
top: 1.25em;
position: relative;
margin-bottom: 2em;
}
fieldset fieldset {
margin: 0 0 1em 0;
}
fieldset legend {
padding: .25em .5em 0 .5em;
border-bottom: none;
font-weight: bold;
margin-top: -1.25em;
position: relative;
*left: -.5em;
color: #666;
}fieldset form,
fieldset .fieldset {
margin: 0;
padding: 1em .5em 0 .5em;
overflow: hidden;
}
fieldset.nolegend {
position: static;
margin-bottom: 1em;
background-color: transparent;
padding: 0;
overflow: hidden;
}
fieldset.nolegend p,
fieldset.nolegend div {
float: left;
margin: 0 1em 0 0;
}
fieldset.nolegend p:last-child,
fieldset.nolegend div:last-child {
margin-right: 0;
}
fieldset.nolegend label>span {
display: block;
}
fieldset.nolegend label span {
_display: block;
}

我通过 Safari hacks 省略了几行 CSS。 您可以查看此代码的实时版本

This is what I usually use when I need to design pretty complex forms.

HTML:

<fieldset>
<legend>Consent group</legend>
<form>
<fieldset class="nolegend">
<p><label><span>Title</span> <input type="text" name="title" size="40" value="" /></label></p>
<p><label><span>Short name</span> <input type="text" name="sname" size="20" value="" /></label></p>
<p><label><br /><input type="checkbox" name="approval"> This consent group requires approval</label></p>
</fieldset>
<fieldset class="nolegend">
<p><label><span>Data use limitations</span> <textarea name="dul" cols="64" rows="4"></textarea></label></p>
</fieldset>
<input type="submit" value="Submit" />
</form>
</fieldset>

CSS:

body, input, textarea, select {
font: 1em Arial, Helvetica, sans-serif;
}
input, textarea, select { font-size: .8em }
fieldset,
fieldset legend {
background-color: #EEE;
}
fieldset {
border: none;
margin: 0;
padding: 0 0 .5em .01em;
top: 1.25em;
position: relative;
margin-bottom: 2em;
}
fieldset fieldset {
margin: 0 0 1em 0;
}
fieldset legend {
padding: .25em .5em 0 .5em;
border-bottom: none;
font-weight: bold;
margin-top: -1.25em;
position: relative;
*left: -.5em;
color: #666;
}fieldset form,
fieldset .fieldset {
margin: 0;
padding: 1em .5em 0 .5em;
overflow: hidden;
}
fieldset.nolegend {
position: static;
margin-bottom: 1em;
background-color: transparent;
padding: 0;
overflow: hidden;
}
fieldset.nolegend p,
fieldset.nolegend div {
float: left;
margin: 0 1em 0 0;
}
fieldset.nolegend p:last-child,
fieldset.nolegend div:last-child {
margin-right: 0;
}
fieldset.nolegend label>span {
display: block;
}
fieldset.nolegend label span {
_display: block;
}

I omitted couple lines of CSS with Safari hacks. You can check out live version of this code.

久夏青 2024-07-24 06:22:13

佩斯·凯尔·法里斯 (Pace KyleFarris),但我不得不给 Ben S 投一票,因为他有勇气提到桌子。 只需查看本页和互联网上的各种 CSS 解决方案即可解决一个极其简单的问题。 CSS 有一天可能会成为一种很好的解决方案,但目前复制 table 标签提供的简单行和列网格是极其复杂的。 我带着对表格之类的东西的偏见度过了无数毫无结果的时间。 我们为什么要这样对自己?

Pace KyleFarris but I just had to give Ben S a vote for having the guts to mention tables. Just look at the variety of CSS solutions on this page and around the internet for a ridiculously simple problem. CSS may one day become a good solution, but for the time being replicating the simple row and column grid that the table tag provides is extremely complex. I have spent countless fruitless hours with this prejudice against tables for things like a form. Why do we do this to ourselves?

掐死时间 2024-07-24 06:22:13

默认情况下,输入字段是内联的。 因此,您可以简单地使用将它们排列起来,而无需
如果您希望它们正确排列,另一种选择如下:

<div id="col1" style="float: left;>
    <input type="text" name="field1" />
    <br />
    <input type="text" name="field3" />
</div>

<div id="col2" style="float: left;>
    <input type="text" name="field2" />
    <br />
    <input type="text" name="field4" />
</div>

input fields, by default, are inline. Therefore, you can simply use line them up without
Another option if you want them lined up correctly is as follows:

<div id="col1" style="float: left;>
    <input type="text" name="field1" />
    <br />
    <input type="text" name="field3" />
</div>

<div id="col2" style="float: left;>
    <input type="text" name="field2" />
    <br />
    <input type="text" name="field4" />
</div>
提赋 2024-07-24 06:22:13

我更喜欢使用 fieldset 对所有元素进行分组,并使用 p 对每个表单字段进行分组。

<html>
<head>
<style type="text/css">
    fieldset {
        width: 500px;
        background-color: lightblue;
    }

    fieldset legend {
        font-weight: bold;
    }

    fieldset p {
        clear:both;
        padding: 5px;
    }

    fieldset label {
        text-align: left;
        width: 100px;
        float: left;
        font-weight: bold;
    }

    fieldset .Validator {
        color: red !important;
        font-weight: bold;
    } 

</style>
<head>
<body>
    <form>
        <fieldset>
            <legend>Data</legend>
            <p>
                <label for="firstName">First Name:</label>
                <input name="firstName" id="firstName" class="required" type="text" />
                <span class="Validator" style="display: none;">*</span>
            </p>
            <p>
                <label for="lastName">Last Name:</label>
                <input name="lastName" id="lastName" class="required" type="text" />
                <span class="Validator">*</span>
            </p>
        </fieldset>
    </form>
</body>
</html>

I prefer to use fieldset to group all elements and p for each form field.

<html>
<head>
<style type="text/css">
    fieldset {
        width: 500px;
        background-color: lightblue;
    }

    fieldset legend {
        font-weight: bold;
    }

    fieldset p {
        clear:both;
        padding: 5px;
    }

    fieldset label {
        text-align: left;
        width: 100px;
        float: left;
        font-weight: bold;
    }

    fieldset .Validator {
        color: red !important;
        font-weight: bold;
    } 

</style>
<head>
<body>
    <form>
        <fieldset>
            <legend>Data</legend>
            <p>
                <label for="firstName">First Name:</label>
                <input name="firstName" id="firstName" class="required" type="text" />
                <span class="Validator" style="display: none;">*</span>
            </p>
            <p>
                <label for="lastName">Last Name:</label>
                <input name="lastName" id="lastName" class="required" type="text" />
                <span class="Validator">*</span>
            </p>
        </fieldset>
    </form>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文