2016-04-28 46 views

回答

2

General sibling selectors & :focus做的招用非常简单的方式;)

input{ 
 
position:absolute; 
 
top:20px; 
 
} 
 
input ~ span{ 
 
transition:top .7s ease; 
 
position:absolute; 
 
top:20px; 
 
} 
 
input:focus ~ span{ 
 
top:0px; 
 
}
<label> 
 
<input> 
 
<span>Text</span> 
 
</label>

这里是一个多领域

https://jsfiddle.net/shLe3107/1/

希望这就够了别人只问

+0

,当你添加文本,然后将标签移回下来怎么办?输入文字后如何保持以上? – Jylaw38

+0

你需要添加javascript来检查它是否为空......没有简单的解决方案。 http://stackoverflow.com/questions/3617020/matching-an-empty-input-box-using-css http://stackoverflow.com/questions/16952526/detect-if-an-input-has-text-in -it-using-css – cocco

+0

没有这样的输入:空或输入[值=“”] .... btw在铬输入[值=“”]应该工作...但其他人? – cocco

0

我找到了一个好Codepen展示了如何做到这一点在CSS的例子为例。

HTML:

<div class="row"> 
    <input id="name" type="text" name="name"> 
    <label for="name">Full Name</label> 
</div> 

CSS:

.row { 
    position: relative; 
    margin-top: 35px; 
} 

input { 
    display: block; 
    padding: 8px 12px; 
    width: 100%; 
    max-width: 300px; 
    border-radius: 5px; 
    border: 0; 
} 

label { 
    position: absolute; 
    font-weight: 600; 
    color: #777777; 
    top: 50%; 
    left: 12px; 
    transform: translateY(-50%); 
    -ms-transform: translateY(-50%); 
    -webkit-transform: translateY(-50%); 
    cursor: text; 
    user-select: none; 
    transition: 0.15s ease-in-out; 
} 

input[data-empty="false"] + label, 
input:valid + label, 
input:focus + label { 
    top: -10px; 
    left: 0px; 
    font-size: 10px; 
    color: #ffffff; 
} 

例子: https://codepen.io/sivan/pen/alKwf