2013-08-27 53 views
17

Tôi đang cố gắng tạo hai đầu vào dạng html (tên và họ) xuất hiện trên cùng một dòng cạnh nhau. Tôi đã thử sử dụng phao, nhưng điều đó dường như làm cho phần còn lại của các yếu tố đầu vào đi khắp mọi nơi. Bất kỳ lời khuyên sẽ được đánh giá rất nhiều - đây là mã của tôi:biểu mẫu html - làm cho dữ liệu nhập xuất hiện trên cùng một dòng

HTML:

<form action="includes/contact.php" method="post"> 

    <label for="First_Name">First Name:</label> 
    <input name="first_name" id="First_Name" type="text" /> 
    <label for="Name">Last Name:</label> 
    <input name="last_name" id="Last_Name" type="text" /> 

    <label for="Email">Email:</label> 
    <input name="email" id="Email" type="email" /> 

    <label for="Telephone">Telephone:</label> 
    <input name="telephone" id="Telephone" type="tel" /> 

    <label for="Wedding">Wedding Date:</label> 
    <input name="wedding" id="Wedding" type="date" /> 

    <label for="Requirements">Specific Requirements:</label> 
    <textarea name="requirements" id="Requirements" maxlength="1000" cols="25" rows="6"> </textarea> 

    <input type="submit" value="Submit"/> 
</form> 

CSS:

#contactpage form { 
    overflow: hidden; 
    display: block; 
} 

#contactpage form label { 
    margin-top:12px; 
    font-size: 1.15em; 
    color: #333333; 
    font-family: 'Roboto Condensed', Helvetica, Arial, sans-serif; 
    font-weight: normal; 
    line-height: 1.2; 
} 

#contactpage form input { 
    border: 1px solid #DDDDDD; 
    box-shadow: none; 
    -webkit-box-shadow: none; 
    -moz-box-shadow: none; 
    border-radius: 0px; 
    -moz-border-radius: 0px; 
    -khtml-border-radius: 0px; 
    -webkit-border-radius: 0px; 
} 

#contactpage form input[type='text'] { 
    width: 22%; 
    display: inline!important; 
    background: #F9F9F9; 
    font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; 
    font-size: 1.1em; 
    line-height: 1.4; 
    padding: 6px; 
} 

#contactpage form input[type='email'], 
#contactpage form input[type='tel'], 
#contactpage form input[type='date'], 
#contactpage form textarea { 
    width: 94%; 
    display: block; 
    background: #F9F9F9; 
    font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; 
    font-size: 1.1em; 
    line-height: 1.4; 
    padding: 6px; 
} 

#contactpage form input[type='submit'] {  
    float:right; 
    clear:both; 
    display: block; 
    background: #F9F9F9; 
    color: #333333; 
    font-size: 1.5em; 
    font-family: 'Roboto Condensed', Helvetica, Arial, sans-serif; 
    font-weight: 300; 
    font-style: normal; 
    text-transform: uppercase; 
    text-decoration: none; 
    line-height: 1.2; 
    padding: 20px 20px 40px; 
} 

#contactpage form input[type='submit']:hover { 
    color: #FFFFFF; 
    background: #f1bdb5; 
} 

Đây là JSBin Demo.

+1

Sử dụng 'display: inline-block' insted của 'float'. – Vucko

Trả lời

17

Bạn có thể bao gồm những điều sau đây trong DIV:

<div class="your-class"> 

    <label for="First_Name">First Name:</label> 
    <input name="first_name" id="First_Name" type="text" /> 
    <label for="Name">Last Name:</label> 
    <input name="last_name" id="Last_Name" type="text" /> 

</div> 

Cung cấp cho mỗi đầu vào float:left trong CSS của bạn:

.your-class input{ 
    float:left; 
} 

ví dụ duy nhất

Bạn có thể phải điều chỉnh lợi nhuận.

Hãy nhớ để áp dụng clear:left hoặc cả hai để bất cứ điều gì xảy ra sau khi ".your-class"

+0

Không hoạt động. Hãy nhìn xuống câu trả lời của Mlasell. – DBS

+0

Tại thời điểm trả lời (2013 gần 2 năm trước) bảng hiển thị không được hỗ trợ đầy đủ, bây giờ là: http://caniuse.com/#search=display%3Atable – Alex

1

Cố gắng chỉ cần đặt một div xung quanh tên đầu tiên và cuối cùng đầu vào/nhãn như thế này:

<div class="name"> 
     <label for="First_Name">First Name:</label> 
     <input name="first_name" id="First_Name" type="text" /> 

     <label for="Name">Last Name:</label> 
     <input name="last_name" id="Last_Name" type="text" /> 
</div> 

Nhìn vào fiddle đây: http://jsfiddle.net/XAkXg/

1

bảng sử dụng

<table align="center" width="100%" cellpadding="0" cellspacing="0" border="0"> 
<tr> 
<td><label for="First_Name">First Name:</label></td> 
<td><input name="first_name" id="First_Name" type="text" /></td> 
<td><label for="last_name">Last Name:</label></td> <td> 
<input name="last_name" id="Last_Name" type="text" /></td> 
</tr> 
<tr> 
<td colspan="2"><label for="Email">Email:</label></td> 
<td colspan="2"><input name="email" id="Email" type="email" /></td> 
</tr> 
<tr> 
<td colspan="4"><input type="submit" value="Submit"/></td> 
</tr> 
</table> 
+2

Tôi sẽ không sử dụng bảng nếu không hiển thị dữ liệu .... – Alex

+4

Thôi nào, đó là năm 2013! – FreeAsInBeer

+3

Cảm ơn! Bảng làm việc hoàn hảo cho lựa chọn đầu vào đơn giản của tôi. Và đó là năm 2015 không kém! – Westrock

23

http://www.w3schools.com/cssref/pr_class_display.asp Quấn nhiều yếu tố hình thức của bạn trong một div với một lớp có sử dụng display: table. Bên trong div đó, bọc từng nhãn và nhập vào các div với một lớp sử dụng display: table-cell. Tránh xa những phao nổi!

+6

Mọi người bỏ qua câu trả lời này, nhưng đó là chỉ điều đó làm việc cho tôi. –

4

Một giải pháp hiện đại hơn:

Sử dụng display: flexflex-direction: row

form { 
 
    display: flex; /* 2. display flex to the rescue */ 
 
    flex-direction: row; 
 
} 
 

 
label, input { 
 
    display: block; /* 1. oh noes, my inputs are styled as block... */ 
 
}
<form> 
 
    <label for="name">Name</label> 
 
    <input type="text" id="name" /> 
 
    <label for="address">Address</label> 
 
    <input type="text" id="address" /> 
 
    <button type="submit"> 
 
    Submit 
 
    </button> 
 
</form>