Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select text in ::before or ::after pseudo-element

Tags:

html

dom

css

Look this very simple code

<!DOCTYPE html>
<html>
<head>
<style>
p::before {
    content: "Before - ";
}
</style>
</head>
<body>
<p>Hello</p>
<p>Bye</p>   
</body>
</html>

Css adds ¨Before -" at the start of every <P> and renders like this

enter image description here

If you use your mouse to select the text (for copy paste) you can select the original text, but not the Before or Aftter text added by css.

enter image description here

I have a very specific requirement where I need to allow users to select with the mosue the Before text. How would you do it?

like image 874
Ricardo Polo Jaramillo Avatar asked Dec 10 '14 02:12

Ricardo Polo Jaramillo


People also ask

When would you use the :: before or :: after pseudo element in your CSS?

In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

How do you use before and after pseudo-elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What is :: before and :: after?

Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.


2 Answers

This cannot be done within an HTML page, but as a hack, if you need to copy/paste pseudo elements, you can export the page to PDF and copy from it. In Chrome, for example, you can copy page's content from print preview.

like image 154
Alex Avatar answered Oct 12 '22 02:10

Alex


You can't, the before and after pseudo classes are not meant to be used for that purpose.

like image 24
Eyal Avatar answered Oct 12 '22 01:10

Eyal