【前端】css笔记(10)

目录:

伪 类

anchor 伪 类

css类-伪 类

first - child 伪 类

lang 伪 类

focus 伪 类

伪 元素

first-line伪 元素

first-letter伪 元素

css类-伪 元素

Multiple Pseudo-elements

before伪 元素

after伪 元素


伪 类

用来添加一些选择器的特殊效果(由于状态的变化是非静态的,所以元素达到一个特定状态时,它可能得到一个伪类的样式;当状态改变时,它又会失去这个样式。由此可以看出,它的功能和 class 有些类似,但它是基于文档之外的抽象,所以叫伪 类)

伪 类语法:

selector:pseudo-class {property:value;}

css类-伪 类:

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

anchor 伪 类

链接的不同状态:

<style>
    a:link {color:#FF0000;}    
    a:visited {color:#00FF00;} 
    a:hover {color:#FF00FF;}   
    a:active {color:#0000FF;}  
</style>

ps:

具体应用见上一篇


css类-伪 类

伪 类可以与 CSS 类配合使用:

a.red:visited {color:#FF0000;}       

<a class="red" href="css-syntax.html">CSS Syntax</a>


first - child 伪 类

选择元素的第一个子元素:

<style>
    p:first-child
    {
        color:blue;
    } 
</style>

ps:

选择器匹配作为任何元素的第一个子元素的 <p> 元素

<style>
    p > i:first-child
    {
        color:blue;
    } 
</style>

ps:

选择相匹配的所有 <p> 元素的第一个 <i> 元素

<style>
    p:first-child i
    {
        color:blue;
    } 
</style>

ps:

选择器匹配所有作为元素的第一个子元素的 <p> 元素中的所有 <i> 元素


lang 伪 类

为不同的语言定义特殊的规则:

<style>
    q:lang(no)
    {
        quotes: "~" "~";
    }
</style>

<body>
    <p>一些文字 <q lang="no">段落中的引用</q> 一些文字。</p>
</body>

ps:

lang 类为属性值为 no 的 q 元素定义引号的类型


focus 伪 类

设置输入框(选择元素输入后具有焦点):

<style>
    input:focus
    {
        background-color:yellow;
    }
</style>


伪 元素

用来添加一些选择器的特殊效果(css伪 元素控制的内容和元素是没有差别的,但是它本身只是基于元素的抽象,并不存在于文档中,所以称为伪 元素)

伪 元素语法:

selector:pseudo-element {property:value;}

css类-伪 元素:

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

first-line伪 元素

用于向文本的首行设置特殊样式:

<style>
    p:first-line 
    {
    color:#ff0000;
    font-variant:small-caps;
    }
</style>

ps:

  1. "first-line" 伪 元素只能用于块级元素
  2. 下面的属性可应用于 "first-line" 伪 元素:
  • font properties
  • color properties 
  • background properties
  • word-spacing
  • letter-spacing
  • text-decoration
  • vertical-align
  • text-transform
  • line-height
  • clear

first-letter伪 元素

用于向文本的首字母设置特殊样式:

<style>
    p:first-letter 
    {
        color:#ff0000;
        font-size:xx-large;
    }
</style>

ps:

  1. "first-letter" 伪元素只能用于块级元素
  2. 下面的属性可应用于 "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

css类-伪 元素

伪元素可以结合 CSS 类: 

    <style>
        p.article:first-letter {color:#ff0000;}
    </style>


Multiple Pseudo-elements

可以结合多个伪元素来使用:

<style>
    p:first-letter
    {
        color:#ff0000;
        font-size:xx-large;
    }
    p:first-line 
    {
        color:#0000ff;
        font-variant:small-caps;
    }
</style>

ps:

段落的第一个字母将显示为红色,其字体大小为 xx-large。第一行中的其余文本将为蓝色,并以小型大写字母显示


before伪 元素

在元素的内容前面插入新内容:

<style>
    h1:before {content:url(/statics/images/course/smiley.gif);}
</style>

ps:

在每个 <h1>元素前面插入一幅图片


after伪 元素

在元素的内容之后插入新内容:

<style>
    h1:after {content:url(http://www.w3cschool.cn/statics/images/course/smiley.gif);}
</style>

ps:

在每个 <h1> 元素后面插入一幅图片


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>css整理</title>
</head>
<body>
    <style>
        a:link {
            color: #FF0000;
        }

        a:visited {
            color: #00FF00;
        }

        a:hover {
            color: #FF00FF;
        }

        a:active {
            color: #0000FF;
        }
    </style><!--链接-->

    <style>
        p:first-child {
            color: blue;
        }
    </style><!--首元素-->

    <style>
        p > i:first-child {
            color: blue;
        }
    </style><!--首元素-->

    <style>
        p:first-child i {
            color: blue;
        }
    </style><!--首元素-->

    <style>
        q:lang(no) {
            quotes: "~" "~";
        }
    </style>
    <body>
        <p>一些文字 <q lang="no">段落中的引用</q> 一些文字。</p>
    </body><!--定义lang-->

    <style>
        input:focus {
            background-color: yellow;
        }
    </style><!--输入框-->

    <style>
        p:first-line {
            color: #ff0000;
            font-variant: small-caps;
        }
    </style><!--first-line伪 元素-->

    <style>
        p:first-letter {
            color: #ff0000;
            font-size: xx-large;
        }
    </style><!--first-letter伪 元素-->

    <style>
        p.article:first-letter {
            color: #ff0000;
        }
    </style><!--伪 元素结合CSS类-->

    <style>
        p:first-letter {
            color: #ff0000;
            font-size: xx-large;
        }

        p:first-line {
            color: #0000ff;
            font-variant: small-caps;
        }
    </style><!--多个伪 元素-->

    <style>
        h1:before {
            content: url(/statics/images/course/smiley.gif);
        }
    </style><!--before伪 元素-->

    <style>
        h1:after {
            content: url(http://www.w3cschool.cn/statics/images/course/smiley.gif);
        }
    </style><!--after伪 元素-->

</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

足足一米58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值