如何使用验证为表单中的所有字段提供相等的宽度?

时间:2014-05-01 06:21:30

标签: javascript jquery css html5 forms

在下面的HTML5表单代码中,如何为所有名称和输入字段赋予相同的宽度,以便所有名称在同一列中对齐,并且所有输入类型字段在相同的列中对齐,具有相同的宽度? / p>

Html代码:

<!DOCTYPE html>

<html>

<head>
    <title>CSS Contact Form</title>

    <meta charset="utf-8" />

    <link rel="stylesheet" href="style.css" type="text/css" media="all" />
</head>

<body style=" background-color:#E5E4E2">

    <h2>Registration Form</h2>
    <div style="margin-left:420px; border:1px solid #ccc; width:400px; padding:15px; border-radius:5px;">
    <form class="form">

        <p class="name">
            <label for="name"> First Name *:</label>
            <input type="text" name="fname" id="fname" placeholder="John" required/>
        </p>

        <p class="name">
            <label for="name">Last Name *:</label>
            <input type="text" name="lname" id="lname" placeholder="Maxwell" required/>

        </p>

        <p class="web">
            <label for="web">Company Name *:</label>
            <input type="text" name="web" id="web" placeholder="BDI Systems & Technologies" required/>

        </p>

        <p class="email">
            <label for="email">Email Id *:</label>
            <input type="text" name="email" id="email" placeholder="mail@bdisys.com" required/>
        </p>

        <p class="budget">
            <label for="name">Budget :</label>
            <input type="text" name="budget" id="budget" placeholder="200000"/>

        </p>

        <p class="submit">
            <input type="submit" value="Send" />
        </p>

    </form>
</div>
</body>

</html>

的CSS:

body {
    padding: 100px 100px;
    font-size: 13px;
    font-style: Verdana, Tahoma, sans-serif;
}

h2 {
    margin-bottom: 20px;
    color: #474E69;
    margin-left:420px;
}

/* ===========================
   ====== Contact Form ======= 
   =========================== */

input, textarea {
    padding: 10px;
    border: 1px solid #E5E5E5;
    width: 200px;
    color: #999999;
    box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
    -moz-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;     
}

textarea {
    width: 400px;
    height: 150px;
    max-width: 400px;
    line-height: 18px;
}

input:hover, textarea:hover,
input:focus, textarea:focus {
    border-color: 1px solid #C9C9C9;
    box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px;
    -moz-box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 8px; 
}

.form label {
    margin-left: 10px;
    color: #999999;
}

/* ===========================
   ====== Submit Button ====== 
   =========================== */

.submit input {
    width: 100px; 
    height: 40px;
    background-color: #474E69; 
    color: #FFF;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;     
}

2 个答案:

答案 0 :(得分:6)

JSBIN

只需添加display:inline-block并指定min-width到label元素

.form label {
    display: inline-block;
    margin-left: 10px;
    color: #999999;
    min-width: 110px;
}

答案 1 :(得分:1)

您可以使用浮动来调整对齐方式。就像让标签向左浮动而字段向右浮动一样。

我为你做了所有改变的小提琴。看看:http://jsfiddle.net/CZL4w/

我在CSS中进行了以下更改:

input, textarea {
   float:right;
}
input, textarea {
    padding: 10px;
    border: 1px solid #E5E5E5;
    width: 200px;
    color: #999999;
    box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
    -moz-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;     
   float:right;
}
.form label {
    margin-left: 10px;
    color: #999999;
    float:left;
    margin-top:10px;
}

HTML代码中的以下更改:

<div style="margin-left:420px; border:1px solid #ccc; width:400px; padding:15px; border-radius:5px; float:left;">

我希望这会有所帮助。