HTML / CSS выравнивает два входа и текст под ним - PullRequest
2 голосов
/ 25 декабря 2011

Я пытаюсь разместить две формы ввода рядом друг с другом и расположить текст под ними.Каким-то образом это никогда не заканчивается правильно выровненным.На этой картинке показано, что я пытаюсь сделать:

Example picture

HTML:

<form action="#" class="cleanForm" method="POST">
    <fieldset>
        <input type="text" name="firstname" placeholder="first name" required>
        <em>Please enter your first name</em>

        <input type="text" name="lastname" placeholder="last name" required>
        <em>Enter your last name</em>

        <input type="email" name="email"  placeholder="e-mail" required>
        <em>Enter your e-mail address</em>

        <input type="email" name="email2" placeholder="re-enter e-mail" required>
        <em>Re-enter your e-mail address</em>

        <input type="password" name="password" placeholder="password" required>
        <em>Enter a password between 8 and 20 digits</em>

        <input type="password" name="password2" placeholder="re-enter password" required />
        <em>Re-enter the password</em>

        <p>
            <input type="radio" name="gender" value="Female" checked>
            <label for="female">Female</label>

            <input type="radio" name="gender" value="Male">
            <label for="male">Male</label>
        </p>

        <p>
            <input type="checkbox" id="agree-TOS">
            <label for="agree-TOS">I have read and agree to the <a href="#">Terms of Service</a>.</label>
        </p>

        <input type="submit" value="Create account">
    </fieldset>
</form>

CSS:

form.cleanForm {
    width:700px;
    margin:0 auto;
}

form.cleanForm p {
    margin-bottom:15px;
}

input[type="email"], input[type="password"], input[type="text"]  {
    font-family: Arial, Sans-Serif;
    font-size: 18px;
    color: #adadad;
    padding: 10px;
    outline:none;   
    float:left;
    border: solid 1px #adadad;
    width: 230px;
    transition: all 2s ease-in-out;
    -webkit-transition: all 2s ease-in-out;
    -moz-transition: all 2s ease-in-out;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;
    -moz-box-shadow:inset 0 0 5px 5px #E6E6E6;
    -webkit-box-shadow:inset 0 0 5px 5px #E6E6E6;
    box-shadow:inset 0 0 5px 5px #E6E6E6;
    clear: right;
}

input[type="email"]:focus, input[type="email"]:hover, input[type="password"]:focus,
input[type="password"]:hover, input[type="text"]:focus, input[type="text"]:hover {
    border:1px solid #FF003F;   
} 
form.cleanForm em {
    font-size:12px;
}

Ответы [ 4 ]

3 голосов
/ 25 декабря 2011

Вы применяете float:left к <input> с, но не к <em> с.Вот почему все поля сдвигаются влево, а метки остаются в обычном потоке страниц.

Одно из возможных решений - обернуть их оба в <div> и применить float к этому:

HTML:

<div class="field">
    <input type="text" name="firstname" placeholder="first name" required />
    <em>Please enter your first name</em>
</di>

CSS:

div.field {
    float: left;
}

Кроме того, было бы более семантически правильно использовать<label> вместо <em>.

2 голосов
/ 25 декабря 2011

Я думаю, что ваша разметка нуждается в повторении.Используйте <label> s для описания меток элементов формы, а не <em> s.Там нет смысловой ценности в акценте.Кроме того, вы можете использовать сами метки, чтобы легко выровнять форму. Вот мой код (Пример из жизни) :

HTML

<form action="#" class="cleanForm" method="POST">
    <fieldset>
        <label><input type="text" name="firstname" placeholder="first name" required>
        Please enter your first name</label>

        <label><input type="text" name="lastname" placeholder="last name" required>
        Enter your last name</label>

        <label><input type="email" name="email"  placeholder="e-mail" required>
        Enter your e-mail address</label>

        <label><input type="email" name="email2" placeholder="re-enter e-mail" required>
        Re-enter your e-mail address</label>

        <label><input type="password" name="password" placeholder="password" required>
        Enter a password between 8 and 20 digits</label>

        <label><input type="password" name="password2" placeholder="re-enter password" required>
        Re-enter the password</label>

        <div id="gender">
            <label><input type="radio" name="gender" value="Female" checked>
            Female</label>

            <label><input type="radio" name="gender" value="Male">
            Male</label>
        </div>

        <label class="tos"><input type="checkbox" id="agree-TOS">
        I have read and agree to the <a href="#">Terms of Service</a>.</label>
    </fieldset>
    <button type="submit">Create account</button>
