[英]How can I get two form fields side-by-side, with each field’s label above the field, in CSS?
I am stuck on figuring out some css, I need a section of my form to look like the following,我坚持要找出一些 css,我需要我的表格的一部分如下所示,
I have tried every variation I can think of,我已经尝试了所有我能想到的变化,
I have given the labels a fixed width and floated them left, then given the inputs the same width and floated them left.我给标签一个固定的宽度并将它们向左浮动,然后给输入相同的宽度并将它们向左浮动。
I am all out of ideas, how can I achieve this please?我完全没有想法,请问我该如何实现?
<form>
<label for="company">
<span>Company Name</span>
<input type="text" id="company" />
</label>
<label for="contact">
<span>Contact Name</span>
<input type="text" id="contact" />
</label>
</form>
label { width: 200px; float: left; margin: 0 20px 0 0; }
span { display: block; margin: 0 0 3px; font-size: 1.2em; font-weight: bold; }
input { width: 200px; border: 1px solid #000; padding: 5px; }
Illustrated at http://jsfiddle.net/H3y8j/ 图为http://jsfiddle.net/H3y8j/
You need an HTML element for each column in your layout. 您需要为布局中的每个列添加一个HTML元素。
I'd suggest: 我建议:
<div class="two-col">
<div class="col1">
<label for="field1">Field One:</label>
<input id="field1" name="field1" type="text">
</div>
<div class="col2">
<label for="field2">Field Two:</label>
<input id="field2" name="field2" type="text">
</div>
</div>
.two-col {
overflow: hidden;/* Makes this div contain its floats */
}
.two-col .col1,
.two-col .col2 {
width: 49%;
}
.two-col .col1 {
float: left;
}
.two-col .col2 {
float: right;
}
.two-col label {
display: block;
}
This works well 这很好用
How about something like this? 这样的事怎么样?
<div id="leftContainer">
<span>Company Name</span>
<br><input type="text" value="John Lewis Partnership">
</div>
<div id="rightContainer">
<span>Contact Name</span>
<br><input type="text" value="Timothy Patten">
</div>
Then, you can align the 2 divs by floating them left and right:- 然后,您可以通过左右浮动来对齐2个div: -
#leftContainer {
float:left;
}
#rightContainer {
float:right;
}
This worked perfectly for me without css. 没有CSS,这对我来说非常合适。 I think css would put some icing on the cake though.
我认为css会给蛋糕锦上添花。
<form>
<label for="First Name" >First Name:</label>
<input type="text" name="username" size="15" maxlength="30" />
<label for="Last Name" >Last Name:</label>
<input type="text" name="username" size="15" maxlength="30" />
</form>
<div>
<div style="float:left; width:101px; height:auto;">
<div style="width:200px; float:left;">
LabelText
</div>
<div style="width:200px; float:left;">
<input type="text" name="textfield" id="textfield" />
</div>
</div>
<div style="float:left; width:101px; height:auto;">
<div style="width:200px; float:left;">
LabelText
</div>
<div style="width:200px; float:left;">
<input type="text" name="textfield" id="textfield" />
</div>
</div>
</div>
Give this a try 试一试
<style type="text/css">
form {width:400px;}
#text1 {float:right;}
#text2 {float:left;}
</style>
then 然后
<form id="form1" name="form1" method="post" action="">
<label id="text1">Company Name<br />
<input type="text" name="textfield2" id="textfield2" />
</label>
<label id="text2">Contact Name<br />
<input type="text" name="textfield" id="textfield" />
</label>
</form>
Test Page: http://jsbin.com/ahelo4 测试页面: http : //jsbin.com/ahelo4
Works for me in the latest versions of Firefox, Safari, Chrome, Opera. 适用于最新版本的Firefox,Safari,Chrome,Opera。 Not 100% sure about IE though as im on a Mac, but I cant see why it wouldn't :)
不是100%肯定IE虽然作为我在Mac上,但我不明白为什么它不会:)
Here is a simple way to do it这是一个简单的方法
.style1 {display: inline-block; }
<form> <div class="style1"> <label for="field1">Company Name</label> <br> <input type="text" id="field1" name="field1"> </div> <div class="style1"> <label for="field2">Contact Name</label> <br> <input type="text" id="field2" name="field2"> </div> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.