'How to highlight HTML text without wrapping it with tags?
Is it possible to highlight text in an HTML document using without wrapping it with <span>
or any other tag for that matter?
For example, in the HTML code <p>The quick fox</p>
I would like to highlight quick
but without adding a DOM element around it. Adding a DOM element to a parent element is fine.
Thanks!
Solution 1:[1]
No, it is not possible.
You can't tell the browser to render a piece of text differently without inherently changing the DOM, regardless of whether you do it statically or dynamically (with Javascript, for example, as a post processing step).
Solution 2:[2]
It is possible if you use an absolutely positioned element with a transparent repeating background image or a transparent background color (using rgba or hsla) and position it over the selected area.
Another way to do it would be to have an absolutely positioned canvas element without a background that takes up the whole browser viewport and draw a transparent rectangle over the selection.
Solution 3:[3]
It's not possible.
If you just want no tags in the original source code, it might be possible by adding tags later using Javascript magic. You could do something like
<p highlight="quick">The quick fox</p>
and write a JQuery/Prototype/plain JS function to highlight it on the fly, but what for and why? If you elaborate a bit, someone may come up with an idea.
Solution 4:[4]
The only way to do this than I can imagine would be to use the <canvas>
element, and render absolutely everything by hand.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mike Atlas |
Solution 2 | Eli Grey |
Solution 3 | Pekka |
Solution 4 | August Lilleaas |