What is :placeholder-shown in CSS? how to work? What is the use?

青灯夜游
Release: 2021-07-07 19:34:36
forward
3460 people have browsed it

CSS :placeholder-shown pseudo-class is an object specifically used to determine whether an element displays a placeholder. It is mainly used to check whether the input content is empty. This article will take you through: placeholder-shown pseudo-class and introduce in detail how it works.

What is :placeholder-shown in CSS? how to work? What is the use?

Use this pseudo-class to style an input that is currently displaying placeholder text, in other words, the user is not typing anything in the text box

It would be nice to apply some dynamic styling depending on whether your input is empty or not

input:placeholder-shown { border-color: pink; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

How does it work?

:placeholder-showis a CSS pseudo-class that allows you to apply styles toor ## that have placeholder text #.

 
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

Result:

    If the placeholder is shown, it is pink, indicating that the user did not enter anything
  • If no placeholder is shown, it is black, indicating that the user has typed

:placeholder-showdMust have placeholder

This selector will have no effect if the element has no placeholder text.

  
Copy after login
input:placeholder-shown { border-color: pink; }
Copy after login
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

##:placeholder-shownvs::placeholderTherefore, We can use

:placeholder-shown

to style the input element.

input:placeholder-shown { border: 1px solid pink; background: yellow; color: green; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?☝️Hmm... noticed something strange - we set the color to: green, but it didn't work. Well, that's because

:placeholder-shown

only targets the input itself. But for actual placeholder text, you must use the pseudo-element::placeholder.

input::placeholder { color: green; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?But! While I was working on this issue, I noticed that there are a few other properties that would affect the placeholder text if applied at the

:placeholder-shown

level.

input:placeholder-shown { font-style: italic; text-transform: uppercase; letter-spacing: 5px; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?Now, I don't really know why this happens, maybe it's because the properties are inherited by the placeholders.

:placeholder-shownvs:emptyalthough

:placeholder-shown

is specifically Used to determine whether an element displays placeholders. In fact, we can use this to check if the input is empty (assuming, of course, that all inputs have a placeholder). So maybe your next question is, can't we use CSS empty? Ok let's check

<input value="not empty"> <input><!-- empty -->
Copy after login
input:empty { border: 1px solid pink; } input { border: 1px solid black; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?Expecting:

Pink if empty
  • If not Empty is black
  • Hmm...from here on, you might think that
:empty

seems to be working, since what we see is a pink border. But this doesn't actually workThe reason it's shown in pink is because pseudo-classes add specificity, similar to how class selectors (i.e.

.form-input

) are better than type selectors ( i.e.input) has higher specificity. High-specificity selectors will always override styles set by low-specificity.This is the verdict! Don't use

:empty

to check if the input element is empty

How to check if the input is empty without placeholder?

Okay, so the only way we can check if the input is empty is to use

:placeholder-shown

. But what happens if our input elements don't have placeholders? Well, that's a smart way! Pass in an empty string" ".

<input placeholder=" "><!-- 传递空字符串 -->
Copy after login
input:placeholder-shown { border-color: pink; }
Copy after login
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

Combined with other selectors

So, we can target input elements that display placeholder text, which is cool . In other words, if placeholder text is displayed, it must mean that the element is empty. Using this knowledge, we can combine this pseudo-class with other selectors and do some really neat things! let's see.

Reverse

:placeholder-shownFor:notwe can use

:not

Pseudo class to do the opposite. Here, we can perform target operations when the input is not empty.

<input placeholder="placeholder" value="not empty" />
Copy after login
input:not(:placeholder) { border-color: green; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

结果:

  • 绿色,如果不为空,则表示用户已经输入了一些内容。
  • 如果为空,则为黑色

浮动标签

使用占位符而不使用标签的问题之一就是无障碍,因为一旦你在打字的时候,占位符文字就没有了,这可能会导致用户的困惑。一个真正好的解决方案是浮动标签。最初,占位符文本显示时没有标签,而一旦用户开始输入,标签就会出现。这样一来,你仍然可以在不影响用户体验和可访问性的前提下,保持表单的简洁性。双赢

而这是可以用纯CSS实现的,我们只需要将 placeholder-shown:not+结合起来就可以了。这是一个超级简化版的浮动标签。

 
Copy after login
label { display: none; position: absolute; top: 0; } input:not(:placeholder-shown) + label { display: block; }
Copy after login

What is :placeholder-shown in CSS? how to work? What is the use?

浏览器支持

:placeholder-shown的支持非常好!这包括Internet Explorer(是的,我和你一样惊讶)。但是,对于IE,你需要使用非标准名称:-ms-input-placeholder

1What is :placeholder-shown in CSS? how to work? What is the use?

本文转载自:https://segmentfault.com/a/1190000039928413

作者:杜尼卜

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What is :placeholder-shown in CSS? how to work? What is the use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!