简体   繁体   English

输入的CSS流体布局

[英]CSS fluid layout for inputs

I am currently making a website and the sign up page requires: 我目前正在建立一个网站,注册页面要求:
- first name - 名字
- last name - 姓
- username - 用户名
- email - 电子邮件
- password - 密码
- retype password - 重新输入密码
I need the first name and last name inputs to be side by side and small while the others below them are approximately twice the width. 我需要名字和姓氏输入并排和小,而其他下面的输入大约是宽度的两倍。 Like this: 像这样:

 Name
+-----------+    +-----------+
| first     |    | last      |
+-----------+    +-----------+

 Email
+----------------------------+
|                            |
+----------------------------+

I am extremely opposed to using px and absolutely need to use % for the widths. 非常反对使用px ,绝对需要使用%作为宽度。
I cannot make the small inputs 50% and the large ones 100% because the space in between the two small ones is not accounted for, you know? 我不能将小输入50%和大输入100%因为两个小的输入之间的空间没有被考虑,你知道吗?
I'm talking about this space: 我在谈论这个空间:

 Name
+-----------+    +-----------+
| first     |<-->| last      |
+-----------+    +-----------+

So that is why I can't use 50% for small ones and 100% for large ones! 所以这就是为什么我不能使用50%的小型和100%的大型的!
Any help is greatly appreciated! 任何帮助是极大的赞赏!

Simple do this: 这样做很简单:

call the small ones : half 叫小的:一半

.first {margin-left: 0 !important; clear: left;}
.full {width:100%}
.half {width: 48%;}
.half {float: left; margin-left: 2%;}

<div class="first half">First Name</div>
<div class="half">Last Name input</div>
<div class="full">Email Input</div>

As you can see I used margin % to make the spacing...which thats how I made 48% -2%(you can do whatever you like as long as it equals..). 正如你所看到的,我使用margin%来制作间距...这就是我如何制作48%-2%(你可以做任何你喜欢的事情,只要它等于......)。 I made a class called .first to make things look right (you can use jquery to create this an auto class). 我创建了一个名为.first的类来使事情看起来正确(您可以使用jquery创建一个自动类)。

Heres a Fiddle: 这是一个小提琴:

FIDDLE THAT 很好的

Hope this helps you. 希望这对你有所帮助。

Here's a Fiddle 这是一个小提琴

<form>
  <input name="firstname" type="text" placeholder="First Name" class="small left"/>
  <input name="lastname" type="text" placeholder="Last Name" class="small"/>
  <input name="username" type="text" placeholder="User Name" class="big"/>
  <input name="email" type="text" placeholder="E-mail" class="big"/>
  <input name="password" type="password" placeholder="Password" class="big"/>
  <input name="rpassword" type="password" placeholder="Repeat Password" class="big"/>
  <button name="submit" type="">Sign Up</button>
</form>


form {
  background: #555;
  width: 350px;
  height: 300px;
  text-align: center;
  padding: 30px 15px;
  border-radius: 10px;
}
button {
  background: #04a9e8;
  position: relative;    
  margin: 25% auto;
  width: 140px;
  height: 24px;
  border: 1px solid #444;
  letter-spacing: 1px;
  font-weight: bold;
  color: #fff;
  border-radius: 4px;
}
button:hover {
  background: #02b9fc;
}
.small,
.big {
  background: #f5f5f5;
  height: 22px;
  padding-left: 4px;
  margin-bottom: 12px;
  border: 1px solid #444;    
}
.small:focus,
.big:focus {
  background: #fff;
  outline: none;
  border: 1px solid #0296cc;
  box-shadow: 0 0 2px 0 #0296cc;
}
.small {
  width: 45.5%;
}
.big {
  width: 98.1%;    
}
.left {
  margin-right: 15px;
}

if you apply 100% with on input element it will overflow the div box because input element has some default padding, for perfect solution you should use this: 如果你对输入元素应用100%它将溢出div框,因为输入元素有一些默认填充,为了完美的解决方案你应该使用这个:

Your CSS: 你的CSS:

input{
    box-sizing:border-box;
    -moz-box-sizing:border-box; /* Firefox */
}
.clearfix { clear: both; }
.first, .last{ width: 48%; }
.first{ float: left; }
.last{ float: right; }
.email{ width: 100%;}

Your HTML: 你的HTML:

<div class="clearfix">
    <label>Name</label><br>
    <input type="text" class="first">
    <input type="text" class="last">
</div>
<div class="clearfix">
    <label>Email</label><br>
    <input type="text" class="email">
</div>

You can checkout this page to know how to align layout with float - http://www.tutorialrepublic.com/css-tutorial/css-alignment.php 您可以查看此页面以了解如何将布局与浮动对齐 - http://www.tutorialrepublic.com/css-tutorial/css-alignment.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM