🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## :before 选择器 ### 定义和用法 :before 选择器向选定的元素前插入内容。 :before是伪元素,并且它生成包含放置在元素中的内容之前的生成内容的伪元素。 使用[content](https://www.w3cschool.cn/cssref/pr-gen-content.html) 属性来指定要插入的内容。 默认情况下,生成的伪元素是内联的,但这可以使用属性显示更改。 **例子:** ~~~ a[href]:before {content: "[LINK]";) p:before {content: attr(class);} ~~~ ## 实例1--每个 元素之前插入内容: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>W3Cschool教程(w3cschool.cn)</title> <style> p:before { content:"Read this -"; } </style> </head> <body> <p>My name is Donald</p> <p>I live in Ducksburg</p> <p><b>注意:</b> :before作用于 IE8,DOCTYPE必须已经声明.</p> </body> </html> ``` ## 实例2--在每个之前插入的内容和样式: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>W3Cschool教程(w3cschool.cn)</title> <style> p:before { content:"Read this -"; background-color:yellow; color:red; font-weight:bold; } </style> </head> <body> <p>My name is Donald</p> <p>I live in Ducksburg</p> <p><b>注意:</b> :before 作用于 IE8, DOCTYPE必须已经声明.</p> </body> </html> ```