无法对齐标签,在表单中输入

时间:2016-03-23 21:29:19

标签: html css

您好我在html页面中对齐标签,输入字段和投递箱时遇到问题。我希望标签在左边对齐,然后在它旁边输入。但它总是在这个地方并没有对齐它。我希望我的表单看起来像这样

enter image description here

但我的网页目前看起来像这样: enter image description here

下面是我的html和css代码:

<!Doctype html>
<html lang="en-US">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" /> 
  <meta name="description" content="Assignment 3 using Ajax" />

  <!-- Title -->
  <title>Assignment 3</title>

 <!--CSS Styling -->
 <link type="text/css" href="style.css" rel="stylesheet" />

 <!-- script -->
 <script type="text/javascript" src="script.js">
 </script>

</head>

<body>
  <header><h1> Registration </h1></header>
    <form name="myForm" action="#" method="post" onsubmit="return validate()">
        <fieldset>
            <label for="username">Username: </label>
            <input type="text" id="username" name="user" placeholder="Enter username" />

            <label for="password">Password: </label>
            <input type="password" id="password" name="user_p" />

            <progress id="progress" value='0' max="100"></progress><span id="text"></span>

            <label for="re_password">Verify Password: </label>
            <input type="password" id="re_password" name="user_pass" placeholder="Re-enter password" />

            <label for="email">Email: </label>
            <input type="email" id="email" name="user_email" placeholder="Enter email"  />

            <label for="email">Verify Email: </label>
            <input type="email" id="email" name="user_email2" />    

            <label for="question1">Security Question </label>
            <select id="question1" name="question1">
                <option value="none"> --- Select a question --- </option>
                <option value="a">What's your favorite color?</option>
                <option value="b">What's the make of your first car?</option>
                <option value="c">What's your mother's maiden name?</option>
                <option value="d">What city were you born?</option>
                <option value="e">What high school did you attend?</option>
            </select>

            <label for="answer">Security Answer: </label>
            <input type="text" name="user_answer1"  />

            <label for="question2">Security Question </label>
            <select id="question2" name="question2">
                <option value="none"> --- Select a question --- </option>
                <option value="a">What's your favorite color?</option>
                <option value="b">What's the make of your first car?</option>
                <option value="c">What's your mother's maiden name?</option>
                <option value="d">What city were you born?</option>
                <option value="e">What high school did you attend?</option>
            </select>


            <label for="answer2"> Security Answer: </label>
            <input type="text" id="answer2" name="user_answer2" />

            <label for="mobile">Phone: </label>
            <input type="text" id="mobile" name="user_mobile" />

            <label for="address">Address: </label>
            <textarea id="t1" rows="8" cols="40"name="user_address" style="vertical-align: top;"></textarea>

            <label for="interests">Areas you may be interested in, please select one or more: </label>
            <textarea id="interests" rows="10" cols="40" name="user_extra" style="vertical-align: top;"></textarea>

            <div id="box1" ondrop="drop(event)" ondragover="allowDrop(event)">
                <img src="Red_Apple.jpg" draggable="true" ondragstart="drag(event)" id="drag1" width="110" height="40" />
            </div>
            <div id="box2"  ondrop="drop(event)" ondragover="allowDrop(event)">
                <img src="Banana.jpg" draggable="true" ondragstart="drag(event)" id="drag2" width="110" height="40" />
            </div>

            <div id="box3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>            
        </fieldset>

        <input type="button" name="submit" value="Register" onclick="return validate()" />
        <input type="button" value="Clear" />

    </form>

</body>

CSS:

form {
     max-width: 750px;
     margin-left: 50px;
     background: #f4f7f8;
     border-radius: 8px;
    }
label {
     display:block;
     text-align: right;
     width:145px;
     float: left;
     padding-top: 5px;
   }

input {
    display: inline-block;
    margin-bottom: 5px;
    margin-top: 5px;
    margin-left: 20px;
    vertical-align: middle;
   }

fieldset  {
    margin-bottom: 30px;
    border: none;
   }

#box1, #box2 {
    float:right;
    width: 112px;
    height: 42px;
    border: 1px solid;
  }

div#box3{
  width:112px;
  height:42px;
  border: 1px solid;
 }

如果能够像第一张照片中那样正确地对齐标签和输入,请告诉我正确的方向。

2 个答案:

答案 0 :(得分:0)

如果您将inputlabel的每个“行”放在包装器div中,您可以轻松解决您遇到的问题。包装器的作用类似于标签周围的全宽包装和该行的输入。每个新标签&amp;输入将进入下一行。

我还对您的label样式做了一些调整,因为它需要display: inline-block;而且不需要float: left;

请参阅my JSFiddle demo

答案 1 :(得分:0)

我知道你想要一个原生的css解决方案,但我已经完成了一个有趣的bootstrap版本。您可以查看here

相当于我的解决方案是为标签和输入设置特定的宽度:

input,label{
  display:inline-block;
  width:60%;
}
label{
  width:30%;
}

并确保总和为100%或使用容器div,就像我在bootstrap中一样。