I donβt think that this could be done using CSS, you will most likely need JavaScript to determine which elements the user has started to select.
What I did, I used the containsNode method from the selection API (The working draft may not work with all browsers and may be deprecated) to determine if the selected range contains the entire selected element or is missing at all. If the selection contains only part of the checked item, it will clear the selection using the removeAllRanges method.
function checkSelection () { var sel = window.getSelection() var marks = document.getElementsByTagName('mark') for (var i = 0; i < marks.length; i++) { if (sel.containsNode(marks[i], false) !== sel.containsNode(marks[i], true)) sel.removeAllRanges()
mark { background: yellow; }
This is <mark>marked text</mark>. I want to disable selecting only part of <mark>marked text</mark>.
Note that you will need to capture all the selection methods that you want, be it mouse events and touch events (all that I have included), keyboard carriage selection, or programmatic selection.
It can be expanded and customized to your liking, but this is my best chance to reproduce the behavior you want from your specifications. The above snippet does not work correctly all the time if one of the selection boundaries starts or ends on the edge of the marked area, but the snippet should demonstrate the basic concept of what should work, just with a little tweaking for the edge of the cases.
source share