• 微信号
目录

html笔记

您当前的位置:首页 > 我的笔记 > html笔记>css伪元素

css伪元素

伪元素:用于向某些选择器设置特殊效果

:first-letter :向文本的第一个字母添加特殊样式

实例

<p> hello heml </p>
<style>
    p:first-letter { color:red; font-size:30px; }
</style>

:first-line :向文本的首行添加特殊样式

实例

<div>
    <p>两只黄鹂鸣翠柳,一行白鹭上青天</p>
    <p>窗含西岭千秋雪,门泊东吴万里船</p>
</div>
<style>
    div{width: 300px;}
    div:first-line { color:red;}
</style>

:before :在元素之前添加内容

实例

<p> hello before </p>
<style>
    p:before { content: 'before content' }
</style>

:after :在元素之后添加内容

实例

<p> hello after </p>
<style>
    p:after { content: 'after content' }
</style>

所有伪元素

选择器 例子 例子描述
::after p::after 在每个 <p> 元素之后插入内容。
::before p::before 在每个 <p> 元素之前插入内容。
::first-letter p::first-letter 选择每个 <p> 元素的首字母。
::first-line p::first-line 选择每个 <p> 元素的首行。
::selection p::selection 选择用户选择的元素部分。