Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the CSS pseudo-element :before{ content: '' } to affect an <option> element?

If I have a list of optons, how can I use the CSS pseudo-element :before{ content: '' } to affect the <option>? Below there are three options an I'd like to insert text before each one.

I know this can be done wit javascript / jQuery, but is it possible with CSS?

<html>
<head>
    <style>

        option::before {
            content: "sandy - "
        }

    </style>


</head>
<body>

<select>
    <option>beach</option>
    <option>field</option>
    <option>carpet</option>
</select>


</body>
</html>
like image 801
cwd Avatar asked Nov 20 '25 12:11

cwd


1 Answers

The ::before and ::after pseudo-elements actually prepend/append a child node to the element, so this will not work on any element that cannot contain child nodes.

It would be (roughly) the equivalent of doing:

<option><span>sandy - </span>beach</option>

If you want to update the text value, you will need to use JavaScript.

like image 200
Gary Chambers Avatar answered Nov 22 '25 01:11

Gary Chambers