css标签宽度未生效

The*_*One 104 html css label

我有一个通用的表单,我想设置样式以对齐标签和输入字段.出于某种原因,当我给标签选择器一个宽度时,没有任何反应:

HTML:

<form id="report-upload-form" method="POST" action="" enctype="multipart/form-data">
    <p>
        <label for="id_title">Title:</label> 
        <input id="id_title" type="text" class="input-text" name="title"></p>
    <p>
        <label for="id_description">Description:</label> 
        <textarea id="id_description" rows="10" cols="40" name="description"></textarea></p>
    <p>
        <label for="id_report">Upload Report:</label> 
        <input id="id_report" type="file" class="input-file" name="report">
    </p>
</form>
Run Code Online (Sandbox Code Playgroud)

CSS:

#report-upload-form {
    background-color: #316091;
    color: #ddeff1;
    font-weight:bold;
    margin: 23px auto 0 auto;
    border-radius:10px;
    width: 650px;
    box-shadow:  0 0 2px 2px #d9d9d9;
}

#report-upload-form label {
    padding-left:26px;
    width:125px;
    text-transform: uppercase;
}

#report-upload-form input[type=text], 
#report-upload-form input[type=file],
#report-upload-form textarea {
    width: 305px;
}
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

我究竟做错了什么?

Dav*_*vis 204

display: inline-block:

#report-upload-form label {
    padding-left:26px;
    width:125px;
    text-transform: uppercase;
    display:inline-block
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/aqMN4/


Qua*_*cal 35

使用 display: inline-block;

说明:

label是一个内联元素,意味着它只是它需要的大小.

display属性设置为inline-block或者block为了使width属性生效.

例:

#report-upload-form {
    background-color: #316091;
    color: #ddeff1;
    font-weight: bold;
    margin: 23px auto 0 auto;
    border-radius: 10px;
    width: 650px;
    box-shadow: 0 0 2px 2px #d9d9d9;

}

#report-upload-form label {
    padding-left: 26px;
    width: 125px;
    text-transform: uppercase;
    display: inline-block;
}

#report-upload-form input[type=text], 
#report-upload-form input[type=file],
#report-upload-form textarea {
    width: 305px;
}
Run Code Online (Sandbox Code Playgroud)
<form id="report-upload-form" method="POST" action="" enctype="multipart/form-data">
    <p><label for="id_title">Title:</label> <input id="id_title" type="text" class="input-text" name="title"></p>
    <p><label for="id_description">Description:</label> <textarea id="id_description" rows="10" cols="40" name="description"></textarea></p>
    <p><label for="id_report">Upload Report:</label> <input id="id_report" type="file" class="input-file" name="report"></p>
</form>
Run Code Online (Sandbox Code Playgroud)

  • +1用于在答案中添加解释 - 即使答案与接受的答案相同 (6认同)
  • 请注意,在IE8下面的IE中,内联块的支持是粗略的 - 这些天可能不是太大的问题,但要记住一些事情.(来源http://www.quirksmode.org/css/display.html) (2认同)

Mik*_*ike 6

我相信标签是内联的,因此它们不会占用宽度.也许尝试使用"display:block"并从那里开始.


ctr*_*eep 6

首先使其成为块,然后向左浮动以停止将下一个块推入新行.

#report-upload-form label {
                           padding-left:26px;
                           width:125px;
                           text-transform: uppercase;
                           display:block;
                           float:left
}
Run Code Online (Sandbox Code Playgroud)


Phi*_*mon 5

给风格

display:inline-block;
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助