html浮动占位,带有HTML / CSS的浮动输入占位符

Here is a cool way to make the placeholders "become" labels as the input receives focus.

Here is a pen with the final result.

First, you have to put the label AFTER the input in the HTML.

Full Name

This will ensure that we can target the label with our CSS.

Then, instead of giving the input a placeholder directly in the HTML, we will use our label as the placeholder. In order for the label to be inside the input, we can use transform: translate(). We can also use a lighter color to really make it look like a native placeholder.

Here is the relevant CSS for the initial state (the values used for the translation can vary depending on font size, positioning, etc.).

label {

color: #999;

transform: translate(0.25rem, -1.5rem);

transition: all 0.2s ease-out;

}

Now, to position it on top of the text when the input is in focus, use the following.

input:focus + label {

color: #111;

transform: translate(0, -2.75rem);

}

It works!

But we still have a problem. The styles are are only applied to the label when the input is in focus. This means that if you type something in the input then focus elsewhere, the label will come back to it's original position.

To fix this, we will have to use the :placeholder-shown selector. This selector allows to style an element whenever an input's placeholder is, well, shown. In our case, we actually want to style the label when the placeholder is NOT shown, so we will also have to use the :not() selector. Finally, for this to work, your input has to have a placeholder. Since we want to use our label as the placeholder text, we can simply write an empty space as the placeholder value (an empty string "" will not work).

Full Name

input:focus + label,

input:not(:placeholder-shown) + label {

color: #111;

transform: translate(0, -2.75rem);

}

Now everything works like we want it to.

The :placeholder-shown selector is relatively new, but browser support is pretty good. The only browser to not recognize it is Edge. To remedy this, we can use a @supports query hack to check if the code is run in Edge (you can find more hacks like this at http://browserhacks.com/).

Usually, this hack is used write code that is only supported in Edge. Since we want the opposite, we can add not in front of the condition of the query.

We should also apply the final position to the label before anything else. This way, the Edge browser will display the labels on top of the inputs like a normal form, and the other browsers will apply the 'placeholder' styling to it.

label {

display: block;

transform: translate(0, -2.75rem);

}

@supports (not (-ms-ime-align:auto)) {

label {

color: #999;

transform: translate(0.25rem, -1.5rem);

transition: all 0.2s ease-out;

}

input:focus + label,

input:not(:placeholder-shown) + label {

color: #111;

transform: translate(0, -2.75rem);

}

}

Another possibility for this cool form style is to only move the label when a user types something. For this, you can simply remove the input:focus + label selector. Here is the result.

I hope you found this useful! Let me know in the comments.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值