使用 CSS 的 HTML 表单布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/736336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:30:27  来源:igfitidea点击:

HTML form layout with CSS

cssformsxhtml

提问by user80603

I need to build a form for data input, let's say FirstName and LastName. I know how to do this with a table. In the first <td>I'd put a label tag and in the second I'd use an input tag with a type="text"attribute. This way the labels and textboxes would be lined up in two columns.

我需要为数据输入构建一个表单,比如说名字和姓氏。我知道如何用桌子来做到这一点。在第一个中<td>我会放置一个标签标签,在第二个中我会使用一个带有type="text"属性的输入标签。这样标签和文本框将排成两列。

Is there a way to do this with CSS?

有没有办法用 CSS 做到这一点?

回答by rp.

You do NOT need tables to make great HTML forms. In fact, you don't want them! Try this code at home and see what you think..

您不需要表格来制作出色的 HTML 表单。事实上,你不想要它们!在家里试试这个代码,看看你的想法..

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact info</title>

<LINK href="main2.css" type="text/css" rel="stylesheet">

<!--[if IE]>
<style>
    fieldset.nested 
    {
        position: relative;
        margin-top: 15px;        
    }

    fieldset.nested legend 
    {
        position: absolute; top: -8px; left: 1em;
    }
</style>
<![endif]-->

</head>

<body>

<div>    
    <form>

    <fieldset class="main">
        <legend>Contact info</legend>

        <fieldset class="nested">
            <legend>Name</legend>    
            <ol>
                <li>
                    <label for="textboxName">Name</label>
                    <input id="textboxName" name="textboxName" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxName" >Title</label>
                    <input id="textboxName" name="textboxTitle" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxCompany">Company</label>
                    <input id="textboxCompany" name="textboxCompany" type="text" style="width: 15em;"/>
                </li>
            </ol>
        </fieldset>        

        <fieldset class="nested">
            <legend>Address</legend>    
            <ol>
                <li>
                    <label for="textboxAddress1" >Street address</label>
                    <input id="textboxAddress1" name="textboxAddress1" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxAddress2" >Street address</label> 
                    <input id="textboxAddress2" name="textboxAddress2" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxCity" >City</label>
                    <input id="textboxCity" name="textboxCity" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxRegion" >City/Region</label>
                    <input id="textboxRegion" name="textboxRegion" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxPostalCode" >Postal code</label>
                    <input id="textboxPostalCode" name="textboxPostalCode" type="text" style="width: 15em;"/>
                </li>
                <li>
                    <label for="textboxCountry" >Country</label>
                    <input id="textboxCountry" name="textboxCountry" type="text" style="width: 15em;"/>
                </li>
            </ol>
        </fieldset>        

        <fieldset class="nested">
            <legend>Phone numbers</legend>
            <ol>
                <li style="display:none">
                    <label for="textboxName" >Name</label>
                    <input id="text1" name="textboxName" type="text" style="width: 15em;"/>
                </li>
                <li style="display:none">
                    <label for="textboxAddress1" >Address</label>
                    <input id="text2" name="textboxAddress1" type="text" style="width: 15em;" />
                </li>
                <li>
                    <label for="textboxAddress2" >Phone</label> 
                    <input id="text3" name="textboxAddress2" type="text" style="width: 15em;"/>
                </li>
            </ol>    
        </fieldset>        

        <div class="buttonsContainer">
            <input class="button" type="submit" value="OK" /> 
            <input class="button" type="button" value="Cancel" /> 
        </div>

    </fieldset>

    </form>
</div>    


</body>

</html>

CSS:

CSS:

body 
{   
    margin: 0;
    padding: 0; 
    font-family: Verdana, Tahoma, Arial, sans-serif;
}

fieldset.main 
{  
    margin: 1.5em 0 0 1.5em;  
    padding: 1em 0 0 0;
    width: 400px;
    font-size: .9em;    
}

fieldset.main legend
{  
    margin-left: 1em;  
    color: #000000;  
    font-weight: bold;    
}

fieldset.main ol 
{  
    padding: 1em 1em 0 1em;  
    list-style: none;
}

fieldset.main li 
{  
    padding-bottom: .5em;
}

fieldset.main ol li label 
{  
    float: left;
    width: 10em;        
    margin-right: 1em;
}

/* ----------------------------------------- */

fieldset.nested 
{  
    margin: 0 0 1em 1em;  
    padding: 0;
    width: 93%;
    font-size: .8em;
    border: 1px solid gray;
    background: #B0C4DE;    

}

fieldset.nested legend
{  
    margin-left: 1em;      
    font-weight: normal;
    font-size: .9em; 
    color: black;
    background-color: white;
    padding: 0 1em 0 1em;
    border: 1px solid black;
}

fieldset.nested ol 
{  
    padding: 0 1em 0 1em;  
    list-style: none;
}

fieldset.nested li 
{  
    /* Control leading between rows. */
    padding-bottom: .7em;
}

fieldset.nested ol li label 
{  
    float: left;
    width: 10em;        
    margin-right: 1em;
}

/* ----------------------------------------- */

input.button
{                                  
    /* border-style: none; */
    width: 6em;
    height: 2.5em;
}

div.buttonsContainer
{
    float: right;
    margin: 1em 1em 1em 0;
}

回答by Scott Evernden

CSS will work fine -- IF you are okay with entering pixel widths for things But sadly fails when you need to localize your strings and discover labels don't fit. For an address entry form, I would stick to using Tables, as they do all the right re-sizing and wrap behaviour and work w/o issues on almost every browser there is.

CSS 可以正常工作——如果您可以为事物输入像素宽度但遗憾的是,当您需要本地化字符串并发现标签不适合时,它会失败。对于地址输入表单,我会坚持使用表格,因为它们可以进行所有正确的调整大小和包装行为,并且几乎在所有浏览器上都没有问题。

EDIT: I kinda wonder if any of the down-voters has checked the layout for these S.O. pages

编辑:我有点想知道是否有任何反对者检查了这些 SO 页面的布局

回答by Jon Winstanley

A really good way to do this yourself is to install firebug on firefox and inspect elements on websites which implement this really well.

自己做这件事的一个很好的方法是在 firefox 上安装 firebug 并检查网站上的元素,这些元素实现得非常好。

There is a great smashing maagzine on sign up forms. Several approaches in CSS can be seen, with some really great examples.

注册表单上有一本很棒的杂志。可以看到 CSS 中的几种方法,以及一些非常棒的例子。