0

我一直在尝试编辑这个简单的表格,让它看起来很好看 3 个小时,但我仍然不在那里。我希望输入字段自然与标签位于同一行,但不知何故,输入比标签低一点,我似乎无法用边距编辑它们。我究竟做错了什么?

这是表格的样子:

http://snag.gy/oPRNy.jpg

// 联系表

<label for="name"><p>Name:</p></label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email"><p>Email:</p></label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject"><p>Subject:</p></label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments"><p>Comments:</p></label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />

// CSS

    label {
    float: none;
    font-size: 100%;
    width: 250px; /* just this width evens out input box placement */
    font-weight: bold;
}
input { /*I think these just fall in because they are naturally following the labels!*/
    width: 250px;
    padding:5px;
    margin-top: -10px;
}
textarea {
    width: 250px;
    height: 150px;
    resize:none;
}
guestbook {
    margin-top:50px;
    text-align:center;
    font-size:26px;
    color:#05924b;
    font-family:Gisha;

}
gb p {
    color:#05924b;
    font-family:Gisha;
    text-align:left;
    margin-left:85px;
    margin-top:0px;
    margin-bottom:0px;
}

// 解决方案:从表单中删除“p”段落并从 CSS 调整其余部分,将 float:left 添加到不同的输入字段和行,降低标签宽度以使输入更接近,然后计算并放置正确的边距- 对 input{} 和 textarea{} 都有权利,最后是 #submit 以使所有内容井井有条。下面是新代码的截图: http: //snag.gy/PwpbQ.jpg

// CSS

    /* Input */
label {
    float: left;
    font-size: 100%;
    width: 50px; /* just this width evens out input box placement */
    font-weight: bold;
    margin: 2px 0;
    padding:5px;
    font-family: Gisha;
    font-style: normal;
    font-variant: normal;
    text-decoration: none;
    text-align: left;
}
input { /*I think these just fall in because they are naturally following the labels!*/
    width: 300px;
    padding:5px;
    margin: 5px 0;
    font-size:24px;
    margin-right:192px;
}
textarea {
    width: 300px;
    height: 150px;
    resize:none;
    margin:5px 0;
    padding:5px;
    margin-right:192px;
}
#submit {
    margin-right:225px; 
}
/* End of input */
4

1 回答 1

1

从标签中删除所有<p>标签。<p>标记是块级元素,因此不应嵌套在内联元素中<label>。块级元素也清晰,这意味着它们不允许任何一侧的内容(除非浮动)。我相信这会导致您的问题。

<label for="name">Name:</label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />

调整标记后,labelandinput标签没有垂直间距。要添加垂直间距,您可以为两个元素添加边距。

label {
    font-size: 100%;
    width: 250px;
    font-weight: bold;
    margin: 2px 0;
}

input {
    width: 250px;
    padding:5px;
    margin: 2px 0;
}

示例:http: //jsfiddle.net/KZrXD/

于 2012-12-07T01:09:41.287 回答