I am trying to use Less as efficient as possible. Now I want to replace the color of a placeholder, which I normally in CSS would do like this:
input::-webkit-input-placeholder /* WebKit, Blink, Edge */
{
color: #000000;
}
input:-moz-placeholder /* Mozilla Firefox 4 to 18 */
{
color: #000000;
}
input::-moz-placeholder /* Mozilla Firefox 19+ */
{
color: #000000;
}
input:-ms-input-placeholder /* Internet Explorer 10-11 */
{
color: #000000;
}
Now I thought using nested selectors in Less I could use:
input{
&::-webkit-input-placeholder, /* WebKit, Blink, Edge */
&:-moz-placeholder, /* Mozilla Firefox 4 to 18 */
&::-moz-placeholder, /* Mozilla Firefox 19+ */
&:-ms-input-placeholder /* Internet Explorer 10-11 */
{
color: #000000;
}
}
Unfortunately that does not work like I expected. When I only use one selector (without the comma's) it works fine, but that means I would still have to make four nested selectors for each prefix, which is not efficient. How can I accomplish the effect of the first CSS block in Less with the less possible lines?
Note: the full code block is more extensive, with more nested rules. Of course for this example I could just comma all the selectors with just CSS - but I want it to work in a nested Less-selector.
Disclaimer: As always I don't recommend using Less mixins for vendor prefixing stuff. They are best left to libraries like prefix-free or Auto-prefixer. This answer is just to show how similar things can be handled using Less.
Like you've already found out (and mentioned in comments), grouping of vendor prefixed selectors will not work because the User Agent will drop the entire rule when it comes across a selector that it does not understand. You can read more about it in this answer.
This is not a problem with the Less compiler. It will compile and output the code as expected.
One way to avoid writing the four selector blocks again and again would be to put the vendor prefixed selectors into a mixin which accepts a ruleset as argument and then call it wherever required. Below is a sample code for your reference.
.placeholder(#rules){ /* no need to repeat, just copy paste this once in your code */
&::-webkit-input-placeholder /* WebKit, Blink, Edge */
{
#rules();
}
&:-moz-placeholder /* Mozilla Firefox 4 to 18 */
{
#rules();
}
&::-moz-placeholder /* Mozilla Firefox 19+ */
{
#rules();
}
&:-ms-input-placeholder /* Internet Explorer 10-11 */
{
#rules();
}
}
/* call it wherever required */
input{
.placeholder({
color: red;
})
}
input.somethingelse{
.placeholder({
color: black;
padding: 4px;
})
}
Related
In my jQuery v3 / Bootstrap v4.1.2 application, I use chosen.jquery (Version 1.8.7) and I did not find how to set color of placeholder text of of Chosen selection input with styles like:
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #c2c200 !important;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #c2c200 !important;
opacity: 1 !important;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #c2c200 !important;
opacity: 1 !important;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: ##c2c200 !important;
}
::-ms-input-placeholder { /* Microsoft Edge */
color: #c2c200 !important;
}
::placeholder { /* Most modern browsers support this now. */
color: ##c2c200 !important;
}
And I init it with code:
$(".chosen_select_box").chosen({
disable_search_threshold: 10,
allow_single_deselect: true,
no_results_text: "Nothing found!",
});
You can look at it this fiddle:
http://jsfiddle.net/1Lpdk79r/3/
But it does not work for chosen input. How to fix it?
If you view the source of your fiddle, you can see that the Chosen plugin generates a text input from your select, and adds the placeholder text as the input value. It also styles that input. So you just need to override the Chosen styling with your own.
This will do, to change text colour:
.chosen-container-multi .chosen-choices li.search-field input[type="text"] {
color: #c2c200;
}
Here's an updated version of your fiddle showing the result.
I'm styling placeholder text, and need to use several vendor-prefixed selectors so that it works in different browsers. When I put each of them as a separate code block, it works. However, if I use a comma-separated list of selectors instead of repeating the same CSS for each of them, it won't work. Can anyone explain?
This works:
input[type=text]::-webkit-input-placeholder {
color: green;
}
input[type=text]::-moz-placeholder {
color: green;
}
input[type=text]:-ms-input-placeholder {
color: green;
}
input[type=text]:-moz-placeholder {
color: green;
}
<input type="text" placeholder="Placeholder Text" />
But this doesn't:
input[type=text]::-webkit-input-placeholder,
input[type=text]::-moz-placeholder,
input[type=text]:-ms-input-placeholder,
input[type=text]:-moz-placeholder {
color: green;
}
<input type="text" placeholder="Placeholder Text" />
Why?
Unfortunately, you can't.
When a selector that the browser does recognise as valid is found, it stops execution of the code block following it.
Only one of the vendor-prefixed selectors you are using will exist in each browsers (for example WebKit browsers do not have the Mozilla and Microsoft vendor-prefixed selectors); therefore you will never be able to execute that block as there is no browser where all three pseudo-selectors are valid.
However...
... you can simply use three different blocks. For example, this should work:
input[type=text]:focus::-webkit-input-placeholder {
color: green;
}
input[type=text]:focus::-ms-input-placeholder {
color: green;
}
input[type=text]:focus::-moz-placeholder {
color: green;
}
<input type="text" placeholder="Hello, world!">
If you have a lot of code, you could use a preprocessor like LESS or SASS to dynamically put the same code inside each block.
The reason why you can't group these selectors is because as soon as a browser comes across an unknown selector it stops execution for that block of code.
Vendor-specific selectors are only known to the browser that supports them. If you group them, every browser will stop executing that block of code either at the first selector in the group or at the second.
In this example:
input[type=text]::-webkit-input-placeholder, /* Chrome / Opera / Safari */
input[type=text]::-moz-placeholder, /* Firefox 19+ */
input[type=text]:-ms-input-placeholder, /* Edge/IE 10+ */
input[type=text]:-moz-placeholder { /* Firefox 18- */
color: green;
}
Google Chrome, Safari, and Opera will recognize the first selector, but they will stop executing this block of code at the second selector, which is only valid in a Firefox browser. The other browsers will stop execution at the very first selector.
Therefore each of these selectors must have their own block of code.
Is there a way to Write a snippet of CSS that will display a paragraph in blue in older browsers, red in newer browsers, green in IE6 and black in IE7.
From My side I make this snipped but not sure it will work, Is any one know another snippet to make it work.
#content p{color:blue}
html>body #content p {color:red}
* html #content p{color:green}
html>body #content p {*color:black;}
This is a trip down horrible memory lane:
* p { color/**/:blue; } /*IE5*/
* html p { color: green; } /*IE6*/
*:first-child+html p { color: black; } /*IE7*/
p { color: red; } /*The rest*/
Here's a reference I've fortunately not had to refer to for years: https://css-tricks.com/snippets/css/browser-specific-hacks/
If you have to support IE 5 in any way you have my deepest sympathies.
This may help-
html body #content p {
color: red; /* all browsers including Mac IE */
*color: black; /* IE 7 and below */
_color/**/: blue; /* IE 5.0 */
_color:/**/ blue; /* IE 5.5 only */
_color/**/:/**/ green; /* IE 6 only */
}
I am trying to set the color (font-color) attribute for the placholder pseudo class for a multiple classes of inputs.
(So I want all inputs with class .red-va or .blue-va to have placeholder text of a given color)
I can (and have) done this:
.red-va::-webkit-input-placeholder {
color: white;
}
.red-va:-moz-placeholder { /* Firefox 18- */
color: white;
}
.red-va::-moz-placeholder { /* Firefox 19+ */
color: white;
}
.red-va:-ms-input-placeholder {
color: white;
}
.blue-va::-webkit-input-placeholder {
color: white;
}
.blue-va:-moz-placeholder { /* Firefox 18- */
color: white;
}
.blue-va::-moz-placeholder { /* Firefox 19+ */
color: white;
}
.blue-va:-ms-input-placeholder {
color: white;
}
Basically two sets of CSS for each input class, with browser support requiring four different approaches for each.
Is there a more elegant / streamlined way of doing this?
Unfortunately, without making use of a preprocessor (since this is CSS), the best you can do is to group each set of vendor prefixes for both .red-va and .blue-va, but not all of them into a single ruleset:
.red-va::-webkit-input-placeholder, .blue-va::-webkit-input-placeholder {
color: white;
}
.red-va:-moz-placeholder, .blue-va:-moz-placeholder { /* Firefox 18- */
color: white;
}
.red-va::-moz-placeholder, .blue-va::-moz-placeholder { /* Firefox 19+ */
color: white;
}
.red-va:-ms-input-placeholder, .blue-va:-ms-input-placeholder {
color: white;
}
Or if you can afford to change the markup you can go further by adding a common class to both .red-va and .blue-va so you don't have to duplicate your selectors for both — effectively halving the CSS that you currently have.
The reason you can't group them all into one ruleset is covered here. In short, it's because browsers are required to drop an entire ruleset if they don't recognize any part of the selector list, which will be caused by the vendor prefixes (and in the case of Firefox, also by the fact that versions older than 19 don't recognize the pseudo-element syntax).
Thankfully, prefixed pseudos will soon be a thing of the past.
I was playing around right-clicking my line numbers in Dreamweaver, trying to bookmark a line.
I noticed this menu entry:
What is the Caio Hack? With alert('hi'); highlighted, this is the result:
It's an old CSS hack.
"Caio Hack is a simple CSS comments-based hack used in 'inline' and
'external' CSS declarations to hide information from Netscape 4"
For example, this code hides the .foo2 selector to Netscape 4:
.foo1
{
color: green;
background-color: yellow;
}
/*/*/
.foo2
{
color: red;<code></code>
}
/* */
.foo3
{
color: blue;
}
Source: http://en.wikibooks.org/wiki/Cascading_Style_Sheets/Hacks_and_Filters/Caio_Hack
The Caio Hack is named after its discoverer, Caio Chassot.
It is a CSS hack for hiding rules from Netscape 4, exploiting a bug in NN4's parser.
By opening a comment using /*/*/, all subsequent CSS would be ignored by NN, because it thinks the comment didn't close.
To end the block, you'd use /* */.
For example:
.foo1
{
color: green;
background-color: yellow;
}
/*/*/
.foo2
{
color: red;
}
/* */
.foo3
{
color: blue;
}
More info at:
Wikibooks
CSS Discuss
http://css-discuss.incutio.com/wiki/Caio_Hack
It hides CSS codes from Netscape4 and sometimes Opera.