zoukankan      html  css  js  c++  java
  • CSS 的伪元素是什么?

    CSS伪元素是用来添加一些选择器的特殊效果。用于:向某个选择器中的文字的首行。

    ㈠语法

    ①伪元素的语法:

    selector:pseudo-element {property:value;}

    ②CSS类也可以使用伪元素

    selector.class:pseudo-element {property:value;}

    ㈡:first-line 伪元素

    ⑴"first-line" 伪元素用于向文本的首行设置特殊样式。

    ⑵"first-line" 伪元素只能用于块级元素。

    ⑶下面的属性可应用于 "first-line" 伪元素:

    • font properties
    • color properties 
    • background properties
    • word-spacing
    • letter-spacing
    • text-decoration
    • vertical-align
    • text-transform
    • line-height
    • clear

    ⑷示例:浏览器会根据 "first-line" 伪元素中的样式对 p 元素的第一行文本进行格式化

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>:first-line</title> 
    <style>
    p:first-line 
    {
    color:deeppink;
    font-variant:small-caps;
    }
    </style>
    </head>
    
    <body>
    <p>你可以使用 "first-line" 伪元素向文本的首行设置特殊样式。</p>
    </body>
    </html>

    效果图:

     

    ㈢:first-letter 伪元素

    ⑴"first-letter" 伪元素用于向文本的首字母设置特殊样式

    ⑵"first-letter" 伪元素只能用于块级元素。

    ⑶下面的属性可应用于 "first-letter" 伪元素:

    • font properties
    • color properties 
    • background properties
    • margin properties
    • padding properties
    • border properties
    • text-decoration
    • vertical-align (only if "float" is "none")
    • text-transform
    • line-height
    • float
    • clear

    ⑷示例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>首字母特殊样式</title> 
    <style>
    p:first-letter {
                     color:#ff0000;
                     font-size:xx-large;
                   }
    </style>
    </head>
    
    <body>
    <p>用美好的心情去面对接下来会发生的每一件事。</p>
    </body>
    </html>

    效果图:

     

    ㈣伪元素和CSS类

    伪元素可以结合CSS类

    示例:使所有 class 为 article 的段落的首字母变为红色。

    p.article:first-letter {color:#ff0000;}
    
    <p class="article">文章段落</p>

    ㈤多个伪元素

    示例:段落的第一个字母将显示为红色,其字体大小为 xx-large。

               第一行中的其余文本将为蓝色,并以小型大写字母显示。

               段落中的其余文本将以默认字体大小和颜色来显示:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>结合示例</title> 
    <style>
    p:first-letter
    {
        color:deeppink;
        font-size:xx-large;
    }
    p:first-line 
    {
        color:blue;
        font-variant:small-caps;
    }
    </style>
    </head>
    
    <body>
    <p>洗尽铅华始见金,褪去浮华归本真</p>
    </body>
    </html>

    效果图:

     

    ㈥CSS - :before 伪元素

    ":before" 伪元素可以在元素的内容前面插入新内容。

    下面的例子在每个 <h1>元素前面插入一幅图片:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>:before</title> 
    <style>
    h1:before {content:url(smiley.gif);}
    </style>
    </head>
    
    <body>
    <h1>This is a heading</h1>
    <p>The :before pseudo-element inserts content before an element.</p>
    <h1>This is a heading</h1>
    <p><b>注意:</b>仅当 !DOCTYPE 已经声明 IE8 支持这个内容属性</p>
    </body>
    </html>

    效果图:

     

    ㈦CSS - :after 伪元素

    ":after" 伪元素可以在元素的内容之后插入新内容。

    下面的例子在每个 <h1> 元素后面插入一幅图片:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>:after</title> 
    <style>
    h1:after {content:url(smiley.gif);}
    </style>
    </head>
    
    <body>
    <h1>This is a heading</h1>
    <p>The :after pseudo-element inserts content after an element.</p>
    <h1>This is a heading</h1>
    <p><b>注意:</b>仅当!DOCTYPE 已经声明 IE8支持这个内容属性.</p>
    </body>
    </html>

    效果图:

     

    ㈧所有CSS伪类/元素

  • 相关阅读:
    jwt手动生成access_token
    python学习-52 XML模块
    python学习-51 shelve模块
    python学习-50 pickle模块
    python学习-49 json模块
    python学习-48 模块2
    python学习-47 random模块
    python学习-46 时间模块
    python学习-45 模块
    python学习-44 程序的解耦 (不是特别懂的,回头在复习)
  • 原文地址:https://www.cnblogs.com/shihaiying/p/11443265.html
Copyright © 2011-2022 走看看