How to apply a background color to only half of the text on selecting?

后端 未结 4 904
忘掉有多难
忘掉有多难 2020-12-28 08:30

When I select text, the background color changes to yellow.

body p::selection {
  background: #fcf113;
  color: #000;
  display: none;
}
body p::-moz-selecti         


        
4条回答
  •  悲&欢浪女
    2020-12-28 09:25

    This is not possible with css alone (without hacks), currently you can only style a small set of properties for ::selection like color, background-color, cursor, outline, text-decoration, and text-shadow.

    Other background properties will be ignored so using a gradient isn't possible.


    If you really need the color as described you could use javascript to get the selected text, wrap it with a and style that with CSS.

    For small sentences or headlines, check out Roberrrts CSS only answer.

    Source:
    https://developer.mozilla.org/en-US/docs/Web/CSS/::selection
    https://drafts.csswg.org/css-pseudo-4/#highlight-styling

提交回复
热议问题