How to disable text selection highlighting?

2018-12-30 23:26发布

For anchors that act like buttons (for example, Questions, Tags, Users, etc. at the top of the Stack Overflow page) or tabs, is there a CSS standard way to disable the highlighting effect if the user accidentally selects the text?

I realize this could be done with JavaScript, and a little googling yielded the Mozilla-only -moz-user-select option.

Is there a standard-compliant way to accomplish this with CSS, and if not, what is the "best practice" approach?

30条回答
闭嘴吧你
2楼-- · 2018-12-30 23:40

I have learned from CSS-Tricks website.

user-select: none;

And this also:

::selection{ background-color: transparent;}
::moz-selection{ background-color: transparent;}
::webkit-selection{ background-color: transparent;}
查看更多
几人难应
3楼-- · 2018-12-30 23:40
 -webkit-user-select: none;  
  -moz-user-select: none;    
  -ms-user-select: none;      
  user-select: none;
查看更多
梦醉为红颜
4楼-- · 2018-12-30 23:41

If you are using Less and Bootstrap you could write:

.user-select(none);
查看更多
千与千寻千般痛.
5楼-- · 2018-12-30 23:43

Add this to the first div in which you want to disable the selection for text:

onmousedown='return false;' 
onselectstart='return false;'
查看更多
余生请多指教
6楼-- · 2018-12-30 23:44

If you want to disable text selection on everything except on <p> elements, you can do this in CSS (watch out for the -moz-none which allows override in sub-elements, which is allowed in other browsers with none):

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}

p {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
}
查看更多
无与为乐者.
7楼-- · 2018-12-30 23:44

Try insert this rows to CSS and call the "disHighlight" at class property:

.disHighlight{
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -o-user-select: none;
    -moz-user-select: none;
}
查看更多
登录 后发表回答