Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textarea::selection and ::-moz-selection

Tags:

css

selection

Is there a way to apply the ::selection and ::-moz-selection CSS pseudo-elements to the text inside a textarea?

textarea::selection {
    color: #ff0000;
}

Isn't working

like image 966
Trevor Avatar asked Jan 11 '10 17:01

Trevor


People also ask

What is ::- Moz selection?

CSS ::selection Selector You must have to write both rule ::selection and ::-moz-selection to apply CSS. -moz is prefix of Mozilla Firefox browser. Still this ::selection selector not release on CSS2 or CSS3, so is not officially. Only some CSS properties are used inside a ::selection declaration block.

What does:: selection do in CSS?

The ::selection selector matches the portion of an element that is selected by a user. Only a few CSS properties can be applied to the ::selection selector: color, background, cursor, and outline.

How do you change the background color of selected text in CSS?

You can change the background color and color of selected text by styling ::selection . Styling this pseudo element is great for matching user-selected text to your sites color scheme.

What is webkit user-select?

The user-select property enables authors to specify which elements in the document can be selected by the user and how. Chrome has supported only prefixed version: -webkit-user-select.


2 Answers

According to this, it should work.

Can you try giving it an !important?

Can you try applying it to a different element than a textarea, e.g. a div? If it works there, it works differently for input elements - but I can't find any resources mentioning that.

like image 197
Pekka Avatar answered Sep 21 '22 15:09

Pekka


Updated as you said here, I'll update this question too:

I disagree. It is working ;)
Tested on Firefox 4.0b6

I can confirm the following code works at least under Firefox 4.0b6 (Taken from my own answer)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<style type="text/css">

p::-moz-selection, input::-moz-selection, textarea::-moz-selection {
    color: red;
    background-color: grey;
}
</style>
</head>

<body>
<p>This paragraph is selection-aware.</p>
<form action="#">
<input type="text" id="itext" value="So is this input[text]" />
<textarea id="itextarea">And this textarea, as well</textarea>
</form>
</body>
</html>

It may not be wroking programmatically, if you are not careful with what you are doing. Maybe you are trying to select something with jQuery.select() and you are selecting the textarea object instead of its content.

like image 45
ssice Avatar answered Sep 18 '22 15:09

ssice