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

后端 未结 10 1052
有刺的猬
有刺的猬 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 00:49

    It seems like when you define the border inside of a focus pseudo element style declaration it uses that instead of the normal blue border. Using that you can define a style that is exactly the same as the element border.

    input:focus, textarea:focus {
        border:1px solid gray;
    }
    
    #textarea  {
      position:absolute;
      top:10px;
      left:10px;
      right:10px;
      width:calc(100% - 20px);
      height:160px;
      display:inline-block;
      margin-top:-0.2em;
    }
    <textarea id="textarea">yo</textarea>

    Here is a modified border style:

    input:focus, textarea:focus {
        border:2px dotted red;
    }
    
    #textarea  {
      position:absolute;
      top:10px;
      left:10px;
      right:10px;
      width:calc(100% - 20px);
      height:160px;
      display:inline-block;
      margin-top:-0.2em;
    }
    <textarea id="textarea">yo</textarea>

    0 讨论(0)
  • 2020-11-30 00:49

    Try this code to use:

    /* For Mozile Firefox Browser */
    
    ::-moz-selection { background-color: #4CAF50; }
    
    /* For Other Browser*/
    ::selection { background-color: #4CAF50; }
    
    0 讨论(0)
  • 2020-11-30 00:53

    I guess this can help :

    selection styles

    It's possible to define color and background for text the user selects.

    Try it below. If you select something and it looks like this, your browser supports selection styles.

    This is the paragraph with normal ::selection.

    This is the paragraph with ::-moz-selection.

    This is the paragraph with ::-webkit-selection.

    Testsheet:

    p.normal::selection {
      background:#cc0000;
      color:#fff;
    }
    
    p.moz::-moz-selection {
      background:#cc0000;
      color:#fff;
    }
    
    p.webkit::-webkit-selection {
      background:#cc0000;
      color:#fff;
    }
    

    Quoted from Quirksmode

    0 讨论(0)
  • 2020-11-30 00:54

    I realise this is an old question but for anyone who does come across it this can be done using contenteditable as shown in this JSFiddle.

    Kudos to Alex who mentioned this in the comments (I didn't see that until now!)

    0 讨论(0)
  • 2020-11-30 00:54

    Thanks for the links, but it does seem as if the actual text highlighting just isn't exposed.

    As far as the actual issue at hand, I ended up opting for a different approach by eliminating the need for a text input altogether and using innerHTML with some JavaScript. Not only does it get around the text highlighting, it actually looks much cleaner.

    This granular of a tweak to an HTML form control is just another good argument for eliminating form controls altogether. Haha!

    0 讨论(0)
  • 2020-11-30 01:01

    If you are looking for this:

    Here is the link:

    http://css-tricks.com/overriding-the-default-text-selection-color-with-css/

    0 讨论(0)
提交回复
热议问题