[译]用CSS来禁止文本选择_html/css_WEB-ITnose
原文:Disable Text Selection with CSS
有些时候我们需要禁止网页的部分文本不能被选择。你可以使用user-select这个CSS特性来实现这个需求。
举例
下面是一个使用了disable-selection类的样式规则,当它作用于一个HTML元素的时候,它会让我们不能够选择这个元素。
.disable-selection{ -moz-user-select:none; /* Firefox */ -ms-user-select:none; /* Internet Explorer */ -ktml-user-select:none; /* KHTML浏览器(比如:Konqueror) */ -webkit-user-select:none; /* Chrome,Safari,and Opera */ -webkit-touch-callout:none; /* Disable Android and IOS callout */}
关于这些样式的一些细节的解释:
-webkit-user-select是给Chrome,Safari和Opera用的(并不需要使用-o-user-select)。
没有前缀的user-select是被故意略去的。
-webkit-touch-callout属性可以让在移动设备上的触摸后弹出失效。就像下面的这些,我们可以让它们不能出现。
演示
查看演示
要时常记住的
有一个陷阱就是:user-select并不是W3C规范中标准的CSS特性。尽管user-select通过添加浏览器前缀有很好的浏览器支持。
前面的例子中,我没有使用没有前缀的user-select特性声明。那是因为在web标准中根本就没有这个属性。我们可以对它的使用类比于于IE专有的CSS属性ms-filter或者-ms-text-kashida-space的属性的使用。
其他需要注意的地方:
user-select是有问题并且是不稳定的。有些时候你依然还是可以选择文本,特别是当你从没有被屏蔽掉文本选择的文本的那部分开始选择。
使用全选快捷键有些时候还是会把屏蔽文本选择(Win:Ctrl+A/Mac:Cmd+A)。这种情况你可以在IE11中清楚的了解到。
这不是一种万全的保证文本不被选择的策略。CSS能够很容易被屏蔽。这种技巧依赖于非标准的CSS特性,这意味着未来对这个属性的可持续支持上面存在着很大的不确定性。
屏蔽掉文本选择是很恼人的。我会在渐进提升的过程中使用这个技巧:只有当它可以提高使用支持这个user-select特性的浏览器和设备的用户的用户体验的时候才使用它。但是,我绝不会把它设置成一个大范围的CSS选择器像全部选择器(*)或者body.
这个user-select特性可能会让你的样式表失效。如果遵循标准对你来说非常重要,使用这个属性会在你使用规范测试比如说CSS Validation Service的使用出现问题
浏览器支持
更新于:2015年3月
浏览器 | 版本支持(以上) |
---|---|
Chrome | 6 |
Firefox | 2 |
IE | 10 |
Safari | 3.1 |
移动端
浏览器 | 版本支持(以上) |
---|---|
Chrome(Android) | 2.1 |
Safari(IOS) | 3.2 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
