如何单独对齐Web表单文本和输入?

时间:2012-01-01 20:27:59

标签: html css forms alignment

我正在寻找一种最有效的方法来编写一个相当简单的html表单布局我已经完成了一个模型。

到目前为止,我已经想到了一些编码方法,但是在实现时它们看起来都很麻烦。

基本上我正在尝试付诸行动的是纯文本与右边对齐,并形成与左边对齐的插入,两边都有一条线。下面的图片应该举例说明我正在努力实现的目标。

6 个答案:

答案 0 :(得分:3)

这是一种方法,虽然我认为你可以通过展示以前的尝试和解释你遇到的问题来帮助自己更多。但是:

form {
    width: 80%;
    max-width: 40em;
    margin: 0 auto;
    color: #3f3a27;
    background: #f4f0e5 url(http://davidrhysthomas.co.uk/linked/test.png) 35% 0 repeat-y;
    padding: 0.5em;
}

label, input[type=text], select {
    display: inline-block;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -o-box-sizing: border-box;
    -box-sizing: border-box;
    margin-bottom: 0.6em;    
}

label {
    width: 30%;
    text-align: right;
    margin: 0 10% 0 0;
}

label:after {
    content: ': ';
}

input[type=text] {
    width: 40%;
}

select {
    width: 20%;
}

fieldset {
    margin: 0 0 1em 0;
}

使用HTML:

<form action="#" method="post">
    <fieldset>
        <label for="fullName">Full name</label>
        <input type="text" id="fullName" />
        <label for="companyName">Company</label>
        <input type="text" id="companyName" />
    </fieldset>
    <fieldset>
        <label for="select">Select</label>
        <select id="select" name="select">
            <option>Option one</option>
            <option>Option two</option>
        </select>
        <label for="t1">Text input 1</label>
        <input id="t1" type="text" />
        <label for="t2">Text input 1</label>
        <input id="t2" type="text" />
        <label for="t3">Text input 1</label>
        <input id="t3" type="text" />
    </fieldset>
</form>

JS Fiddle demo

答案 1 :(得分:1)

答案 2 :(得分:1)

这是一个最小风格的基本表单

<强> HTML

<form>
    <div class="line">
        <label for="input">Full Name</label>
        <div class="input">
            <input type="text" size="30" name="input">
        </div>
    </div>
    <div class="line">
        <label for="input">Company</label>
        <div class="input">
            <input type="text" size="30" name="input">
        </div>
    </div>
    <div class="line">
        <label for="nselect">Dropdown Menu</label>
        <div class="input">
            <select name="select">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
            </select>
        </div>
    </div>
    <div class="line">
        <label for="input">Text 1</label>
        <div class="input">
            <input type="text" size="30" name="input">
        </div>
    </div>
    <div class="line">
        <label for="input">Text 2</label>
        <div class="input">
            <input type="text" size="30" name="input">
        </div>Save
    </div>
    <div class="line">
        <label for="input">Text 3</label>
        <div class="input">
            <input type="text" size="15" name="input">
        </div>
    </div>
</form>

<强> CSS

form {
    margin:10px 0;
}

label {
    color: #404040;
    float: left;
    font-size: 13px;
    line-height: 18px;
    padding-top: 6px;
    text-align: right;
    width: 130px;
}

label, input, select, textarea {
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    font-size: 13px;
    font-weight: normal;
    line-height: normal;
}

input, textarea, select {
    -moz-border-radius: 3px 3px 3px 3px;
    border: 1px solid #CCCCCC;
    color: #808080;
    display: inline-block;
    font-size: 13px;
    height: 18px;
    line-height: 18px;
    padding: 4px;
    width: 210px;
}

select {
    height: 27px;
    line-height: 27px;
}

form .input {
    margin-left: 150px;
}

form .line {
    margin-bottom: 18px;
}

测试 http://jsfiddle.net/andresilich/qxMVd/

答案 3 :(得分:1)

我建议使用table, table-row, and table-cell的显示值,以使标记尽可能保持整齐:请参阅this jsFiddle

答案 4 :(得分:1)

我喜欢你想要它:http://jsfiddle.net/XURye/

使用相同的颜色,每个位置也是正确的!

答案 5 :(得分:0)

这个Drupal post的最小两个选择器CSS工作得很好:

&#13;
&#13;
.form-item {
  padding: 10px 0 10px 200px;
  position: relative;
}
.form-item label {
  left: 0;
  position: absolute;
}
&#13;
&#13;
&#13;