::before和::after的详细介绍

news/2024/6/18 19:58:01

原文传送门: https://www.cnblogs.com/staro...

一、介绍

css3为了区分伪类和伪元素,伪元素采用双冒号写法。

常见伪类——:hover,:link,:active,:target,:not(),:focus。

常见伪元素——::first-letter,::first-line,::before,::after,::selection。

::before和::after下特有的content,用于在css渲染中向元素逻辑上的头部或尾部添加内容。

这些添加不会出现在DOM中,不会改变文档内容,不可复制,仅仅是在css渲染层加入。

所以不要用:before或:after展示有实际意义的内容,尽量使用它们显示修饰性内容,例如图标。

举例:网站有些联系电话,希望在它们前加一个icon☎,就可以使用:before伪元素,如下:

复制代码
<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">

.phoneNumber::before {
content:'\260E';
font-size: 15px;

}
</style>
<p class="phoneNumber">12345645654</p>
复制代码

Note:这些特殊字符的html,js和css的写法是不同的,具体可查看html特殊字符的html,js,css写法汇总。

二、content属性

::before和::after必须配合content属性来使用,content用来定义插入的内容,content必须有值,至少是空。默认情况下,伪类元素的display是默认值inline,可以通过设置display:block来改变其显示。

content可取以下值。

1、string
使用引号包一段字符串,将会向元素内容中添加字符串。如:a:after{content:""}

举例:

复制代码
<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
p::before{

content: "《";
color: blue;

}
p::after{

content: "》";
color: blue;

}
</style>
<p>平凡的世界</p>
复制代码

2、attr()
通过attr()调用当前元素的属性,比如将图片alt提示文字或者链接的href地址显示出来。

<style type="text/css">
a::after{

content: "(" attr(href) ")";

}
</style>
starof

3、url()/uri()
用于引用媒体文件。

举例:“百度”前面给出一张图片,后面给出href属性。

复制代码
<style>
a::before{

content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");

}
a::after{

content:"("attr(href)")";

}
a{

text-decoration: none;

}

</style>

<body>
百度
</body>
复制代码
效果:

4、counter()
调用计数器,可以不使用列表元素实现序号功能。

配合counter-increment和counter-reset属性使用:

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

代码:

复制代码
<style>
body{

counter-reset: section;

}
h1{

counter-reset: subsection;

}
h1:before{

counter-increment:section;
content:counter(section) "、";

}
h2:before{

counter-increment:subsection;
content: counter(section) "." counter(subsection) "、";

}

</style>

<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>

<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>

</body>
复制代码
效果:

了解更多可参考:https://developer.mozilla.org...

三、使用

1、清除浮动
清除浮动方法有多种,现在最常用的就是下面这种方法,仅需要以下样式即可在元素尾部自动清除浮动

复制代码
.cf:before,
.cf:after {

content: " ";
display: table; 

}
.cf:after {

clear: both;

}
.cf {

*zoom: 1;

}
复制代码
2、模拟float:center的效果
float没有center这个取值,但是可以通过伪类来模拟实现。

这个效果实现很有意思,左右通过::before float各自留出一半图片的位置,再把图片绝对定位上去。

核心css如下:

复制代码

page-wrap { width: 60%; margin: 40px auto; position: relative; }

logo { position: absolute; top: 0; left: 50%; margin-left: -125px; }

l, #r { width: 49%; }

l { float: left; }

r { float: right; }

l:before, #r:before { content: ""; width: 125px; height: 250px; }

l:before { float: right; }

r:before { float: left; }

复制代码
完整代码如下:

View Code

出自:https://css-tricks.com/float-...

3、做出各种图形效果
举例:一个六角星

复制代码
<style>

star-six {

width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
position: relative;
}

star-six::after {

width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 100px solid red;
position: absolute;
content: "";
top: 30px;
left: -50px;
}
</style>
<body>
<div id="star-six"></div>
</body>
复制代码

star-six的div是一个正三角行,#star-six::after是一个倒三角形,通过绝对定位,调整其位置即可实现六角星的效果。

点我查看更多。

4、不使用图片创建小图标
举例:比如一个电话

很巧妙的应用一个div左border加圆角当机身,::before和::after配合圆角当听筒。

复制代码
<style type="text/css">

#phone{
    width:50px;
    height:50px;
    border-left:6px solid #EEB422;
    border-radius:20%;
    transform:rotate(-30deg);
    -webkit-transform:rotate(-30deg);
    margin:20px;
    margin-right:0px;
    position:relative;
    display: inline-block;
    top: -5px;
}
 #phone:before{
    width:15px;
    height:15px;
    background:#EEB422;
    border-radius: 20%;
    content: "";
    position: absolute;
    left:-2px;
    top: 1px;
 }
 #phone:after{
    width:15px;
    height:15px;
    background:#EEB422;
    border-radius: 20%;
    content: "";
    position: absolute;
    left:-3px;
    top: 34px;
 }

