::before与::after

CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此。前几天发现了 Creative Link Effects 这个非常有意思的介绍创意链接特效的页面,里面惊人的效果大量使用到的特性除了 transform 属性进行变形之外,就是接下来要介绍的这两个伪元素了。

一 基本语法

在了解进阶的应用之前,先来了解一下语法规则。平常仅仅需要将这两个伪元素用于添加一些自定义字符时,只需使用伪类使用的单冒号写法,以保证浏览器的兼容性

不过,在 CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号,因此如果使用了 display 或者 width 等属性时使得显示脱离了原本元素后,建议按照标准双写。过于老的浏览器可能会存在支持问题,不过伪元素大多是配合 CSS3 使用,就无所谓向下兼容了:

这两个伪类下特有的属性 content ,用于在 CSS 渲染中向元素逻辑上的头部或尾部添加内容。注意这些添加不会改变文档内容,不会出现在 DOM 中,不可复制,仅仅是在 CSS 渲染层加入。比较有用的是以下几个值:

[String] - 使用引号包括一段字符串,将会向元素内容中添加字符串。示例:

a:after { content: "↗"; }

attr() – 调用当前元素的属性,可以方便的比如将图片的 Alt 提示文字或者链接的 Href 地址显示出来。示例:

a:after { content:"(" attr(href) ")"; }

url() / uri() – 用于引用媒体文件。示例:

h1::before { content: url(logo.png); }

counter() –  调用计数器,可以不使用列表元素实现序号功能。具体请参见 counter-increment 和 counter-reset 属性的用法。示例:

h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }

二 进阶技巧

清除浮动是一个时常会遇到的问题,不少人的解决办法是添加一个空的 div 应用 clear:both; 属性。现在,无需增加没有意义的元素,仅需要以下样式即可在元素尾部自动清除浮动:

.clear-fix { *overflow: hidden; *zoom: 1; }
.clear-fix:after { display: table; content: ""; width: 0; clear: both; }

例如:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .div{
            width: 400px;
            border: 1px solid black;
        }
        .div1{
            width: 180px;
            height: 150px;
            border: 1px solid red;
            float: left;
        }
        .div2{
            width: 180px;
            height: 150px;
            border: 1px solid yellow;
            float: left;
        }
    /*    .div3{
            clear: both;
        }*/
        .div { *overflow: hidden; *zoom: 1; }  
        .div:after { display: table; content: ""; width: 0; clear: both; }  
    </style>
</head>
<body>
    <div class="div">
        <div class="div1"></div>
        <div class="div2"></div>
        <div class="div3"></div>
    </div>
</body>
</html>

许多人喜欢给 blockquote 引用段添加巨大的引号作为背景,这种时候我们就可以用 :before 来代替 background 了,即可以给背景留下空间,还可以直接使用文字而非图片:

blockquote::before {  
    content: open-quote;  
    position: absolute;  
    z-index: -1;  
    color: #DDD;  
    font-size: 120px;  
    font-family: serif;  
    font-weight: bolder;  
}  
<html>
<body>
<style>
blockquote::before {  
    content: open-quote;  
    position: absolute;  
    z-index: -1;  
    color: #DDD;  
    font-size: 120px;  
    font-family: serif;  
    font-weight: bolder;  
}  
</style>
Here comes a long quotation:

<blockquote>
This is a long quotation. This is a long quotation. This is a long quotation. This is a long quotation. This is a long quotation.
</blockquote>

请注意,浏览器在 blockquote 元素前后添加了换行,并增加了外边距。

</body>
</html>

三 特效妙用

除了简单的添加字符,配合 CSS 强大的定位和特效特性,完全可以达到给简单的元素另外附加最多两个容器的效果。有一点需要注意的是,如果不需要内容仅配合样式属性做出效果,内容属性也不能为空,即 content:”" 。否则,其他的样式属性一概不会生效。

悬浮出现方括号

鼠标移上链接,出现方括号:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        a{
            display: inline-block;
            margin: 15px 25px;
            outline: none;
            text-decoration: none;
            text-transform: uppercase;
            letter-spacing: 1px;
            font-weight: 400;
            text-shadow: 0 0 1px rgba(0,0,0,0.3);
            font-size: 1.35em;

        }
        a::before{
            margin-right: 10px;
            content: '[';
            transform: translateX(20px);
        }
        a::after{
            margin-left: 10px;
            content: ']';
            transform: translateX(-20px);
        }
        a::before,a::after{
            display: inline-block;
            opacity: 0;
            transition: transform 1.3s, opacity 1.2s;
        }
        a:hover::before{
            transform: translateX(-20px);
        }
        a:hover::after{
            transform: translateX(20px);
        }
        a:hover::before,a:hover::after{
            display: inline-block;
            opacity:1.0;
            transition: transform 1.3s, opacity 1.2s;
        }
    </style>
</head>
<body>
    <a href="www.chenxiaojun.com">Dont touch me</a>
</body>
</html>

 

posted @ 2016-09-02 15:17  chenxj  阅读(146)  评论(0)    收藏  举报