Changing the highlight color when selecting text in an HTML text input

后端 未结 10 1106
有刺的猬
有刺的猬 2020-11-30 00:07

I\'ve been looking for this throughout the web and can\'t even find anyone else even asking this, let alone a solution...

Is there a way to change the color of the h

10条回答
  •  余生分开走
    2020-11-30 01:06

    All answers here are correct when it comes to the ::selection pseudo element, and how it works. However, the question does in fact specifically ask how to use it on text inputs.

    The only way to do that is to apply the rule via a parent of the input (any parent for that matter):

    .parent ::-webkit-selection, [contenteditable]::-webkit-selection {
    	background: #ffb7b7;
    }
    
    .parent ::-moz-selection, [contenteditable]::-moz-selection {
    	background: #ffb7b7;
    }
    
    .parent ::selection, [contenteditable]::selection {
    	background: #ffb7b7;
    }
    
    /* Aesthetics */
    input, [contenteditable] {
      border:1px solid black;
      display:inline-block;
      width: 150px;
      height: 20px;
      line-height: 20px;
      padding: 3px;
    }
    
    Content Editable

提交回复
热议问题