Purpose: to highlight only text, not space.
Selecting an entire element, for me, can lead to some uncontrollable and bad UX when large paddings, line heights, etc. are implemented. (spaces that are now highlighted). A task as simple as highlighting a block of text can, in turn, highlight other areas of a website that were not intended for the user. I am trying to resolve this on my current site, but I was only able to achieve it using the method below.
In which I use the inline element inside the block level element. Which, as you can see, can become very cumbersome and heavy code if used on a website. Is there a better way to achieve the second method?
I am open to Javascript solutions as well as CSS.
As far as I know (through testing), it is not processed otherwise if you copy + paste into a text or email application such as gmail. If you are aware of any problems that may arise, report it below.
To better illustrate:
With Improved Improvements:
Without Improvements Highlight:

^ Of course, this is half of the respondents, he demonstrates one example where he can come in handy, there are many others, believe me.
.highlight-text-only > *{
display:block;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none}
.highlight-text-only *>span,
.highlight-text-only *>strong{
display:inline;
-webkit-user-select: text;
-moz-user-select: text;
-ms-user-select: text;
-o-user-select: text;
user-select: text}
<div class="highlight-text-and-element">
<h3>Testing Text Selection Method 1 (default)</h3>
<div>of only text</div>
<a href="#"><strong>with</strong></a>
<p>highlighting</p>
<span>the actual elements</span>
</div>
<hr>
<div class="highlight-text-only">
<h3><span>Testing Selection Method 2</span></h3>
<div><span>of only text</span></div>
<a href="#"><strong>without</strong></a>
<p><span>highlighting</span></p>
<span><span>the actual elements</span></span>
</div>
Run codeHide result