简体   繁体   English

仅更改输入占位符*颜色

[英]Change input placeholder * color only

I have multiple input fields some fields are required and required field mention with * so I need to change input placeholder * color only, not change whole placeholder color check below image what I exactly need. 我有多个输入字段required一些字段,需要字段提及*所以我需要更改输入占位符*颜色, 而不是更改整个占位符颜色检查下面的图像我真正需要的。

在此输入图像描述
I have tried below code to achieve this but it can change whole color of placeholder 我尝试过以下代码来实现这一点,但它可以改变占位符的整个颜色

 div { margin:10px 0px; } input { width: 200px; border: none; border-bottom: solid 1px #8d97a0; padding: 5px; border-radius: 0; background: #fff; box-shadow: none; } ::-webkit-input-placeholder { color:red; } 
 <div> <input type="name" name="name" id="name" placeholder="Name *"> </div> <div> <input type="Email" name="email" id="email" placeholder="Email"> </div> <div> <input type="phone" name="phone" id="phone" placeholder="Phone"> </div> <div> <input type="password" name="password" id="password" placeholder="Password *"> </div> 

Check this...You can achieve your requirement using :before and :after and for input tag there is no support of :before and :after . 检查一下......你可以使用:before and :after和input输入标签来实现你的要求,不支持:before and :after So you can do it by adding label to your input and give :before and :after CSS to label 所以你可以通过在输入中添加标签来实现它, :before and :after标记:before and :after给出:before and :after

 input { width: 160px; } input[required] + label { color: #999; font-family: Arial; font-size: .8em; position: relative; left: -166px; /* the negative of the input width */ } input[required] + label:after { content:'*'; color: red; } /* show the placeholder when input has no content (no content = invalid) */ input[required]:invalid + label { display: inline-block; } /* hide the placeholder when input has some text typed in */ input[required]:valid + label{ display: none; } 
 <input type="password" id="name" name="name" required="required" /> <label for="name">Password</label> 

Just placeholder wont give different styles .If you want different style you need to separate like this. 只是placeholder不会给出不同的styles 。如果你想要不同的风格,你需要像这样分开。

 input { width: auto; } input + label { color: #999; font-family: Arial; font-size: .8em; position: relative; left: -166px; } input[required] + label:after { content:'*'; color: red; } input[required]:invalid + label { display: inline-block; } input[required]:valid + label{ display: none; } 
 <div> <input type="text" id="name" name="name" required="required" /> <label for="name">Password</label> </div> 

See This: 见:

 $(document).ready(function(){ $('.colorHolder').click(function(){ $('.havePlace',this).focus(); }) $('.havePlace').on('input',function(){ var len = ($(this).val()).length; if (len) $(this).next('label').hide(); else $(this).next('label').show(); }) }) 
 input { width: 150px; border: none; border-bottom: solid 1px #8d97a0; padding: 5px; border-radius: 0; background: #fff; box-shadow: none; outline: none; } .colorHolder { position: relative; margin-bottom: 20px; display:inline-block; } .colorHolder .havePlace + label { position: absolute; left: 7px; bottom: 2px; color: #CCC; cursor: text; } .colorHolder .havePlace + label:after { content: '*'; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="colorHolder"> <input type="password" name="password" class="havePlace"> <label>Password </label> </div> <div class="colorHolder"> <input type="email" name="email" class="havePlace"> <label>Email </label> </div> <div class="colorHolder"> <input type="text" name="text" class="havePlace"> <label>Text </label> </div> 

maybe you can use span in the placeholder so you can change the color of * 也许你可以在占位符中使用span,这样你就可以改变*的颜色

 $(function() { $(".holder + input").keyup(function() { if($(this).val().length) { $(this).prev('.holder').hide(); } else { $(this).prev('.holder').show(); } }); $(".holder").click(function() { $(this).next().focus(); }); }); 
 .holder { position: absolute; margin: 5px 5px; cursor: auto; font-size: 11pt; z-index: 1; } .red{ color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="holder">Email Adress <span class="red">*</span></div> <input id="input" size="18" type="text" /> 

credits: Add span inside form's placeholder 信用: 在表单的占位符中添加span

Try this: 试试这个:

 label { display: inline-block; width:100%; background: linear-gradient(to right, #999 6em, red 5em); border: 2px solid #ccc; font-family: monospace;/* less surprise about length of text at screen */ } input { font-weight: bold; width: 100%; height:20px; padding:10px; border: none; display: block; outline:none; } input:invalid {/* color part of text only when placeholder is shown */ mix-blend-mode: screen; } 
 <label> <input placeholder="Password *" required /> </label> 

With the current markup I don't think this is possible with just CSS, but: 使用当前标记我不认为这只能用CSS,但是:

This is possible if it's ok to add a label element after the input - like so: 是可能的 ,如果它是确定以添加一个label之后输入元素-像这样:

<div>
  <input type="password" name="password" id="password" placeholder=" " />
  <label for="password">Password</label>
</div>

Codepen demo Codepen演示

The trick here is to use the pseudo class: 这里的技巧是使用伪类:

:placeholder-shown ( caniuse ) :占位符显示caniuse

From the spec : 规格

Input elements can sometimes show placeholder text as a hint to the user on what to type in. See, for example, the placeholder attribute in [HTML5]. 输入元素有时可以显示占位符文本作为用户提示输入内容的提示。例如,请参阅[HTML5]中的占位符属性。 The :placeholder-shown pseudo-class matches an input element that is showing such placeholder text. :placeholder-shown伪类匹配显示此类占位符文本的输入元素。

The idea here is for the text " Password * " to simulate placeholder text. 这里的想法是文本“ 密码* ”来模拟占位符文本。

Placeholders are always shown on input elements - even if they are focused - unless a value is set on them. 占位符始终显示在input元素上 - 即使它们是聚焦的 - 除非在它们上设置了值。

By using the :placeholder-shown pseudo class on the input - we can determine when to display the label text. 通过在input上使用:placeholder-shown伪类 - 我们可以确定何时显示标签文本。

This is the key selector: 这是关键选择器:

input:placeholder-shown + label {
  display: block;
}

This means: when the input's placeholder is shown - show the "placeholder" label. 这意味着:当显示输入的占位符时 - 显示“占位符”标签。 (otherwise we hide it) (否则我们隐藏它)

For this reason we still need to set a non-empty placeholder attribute on the input (a simple whitespace is sufficient) 出于这个原因,我们仍然需要在输入上设置一个非空的占位符属性(一个简单的空格就足够了)

 div { position: relative; } label { position: absolute; height: 20px; left: 5px; top: 0; bottom: 0; margin: auto; display: none; pointer-events: none; color: #757575; font-size: 16px; font-family: system-ui; } label:after { content: " *"; color: red; } input:placeholder-shown + label { display: block; } input { width: 200px; border: none; border-bottom: solid 1px #8d97a0; padding: 5px; border-radius: 0; background: #fff; box-shadow: none; height: 20px; font-size: 16px; margin: 5px 0; } 
 <div> <input type="password" name="name" id="name" placeholder=" " /> <label for="name">Name</label> </div> <div> <input type="password" name="name" id="name" placeholder="Email" /> </div> <div> <input type="password" name="name" id="name" placeholder="Phone" /> </div> <div> <input type="password" name="password" id="password" placeholder=" " /> <label for="name">Password</label> </div> 

Codepen demo Codepen演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM