假设我有一个容器:
<div id="container"> This is a red apple </div>
如何用红色颜色一个字“红”?
像(伪代码)
#container:[word="red"]{ color:red; }
是否可以在纯CSS中执行此操作,而不引入JavaScript或修改现有的HTML?
解决方法
不是为了证明一点,而是回答你的问题 – 这可能在没有JS:
Example的CSS中
简而言之:我们为文本颜色设置黑色背景颜色,并为特定的红色字符串设置红色背景图像.我们使用-webkit-text-fill-color删除原始文本填充.背景使用-webkit-background-clip:text;剪贴到文本大纲;并且红色图像的尺寸和位置位于我们想要着色的任何文本字符串上.
请注意:
我永远不会建议使用这个任何直播网站.这仅适用于webkit,因为它基于非标准的wekbit特定的CSS规则.颜色并没有真正束缚于彩色文本字符串 – 它是完全静态的.
编辑:
这是CSS:
#container { -webkit-text-fill-color: transparent; -webkit-background-clip: text; background-size: 1.5em 1em; background-repeat: no-repeat; background-position: 3.4em 0; background-color: #000; background-image: url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5rooor8DP9oD/2Q==); }