How to make

后端 未结 12 1253
日久生厌
日久生厌 2020-12-12 15:37

I am creating a registration form for a website. I want each label and its corresponding input element to appear on the same line.

Here\'s my code:

12条回答
  •  旧时难觅i
    2020-12-12 16:31

    Assuming you want to float the elements, you would also have to float the label elements too.

    Something like this would work:

    label {
        /* Other styling... */
        text-align: right;
        clear: both;
        float:left;
        margin-right:15px;
    }
    

    #form {
        background-color: #FFF;
        height: 600px;
        width: 600px;
        margin-right: auto;
        margin-left: auto;
        margin-top: 0px;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        padding: 0px;
        text-align:center;
    }
    label {
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        color: #333;
        height: 20px;
        width: 200px;
        margin-top: 10px;
        margin-left: 10px;
        text-align: right;
        clear: both;
        float:left;
        margin-right:15px;
    }
    input {
        height: 20px;
        width: 300px;
        border: 1px solid #000;
        margin-top: 10px;
        float: left;
    }
    input[type=button] {
        float:none;
    }

    Alternatively, a more common approach would be to wrap the input/label elements in groups:

    #form {
        background-color: #FFF;
        height: 600px;
        width: 600px;
        margin-right: auto;
        margin-left: auto;
        margin-top: 0px;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        padding: 0px;
        text-align:center;
    }
    label {
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        color: #333;
        height: 20px;
        width: 200px;
        margin-top: 10px;
        margin-left: 10px;
        text-align: right;
        margin-right:15px;
        float:left;
    }
    input {
        height: 20px;
        width: 300px;
        border: 1px solid #000;
        margin-top: 10px;
    }

    Note that the for attribute should correspond to the id of a labelable element, not its name. This will allow users to click the label to give focus to the corresponding form element.

提交回复
热议问题