Is there any way to change one word color of placeholder

我的未来我决定 提交于 2019-12-11 01:04:30

问题


Suppose i have this text field

<input type="text" placeholder="I am placeholder">

I know with css we can change placeholder color like this but is there any way to change color of one word only.

::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  color: pink;
}
::-moz-placeholder { /* Firefox 19+ */
  color: pink;
}
:-ms-input-placeholder { /* IE 10+ */
  color: pink;
}
:-moz-placeholder { /* Firefox 18- */
  color: pink;
}

This code will change complete placeholder color but i want to change color of word placeholder only instead on complete I am placeholder


回答1:


You can take a look at mix-blend-mode :

label {
  display: inline-block;
  background: linear-gradient(to right, red 2.2em, blue 2.2em);
  border: inset;
  /* border here instead input */
  font-family: monospace;
  /* less surprise about length of text at screen */
}
input {
  box-shadow: inset 0 0 0 2em white;
  /* covers the background, needed for the blending */
}
input:invalid {
  /* color part of text only when placeholder is shown */
  mix-blend-mode: screen;
}
/* snippet disclaimer */

.disclaim {
  mix-blend-mode: screen;
}
body {
  background: white;
}
<label>
  <input placeholder="I am placeholder" required />
</label>
<p class="disclaim">not avalaible yet for <span>'your browser',</span> please be patient.</p>

Else you need to use HTML and text:

label {
  display: inline-block;
}
label>span {
  position: absolute;
  padding-left: 3px;
}
label>span span {
  color: blue
}
input {
  position: relative;
  background: white;
}
input:invalid {
  background: none;
}
<label>
  <span>I am <span>placeholder</span></span>
  <input type="text" required />
</label>



回答2:


You can't possibly do it with standard placeholder. Instead make a div and put your input element and one more child(say span/p element) inside this div and position span/p inside your input element and on focus hide the span/p element.

Something like this : link



来源:https://stackoverflow.com/questions/37699705/is-there-any-way-to-change-one-word-color-of-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!