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.
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; }
How does it work?
:placeholder-show
is a CSS pseudo-class that allows you to apply styles to <input>
or ## that have placeholder text #
- 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. <input /><!-- 没有占位符 --> <!-- 这也被视为没有占位符文本 --> <input placeholder="" />
input:placeholder-shown { border-color: pink; }
##:placeholder-shown vs ::placeholder
Therefore, We can use
:placeholder-shown to style the input element. <pre class='brush:php;toolbar:false;'>input:placeholder-shown {
border: 1px solid pink;
background: yellow;
color: green;
}</pre>
☝️Hmm... noticed something strange - we set the color to: green, but it didn't work. Well, that's because
only targets the input itself. But for actual placeholder text, you must use the pseudo-element ::placeholder
. <pre class='brush:php;toolbar:false;'>input::placeholder {
color: green;
}</pre>
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
level. <pre class='brush:php;toolbar:false;'>input:placeholder-shown {
font-style: italic;
text-transform: uppercase;
letter-spacing: 5px;
}</pre>
Now, I don't really know why this happens, maybe it's because the properties are inherited by the placeholders.
:placeholder-shown vs :empty
although
: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 <pre class='brush:php;toolbar:false;'><input value="not empty">
<input><!-- empty --></pre><pre class='brush:php;toolbar:false;'>input:empty {
border: 1px solid pink;
}
input {
border: 1px solid black;
}</pre>
Expecting:
- If not Empty is black
- Hmm...from here on, you might think that
seems to be working, since what we see is a pink border. But this doesn't actually work The reason it's shown in pink is because pseudo-classes add specificity, similar to how class selectors (i.e.
) 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
to check if the input element is empty
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 " "
. <pre class='brush:php;toolbar:false;'><input placeholder=" "><!-- 传递空字符串 --></pre><pre class='brush:php;toolbar:false;'>input:placeholder-shown {
border-color: pink;
}</pre>
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-shown For :not
we can use
:not Pseudo class to do the opposite. Here, we can perform target operations when the input is not empty. <pre class='brush:php;toolbar:false;'><input placeholder="placeholder" value="not empty" /></pre><pre class='brush:php;toolbar:false;'>input:not(:placeholder) {
border-color: green;
}</pre><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/image/738/627/999/1625538639610588.gif?x-oss-process=image/resize,p_40" class="lazy" title="1625538639610588.gif" alt="What is :placeholder-shown in CSS? how to work? What is the use?"></p>
<p>结果:</p>
<ul>
<li>绿色,如果不为空,则表示用户已经输入了一些内容。</li>
<li>如果为空,则为黑色</li>
</ul>
<h3 id="strong-浮动标签-strong"><strong>浮动标签</strong></h3>
<p>使用占位符而不使用标签的问题之一就是无障碍,因为一旦你在打字的时候,占位符文字就没有了,这可能会导致用户的困惑。一个真正好的解决方案是浮动标签。最初,占位符文本显示时没有标签,而一旦用户开始输入,标签就会出现。这样一来,你仍然可以在不影响用户体验和可访问性的前提下,保持表单的简洁性。双赢</p>
<p>而这是可以用纯CSS实现的,我们只需要将 <code>placeholder-shown
与 :not
和 +
结合起来就可以了。这是一个超级简化版的浮动标签。
<input name="name" placeholder="Type name..." /> <label for="name">NAME</label>
label { display: none; position: absolute; top: 0; } input:not(:placeholder-shown) + label { display: block; }
浏览器支持
对 :placeholder-shown
的支持非常好!这包括Internet Explorer(是的,我和你一样惊讶)。但是,对于IE,你需要使用非标准名称 :-ms-input-placeholder
。
本文转载自: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!

两种方法:1、利用display属性,只需给元素添加“display:none;”样式即可。2、利用position和top属性设置元素绝对定位来隐藏元素,只需给元素添加“position:absolute;top:-9999px;”样式。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

自适应布局又称“响应式布局”,是指可以自动识别屏幕宽度、并做出相应调整的网页布局;这样的网页能够兼容多个不同的终端,而不是为每个终端做一个特定的版本。自适应布局是为解决移动端浏览网页而诞生的,能够为使用不同终端的用户提供很好的用户体验。

实现方法:1、使用“:active”选择器选中鼠标点击图片的状态;2、使用transform属性和scale()函数实现图片放大效果,语法“img:active {transform: scale(x轴放大倍数,y轴放大倍数);}”。

css3中的动画效果有变形;可以利用“animation:动画属性 @keyframes ..{..{transform:变形属性}}”实现变形动画效果,animation属性用于设置动画样式,transform属性用于设置变形样式。

在css3中,可以利用“animation-timing-function”属性设置动画旋转速度,该属性用于指定动画将如何完成一个周期,设置动画的速度曲线,语法为“元素{animation-timing-function:速度属性值;}”。

本篇文章带大家一起深入了解一下CSS3中的新特性::target-text 选择器,聊聊该选择器的作用和使用方法,希望对大家有所帮助!

css3线性渐变可以实现三角形;只需创建一个45度的线性渐变,设置渐变色为两种固定颜色,一个是三角形的颜色,另一个为透明色即可,语法“linear-gradient(45deg,颜色值,颜色值 50%,透明色 50%,透明色 100%)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
