html表单 - 使输入显示在同一行

Pec*_*tum 22 html css forms input css3

我正在努力使两个html表单输入(名字和姓氏)并排出现在同一行.我尝试过使用float,但这似乎使其余的输入到处都是.任何建议将不胜感激 - 这是我的代码:

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> 
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

这是JSBin演示.

小智 29

http://www.w3schools.com/cssref/pr_class_display.asp 将div中的多个表单元素用一个使用display:table的类包装.在div中,使用display:table-cell包装每个标签并在div中输入.远离花车!

  • 每个人都忽略了这个答案,但这是唯一对我有用的东西. (10认同)
  • 这就是为什么在吸引观众和投票方面,StackOverflow标准不将其视为“好”答案:1)缺少格式,使该文本难以阅读。阅读其他几篇文章比仅这篇文章要容易。2)实际上是仅链接的答案,因为它3)描述了如何编写代码而不实际编写任何代码。提供带有示例代码的答案(我们可以看到),将吸引更多的观众实际阅读它。假设建议是好的,更多的阅读将产生更多的投票。 (3认同)

Ale*_*dro 19

您可以在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>
Run Code Online (Sandbox Code Playgroud)

float:leftCSS中的每个输入:

.your-class input{
  float:left;
}
Run Code Online (Sandbox Code Playgroud)

仅举例

您可能需要调整边距.

记得申请clear:left或两者兼而有之".your-class"


zur*_*fyx 11

更现代的解决方案:

使用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... */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)