</form>

CSS

.cleanForm {
    width: 550px;
}

fieldset > label > input {
    display: block;
}
input[type="checkbox"] {
    display: inline;
}
label {
    margin: 10px;
    padding: 5px;
}
fieldset > label {
    float: left;
    width: 200px;
}
label:nth-child(2n+1) {
    clear: both;
}
#gender, .tos, button {
    clear: both;
}
.tos {
    width: 400px;
}

input[type="text"], input[type="email"], input[type="password"] {
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;

    -webkit-box-shadow: inset 2px 2px 3px rgba(0, 0, 0, 0.2);
    -moz-box-shadow: inset 2px 2px 3px rgba(0, 0, 0, 0.2);
    box-shadow: inset 2px 2px 3px rgba(0, 0, 0, 0.2);

    padding: 5px;
}
2 голосов
/ 25 декабря 2011

Вы можете попробовать это:

HTML

<form action="#" class="cleanForm" method="POST">

        <fieldset>

<div class="column">
            <input type="text" name="firstname" placeholder="first name" required />
            <em>Please enter your first name</em>
            </div>
            <div class="column last">

            <input type="text" name="lastname" placeholder="last name" required />
            <em>Enter your last name</em>

</div>
<div class="column">
            <input type="email" name="email"  placeholder="e-mail" required />
            <em>Enter your e-mail address</em>
            </div>
<div class="column last">
            <input type="email" name="email2" placeholder="re-enter e-mail" required />
            <em>Re-enter your e-mail address</em>
            </div>
<div class="column">

            <input type="password" name="password" placeholder="password" required />
            <em>Enter a password between 8 and 20 digits</em>
            </div>
<div class="column last">


            <input type="password" name="password2" placeholder="re-enter password" required /><br />
            <em>Re-enter the password</em>
            </div>

        <p style="clear:both;">
            <input type="radio" name="gender" value="Female" checked />
            <label for="female">Female</label>

            <input type="radio" name="gender" value="Male" />
            <label for="male">Male</label>
        </p>

        <p>
            <input type="checkbox" id="agree-TOS" />
            <label for="agree-TOS">I have read and agree to the <a href="#">Terms of Service</a>.</label>
        </p>

        <input type="submit" value="Create account" />




        </fieldset>

        </form>

CSS

form.cleanForm {
width:700px;
margin:0 auto;
}
form.cleanForm p {
margin-bottom:15px;
}
input[type="email"], input[type="password"], input[type="text"]  {
font-family: Arial, Sans-Serif;
font-size: 18px;
color: #adadad;
padding: 10px;
outline:none;   
float:left;
border: solid 1px #adadad;
width: 230px;
transition: all 2s ease-in-out;
-webkit-transition: all 2s ease-in-out;
-moz-transition: all 2s ease-in-out;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
-moz-box-shadow:inset 0 0 5px 5px #E6E6E6;
-webkit-box-shadow:inset 0 0 5px 5px #E6E6E6;
box-shadow:inset 0 0 5px 5px #E6E6E6;
clear: right;
}

input[type="email"]:focus, input[type="email"]:hover, input[type="password"]:focus,        input[type="password"]:hover, input[type="text"]:focus, input[type="text"]:hover {
border:1px solid #FF003F;   
}
form.cleanForm em {
font-size:12px;

}
.column{
 width:250px;
 float:left;
 margin-right:20px;
 padding: 10px;   
}
.last{
 margin:0;
 padding: 10px;   
}

выход

0 голосов
/ 26 декабря 2011

@ Ответ PPvG очень близок, но не совершенен:

  • Почти во всех случаях каждый тег <input> должен иметь соответствующий тег <label>, и там, где вы использовали <em>, это идеальное место для этикетки.
  • В этом случае inline-block более подходит, чем float

Вот как бы я это сделал:

HTML:

<div class="field">
    <input type="text" name="firstname" id="firstname_field" placeholder="first name" required />
    <label for="firstname_field">Please enter your first name</label>
</div>

CSS:

div.field {
  display: inline-block;
  width: 200px;
}

div.field label
{
  display: block;
}
...