</style>
<div id="wraper">

<div id="phone"></div>

</div>
复制代码

更多图标:

View Code

这个效果来自:http://www.w3cfuns.com/blog-5...

有大神用伪元素创建了84种小图标,具体可查看http://nicolasgallagher.com/p...

5、显示打印网页的URL
复制代码
<style>
@media print {
a[href]:after {

content: " (" attr(href) ") ";

}
}
</style><body>
百度
</body>
复制代码

6、给blockquote添加引号
经常用到给blockquote 引用段添加巨大的引号作为背景,可以用 ::before 来代替 background 。好处是即可以给背景留下空间,还可以直接使用文字而非图片:

复制代码
<meta charset="utf-8"/>
<style type="text/css">

blockquote::before {
content: open-quote;
color: #ddd;
z-index: -1;
font-size:80px;

}
</style>
<blockquote>引用一个段落,双引号用::before伪元素实现</blockquote>
复制代码

7、超链接特效
举例:配合 CSS定位实现一个鼠标移上去,超链接出现方括号的效果

复制代码
<meta charset="utf-8" />
<style type="text/css">
body{

background-color: #425a6c;

}

a {
position: relative;
display: inline-block;
outline: none;
color: #fff;
text-decoration: none;
font-size: 32px;
padding: 5px 20px;

}
a:hover::before, a:hover::after { position: absolute; }
a:hover::before { content: "5B"; left: -10px; }
a:hover::after { content: "5D"; right: -10px; }
</style>
鼠标移上去出现方括号
复制代码

更多创意链接特效可参考: Creative Link Effects 。

8、::before和::after实现多背景图片
举例:一个标签应用5张背景图

View Code

原效果来自:Multiple Backgrounds and Borders with CSS 2.1


http://www.niftyadmin.cn/n/3090027.html

相关文章

一个容易忽略的问题—Javascript文件加载的顺序

今天在用Google Map API做一个GPS跟踪定位的应用.在加测距功能的时候,总是提示GRulerControl未定义. 首先确定了引用的Google Map API 2.0,也引用了,了Mapruler.js文件.调度不少时间,都没有发现问题.偶然灵光一闪,发现,调用GRulerControl的函数写在引用Mapruler.js只前了.因此,…

华为手机备份的通讯录是什么文件_华为通讯录怎么导入新手机(三种方法帮你导通讯录)...

非智能手机时代&#xff0c;大家都晓得通过将手机号码存入手机卡上&#xff0c;换手机时&#xff0c;把旧手机卡放入新手机上&#xff0c;相应地旧手机上的号码就在新手机显示出来了。但那时手机卡的容量很有限&#xff0c;似乎至多能保存200个号码左右。(本文来自www.777n.com…

Slim Framework RabbitMQ

2019独角兽企业重金招聘Python工程师标准>>> 关于 illuminate/database 的注册部分参考这个即可 下面我要另外说明的一点是&#xff0c; 在 settings 中对 database 的部分有一些区别&#xff0c;主要是为了 区分正式数据库和测试数据库。 <?php$remote_addr e…

暑假周总结八9.2

开学了&#xff0c;假期也就结束了&#xff0c;大三了&#xff0c;对上学还是恐惧&#xff0c;开学有很多课&#xff0c;双学位也不想放弃&#xff0c;系主任的暑假任务也没完成&#xff0c;成功不看有多聪明&#xff0c;看有多大的毅力&#xff0c;毅力是没有的&#xff0c;有…

更新mysql表数据alter_Mysql数据库alter修改表的所有场景和用法

如果你想要修改表的信息&#xff0c;你会发现alter很强大。我们可以看到这样一张表。CREATE TABLE score (student_id int(10) unsigned NOT NULL,event_id int(10) unsigned NOT NULL,score int(11) DEFAULT NULL,PRIMARY KEY (event_id,student_id),KEY student_id (student_…

(转)深入剖析C#继承机制

一. 继承基础知识 为了提高软件模块的可复用性和可扩充性&#xff0c;以便提高软件的开发效率&#xff0c;我们总是希望能够利用前人或自己以前的开发成果&#xff0c;同时又希望在自己的开发过程中能够有足够的灵活性&#xff0c;不拘泥于复用的模块。C#这种完全面向对象的程序…

centos7之docker安装

下午四点左右&#xff0c;我准备接触docker这个技术。之所以接触它&#xff0c;原因来自tomcat服务器老是挂&#xff0c;也不能说老是挂&#xff0c;一周一次吧&#xff0c;或者不定时&#xff0c;最初出现的问题&#xff0c;分为这么几类&#xff1f; 一类&#xff0c;java代码…

数据库CRUD操作以及MyBatis的配置使用

• 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成• 注解和XML定义• ViewObject和DateTool • 首页开发• 业务字段设计实体&#xff1a; name:logub_ticket lable: id user_id ticket expired status • 数据库创建 GUI版本管理工具创建&#xff0c;然后通过GUI转…