Styling part of the text in the placeholder

狂风中的少年 提交于 2019-12-20 06:02:07

问题


I would like to modify the css style of a part of placeholder. Attention not all the placeholder.

If my placeholder is: "Where (example: New York)"

<label>Search:</label>
<textarea placeholder="Where (example: New York)" required="required">
</textarea>

Can i set as italic only (example: New York)?

For all the placeholder look here


回答1:


You may want to think about what the role and meaning of placeholders is. Most UI experts agree: placeholders are not labels. Labels (like "Search") are labels, and should be outside the text area. The placeholder is designed to be a sample input, or a format, not a label, nor instructions. To the extent that you use the placeholder in label-like fashion, that information will disappear as soon as the user starts typing.

For instance, in an input field for a phone number, the label would be "Phone Number", and the placeholder might be "212-555-1234". In your case, "Search" should be the label, and "brown fox" the placeholder. If you want to make that entire placeholder italics (but why?), you can do that easily enough with the placeholder pseudo-elements.




回答2:


Yes (ish)... but I wouldn't recommend it.

In some browsers you can use the :before pseudo element on the placeholder and style that separately but you'll have to make the 'Search' prefix part of the pseudo element rather than in the actual placeholder attribute:

::-webkit-input-placeholder {
   color: #ABABAB;
   font-style: italic;
}
:-moz-placeholder { /* Firefox 18- */
   color: #ABABAB;
   font-style: italic;
}
::-moz-placeholder {  /* Firefox 19+ */
   color: #ABABAB;
   font-style: italic;
}
:-ms-input-placeholder {  
   color: #ABABAB;
   font-style: italic;
}
/* prepend the 'Search' prefix */
::-webkit-input-placeholder:before {
    content: "Search ";
    font-style: normal;  
}
:-moz-placeholder:before {
    content: "Search ";
    font-style: normal;  
}
::-moz-placeholder:before {
    content: "Search ";
    font-style: normal;  
}
:-ms-input-placeholder:before {
    content: "Search ";
    font-style: normal;  
}
<textarea placeholder="(example: brown fox)" required="required"></textarea>

Works for me in Chrome but your mileage may vary. I've added the vendor-prefixes just in case but I've not tested it in anything else. As noted in the comments - it doesn't work in Firefox, and probably shouldn't work anywhere! Furthermore, it is considered bad-form to style the placeholders this way as they are not really presentational elements but just kind of helpers to inform users of what's expected.

A better solution would be to NOT style the placeholders at all aside from giving them a font/colour to suit your theme.

See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-placeholder.

A good form will use label, placeholder and title in combination to clearly describe the expected input. Modern browsers will also use the title attribute's value in the required field dialogue when you try to submit an invalid required field. Try this example:

<form>
    <label>Search:
        <input name="searchterms" 
               type="search" 
               placeholder="keyword/s" 
               required="required" 
               title="Space-separated keywords only" />
    </label>
    <input type="submit" value="go" />
</form>


来源:https://stackoverflow.com/questions/28050249/styling-part-of-the-text-in-the-placeholder

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