0

I want to make it harmonize with a default input color in my original component with HTML and JavaScript(or jQuery). Therefore, I'm looking into the method of getting a standard text selection color.

In CSS, though it is not official, it can change the color of a highlight of the text on modern browsers.

input::selection {
    background-color:red;
    color:blue;
}

I think it's effective to trace like this.

var hilightColor = $('input::selection').css('background-color');

Is this realizable?

Tank2005
  • 899
  • 1
  • 14
  • 32

2 Answers2

0

I think that the code you have up there is fine, but going for cross-browser compatibility, you should also include the moz library in your CSS code, as the default selection (I'm pretty sure) is unsupported by Firefox, while it is supported in Chrome and Safari.

input::selection {
    background-color: red;
    color: blue;
}

input::-moz-selection {
    background-color: red;
    color: blue;
}
0

Not sure what do you want.

1) If you want to harmonize with the text and input in your page, you can set the background and text color the same for both text and input.

See DEMO http://jsfiddle.net/yeyene/WLf9x/

html, body, input[type=text]{
    font-size:14px;
    line-height:16px;
    background:#fff;
    color:#555;    
}
input[type=text]{
    border:none;
    height:16px;
    width:85px;    
}

2) If you also want selection color the same for both input and text. You can set ::selection's background and text style the same as body's style.

yeyene
  • 7,297
  • 1
  • 21
  • 29