2 colors in one placeholder of input field
Here is a cross-browser solution that does not use Javascript:
Live demo
Inline elements such input
do not support :before
and :after
. To make things even harder the placeholder selector and their pseudo-classes are not fully supported by all browsers, as you found out.
So, the workaround is to add a label
placed relatively on top of the input box with a for
attribute pointing to the input text box. This way, when user clicks the label (fake placeholder) the focus goes into the input box underneath.
Replace your class="required"
by the attribute required="required"
. This gives you an opportunity to use the :invalid and :valid selectors, and also lets the browser display a validation error, when the form is submitted with empty required fields.
input {
width: 160px;
}
input[type=submit] {
width: auto;
}
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;
}
<form>
<input type="text" id="name" name="name" required="required" />
<label for="name">Name</label>
<br/>
<input type="email" id="email" name="email" placeholder="Email" />
<br/>
<input type="submit" />
</form>
Different colors in placeholder
The appearance of the placeholder
property of <input>
elements is managed by the browser and it cannot be separated into two separate elements, which styling just the asterisk would require, nor can it be easily styled as such.
If you wanted to accomplish this, you would likely need to use something to explicitly override the element with a <div>
that contained your <span>Name</span><span style='color:red'>*</span>
content to overlay on your <input>
element itself similar to the scenario mentioned in this related discussion :
.input-placeholder { position: relative;}.input-placeholder input { padding: 2px;}.input-placeholder input:valid + .placeholder { display: none;}.placeholder { position: absolute; pointer-events: none; top: 2px; bottom: 2px; left: 6px; margin: auto; color: #ccc;}.placeholder span { color: red;}
<div class="input-placeholder"> <input type="text" required> <div class="placeholder"> Name<span>*</span> </div></div>
Is it possible multiple text color in a single placeholder?
No, it is not possible to color the default placeholder but you can create a element similar to placeholder. So, that you can color the letters. This is a workaround to the default placeholder.
Note that I am using opacity: 0.5
, you can change it as per your need.
HTML
.input-field { position: relative; display: inline-block;}.input-field > label { position: absolute; left: 0.5em; top: 50%; margin-top: -0.5em; opacity: 0.5;}.input-field > input[type=text]:focus + label { display: none;}.input-field > label > span { letter-spacing: -2px;}.first-letter { color: red;}.second-letter { color: blue;}.third-letter { color: orange;}.fourth-letter { color: green;}.fifth-letter { color: yellow;}
<div class="input-field"> <input id="input-text-field" type="text"></input> <label for="input-text-field"> <span class="first-letter">H</span> <span class="second-letter">E</span> <span class="third-letter">L</span> <span class="fourth-letter">L</span> <span class="fifth-letter">O</span> </label> </div>
Apply multiple styles to a single placeholder in an input element
The only solution i see is to avoid the use of placeholder and replace its behaviour with javascript. The old-style way!
See this demo
HTML
<div class="location">
<span class="holder">Location <span class="blue">or</span> Place</span>
<input id="input" size="18" type="text" />
</div>
Javascript
$(function() {
$("span.holder + input").keyup(function() {
if($(this).val().length) {
$(this).prev('span.holder').hide();
} else {
$(this).prev('span.holder').show();
}
});
$("span.holder").click(function() {
$(this).next().focus();
});
});
CSS
div.location > span.holder {
position: absolute;
margin: 5px 8px;
color: #ddd;
cursor: auto;
font-family: Helvetica;
font-size: 11pt;
z-index: 1;
}
div.location > span.holder > span.blue{
color: #819FF7;
}
div input {
padding:5px;
font-size:11pt;
background-color: #0B7DAB;
color: white;
border-radius:15px;
-moz-border-radius:15px;
-webkit-border-radius:15px;
border: none;
}
how to change the placeholder text color in two different areas (div)in a web page
try this code
.fname::-webkit-input-placeholder { color: red}
<div class="content"> <input type="text" name="firstname" class="fname" placeholder="placeholder"></div><input type="text" name="lastname" class="lname" placeholder="placeholder">
How to add different placeholder color
you need to be more specified:
if you put ::-webkit-input-placeholder
in the CSS, you will select all placeholders... but this is not what we want
so using :nth-of-type();
a pseudo-class, you can select the wanted one, easily.
useful documentations:
nth-of-type
MDN documentation
so the code will be like this:
/* 1 */
input:nth-of-type(1)::-webkit-input-placeholder {
color: green;
}
/* 2 */
input:nth-of-type(2)::-webkit-input-placeholder {
color: blue;
}
/* 3 */
input:nth-of-type(3)::-webkit-input-placeholder {
color: red;
}
<!-- 1 -->
<input type="text" name="" id="" placeholder="hello world">
<!-- 2 -->
<input type="text" name="" id="" placeholder="hello world">
<!-- 3 -->
<input type="text" name="" id="" placeholder="hello world">
Multiple styles for input placeholder text
You have to combine the class with the pseudo element, and then you can apply a class to the input elements:
input::-webkit-input-placeholder { font-size: 16pt; color: #555; }input::-moz-placeholder { font-size: 16pt; color: #555; }input:-ms-input-placeholder { font-size: 16pt; color: #555; }input:-moz-placeholder { font-size: 16pt; color: #555; }
input.other::-webkit-input-placeholder { font-size: 12pt; color: red; }input.other::-moz-placeholder { font-size: 12pt; color: red; }input.other:-ms-input-placeholder { font-size: 12pt; color: red; }input.other:-moz-placeholder { font-size: 12pt; color: red; }
<input type="text" placeholder="Hello"></input><input type="text" class="other" placeholder="Hello"></input>
Change a part of the text placeholders color
There could be a solution via mix-blend-mode.
https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode
http://caniuse.com/#search=mix-blend-mode
It will not be avalaible everywhere and requires some tuning.
example:
label { display: inline-block; background: linear-gradient(to right, red 8em, blue 5em); border: inset;/* border here instead input */ font-family: monospace;/* less surprise about length of text at screen */}input { font-weight: bold; width: 25em; border: none; display: block; 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;}
<label> <input placeholder="New Password (leave blank to leave unchanged)" required /></label>
Related Topics
Why Is The First Element Always Blank in My Rails Multi-Select, Using an Embedded Array
Why Does "Position: Absolute; Left: 0; Right: 0; Width: Xypx; Margin: 0 Auto" Actually Center
How to Center Multiple Divs Inside a Container in CSS
Insert HTML Code Inside Svg Text Element
CSS Grid Row Height Safari Bug
What Is Aria-Label and How to Use It
What Is a Non-Replaced Inline Element
Using: After to Clear Floating Elements
HTML5 Flexible Box Model Height Calculation
How to Make Bootstrap Carousel Image Responsive
Inline Form Nested Within Horizontal Form in Bootstrap 3
Same Height Column Bootstrap 3 Row Responsive
Mobile Website "Whatsapp" Button to Send Message to a Specific Number
How to Have Two Items on Opposite Sides on The Same Line
Iframe Contents Cant Appear in Firefox
Select Label Using CSS Selector