今天发现很多国外的网站和框架设计都用到了before和after,之前使用的比较少,今天试了下觉得还是很有意思的~

说明

1. :before 和 :after将在内容元素的前后插入额外的元素;:before将会在内容之前“添加”一个元素而:after将会在内容后“添加”一个元素。在它们之中添加内容我们可以使用content属性。

2. :before 和 :after发布于CSS2.1, 在css3中修订后伪元素使用::,伪类使用:, 因而形式为:: before, ::after

3. 无论使用单引号还是双引号浏览器都能识别,但是IE8只支持单冒号格式,因而为兼容还是使用单冒号

简单例子

    .div1:before{
content:open-quote;
}
.div1:after{
content:close-quote;
}
    <div class="div1"> Today is a wonderful day. Wish you happy~</div>

结果:

“ Today is a wonderful day. Wish you happy~”

设置伪元素样式

eg1:

    .div1{
width:500px;
height:200px;
margin:100px auto;
background-color:#F0F0F0;
line-height:200px;
text-align:center;
}
.div1:before{
content:open-quote;
position:relative;
font-size: 24pt;
line-height:200px;
text-align:center;
color:#fff;
background:#ddd;
border-radius:25px; }
.div1:after{
content:close-quote;
position:relative;
font-size: 24pt;
background:#ddd;
border-radius:25px;
line-height:200px;
text-align:center;
color:#fff;
}
    <div class="div1"> Today is a wonderful day. Wish you happy~</div>

结果:

注意:实际使用时注意将相同的css抽取,div[class*='']:before, div[class*='']:after 

eg2:(与伪类结合使用)

添加样式:

    .div1:hover:after,.div1:hover:before {
background-color: #555;
}

效果(鼠标放上即可看到效果):

Today is a wonderful day. Wish you happy~

应用

1.给图片添加阴影

eg1:

css如下:

.div {
width: 500px;
height: 200px;
margin: 100px auto;
background-color: #F0F0F0;
line-height: 200px;
text-align: center;
} .effect {
position: relative;
} .effect:hover:before {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
left: 10px;
width: 50%;
top: 80%;
max-width: 300px;
background: #777; -webkit-box-shadow: 0 15px 10px #777;
box-shadow: 0 15px 10px #777; -webkit-transform: rotate(-3deg);
  transform:rotate(-3deg);
}
  <div class="div effect">Today is a wonderful day. Wish you happy~</div>

效果如下:

eg2:

在eg1的基础上添加after

.div2 {
width: 500px;
height: 200px;
margin: 100px auto;
background-color: #F0F0F0;
line-height: 200px;
text-align: center;
} .effect1 {
position: relative;
} .effect1:hover:before {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
left: 10px;
width: 50%;
top: 80%;
max-width: 300px;
background: #777; -webkit-box-shadow: 0 15px 10px #777;
box-shadow: 0 15px 10px #777; -webkit-transform: rotate(-3deg);
transform: rotate(-3deg);
}
.effect1:hover:after {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
right: 10px;
left: auto;
width: 50%;
top: 80%;
max-width:300px;
background: #777; -webkit-box-shadow: 0 15px 10px #777;
box-shadow: 0 15px 10px #777; -webkit-transform: rotate(3deg);
transform: rotate(3deg);
}
        <div class="div2 effect1">Today is a wonderful day. Wish you happy~</div>

效果如下:

(注意:实际使用时注意将相同的css抽取,div[class*='']:before, div[class*='']:after )

2. 叠加图片

1)未叠加时如下:

* {margin:; padding:;}
body {background: #ccd3d7;}
.imageshow{
border: 6px solid #fff;
float:left;
height:267px;
width:400px;
margin:50px;
position: relative; -webkit-box-shadow: 2px 2px 5px rgba(0,0,0.3);
box-shadow: 2px 2px 5px rgba(0,0,0.3);
}
    <div class="imageshow">
<img src="data:images/demo.png">
</div>

2)添加before伪元素

.imageshow:before {
content: "";
height: 400px;
   width: 267px;
background: #eff4de;
border: 6px solid #fff; position: absolute;
z-index: -1;
top: 0px;
left: -10px; -webkit-box-shadow: 2px 2px 5px rgba(0,0,0,0.3);
box-shadow: 2px 2px 5px rgba(0,0,0,0.3); -webkit-transform: rotate(-5deg);
transform: rotate(-5deg);
}

效果如下:

3)加上after

.imageshow:after {
content: "";
height:267px;
width:400px;
background: #eff4de;
border: 6px solid #fff; position: absolute;
z-index: -1;
top: 5px;
left: 0px; -webkit-box-shadow: 2px 2px 5px rgba(0,0,0,0.3);
box-shadow: 2px 2px 5px rgba(0,0,0,0.3); -webkit-transform: rotate(4deg);
transform: rotate(4deg);
}

效果如下:

( 注意:实际使用时注意将相同的css抽取,div[class*='']:before, div[class*='']:after )

有趣的hover效果

参考:https://github.com/IanLunn/Hover

1. 有趣的curl hover效果

左上curl css如下;

.button {
margin:10px;
padding: 0.5em;
cursor: pointer;
background: #BCA9F5;
text-decoration: none;
color: #666666;
border-radius:10px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
} /* Curl Top Left */
.curl-top-left {
display: inline-block;
position: relative;
-webkit-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
}
.curl-top-left:before {
pointer-events: none;
position: absolute;
content: '';
height:;
width:;
top:;
left:;
background: white;
/* IE9 */
background: linear-gradient(135deg, white 45%, #aaaaaa 50%, #cccccc 56%, white 80%);
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#ffffff', endColorstr='#000000');
/*For IE7-8-9*/
z-index:;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.4);
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: width, height;
transition-property: width, height;
}
.curl-top-left:hover:before, .curl-top-left:focus:before, .curl-top-left:active:before {
width: 25px;
height: 25px;
}
    <div class="div5">
<a href="#" class="button curl-top-left">Curl Top Left</a>
<br/>
<a href="#" class="button curl-top-right">Curl Top Right</a>
<br/>
<a href="#" class="button curl-bottom-right">Curl Bottom Right</a>
<br/>
<a href="#" class="button curl-bottom-left">Curl Bottom Left</a>
</div>

效果如下:(鼠标放上可看到效果:)

2.有趣的bubble hover效果

bubble top css如下:

.button {
margin:10px;
padding: 1em;
cursor: pointer;
background: #BCA9F5;
text-decoration: none;
color: #666666;
border-radius:10px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
/* SPEECH BUBBLES */
/* Bubble Top */
.bubble-top {
display: inline-block;
position: relative;
-webkit-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
}
.bubble-top:before {
pointer-events: none;
position: absolute;
z-index: -1;
content: '';
border-style: solid;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: top;
transition-property: top;
left: calc(50% - 10px);
top:;
border-width: 0 10px 10px 10px;
border-color: transparent transparent #BCA9F5 transparent;
}
.bubble-top:hover:before, .bubble-top:focus:before, .bubble-top:active:before {
top: -10px;
}

效果如下:(鼠标放上可看到效果)


CSS伪元素before和after的更多相关文章

  1. jQuery如何改变css伪元素样式

    首先我们看一下css伪元素是什么: CSS 伪元素用于向某些选择器设置特殊效果. 伪元素有哪些: :first-line 伪元素:"first-line" 伪元素用于向文本的首行设 ...

  2. css 伪元素分享!!!

    最近接触到的css 伪元素觉得还算不错 分享下: 1.清楚内盒浮动设置: ;} .back_list ul:after{;visibility: hidden;}/*清楚内盒浮动设置*/ 2.伪元素a ...

  3. CSS伪类与CSS伪元素的区别及由来

    关于两者的区别,其实是很古老的问题.但是时至今日,由于各种网络误传以及一些不负责任的书籍误笔,仍然有相当多的人将伪类与伪元素混为一谈,甚至不乏很多CSS老手.早些年刚入行的时候,我自己也被深深误导,因 ...

  4. (六)CSS伪元素

    CSS伪元素用于向某些选择器设置特殊效果. 伪元素的用法和伪类相似: selector:pseudo-element {property:value;} CSS类也可以与伪元素配合使用: select ...

  5. CSS 伪元素

    CSS伪元素是用来添加一些选择器的特殊效果. 语法 伪元素的语法: selector:pseudo-element {property:value;} CSS类也可以使用伪元素: selector.c ...

  6. css伪元素

    CSS 伪元素用于向某些选择器设置特殊效果. 1.:first-line 伪元素  "first-line" 伪元素用于向文本的首行设置特殊样式.注意:"first-li ...

  7. js如何控制css伪元素内容(before,after)

    曾经遇到的问题,在对抗UC浏览器屏蔽需要把内容输出到css 伪元素中输出.有个疑问如何用js控制它.于是在segmentfault提问,如下是对问题的整理: 如何用js控制css伪类after 简单粗 ...

  8. css伪元素用法大全

    本文主要讲解css伪元素的用法,有需要的朋友可以阅读此文.本文讲解的伪元素有:before,after. 什么是伪元素(Pseudo element)? 伪元素不是真正的元素,不存在与文档之中,所以j ...

  9. js分析 汽_车_之_家 js生成css伪元素 hs_kw44_configUS::before

    0.参考 https://developer.mozilla.org/zh-CN/docs/Web/CSS/Pseudo-elements https://developer.mozilla.org/ ...

  10. CSS伪元素before、after妙用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. PHP安全编程:shell命令注入(转)

    使用系统命令是一项危险的操作,尤其在你试图使用远程数据来构造要执行的命令时更是如此.如果使用了被污染数据,命令注入漏洞就产生了. exec()是用于执行shell命令的函数.它返回执行并返回命令输出的 ...

  2. 初学coreData数据库读取不成功的问题

    写了一个从数据库读取数据显示列表的代码,结果却无法运行,提示找不到对应的entity,也就是数据库中的某一个表 我查遍了代码也没有发现什么逻辑错误,在appDelegate也初始化了相关数据库,在界面 ...

  3. Android(java)学习笔记243:多媒体之视频播放器

    1.这里我们还是利用案例演示视频播放器的使用: (1)首先,我们看看布局文件activity_main.xml,如下: <RelativeLayout xmlns:android="h ...

  4. 《CODE》讲了什么?

    本书首先从黑夜中用手电筒开关灯的方式来与小伙伴交流从而引出了编码与组合的概念,并阐明了编码的本质就是交流,是一种用来在机器与人之间传递信息的方式.然后在第 2~3 章中讲述了编码与组合的应用,如电报机 ...

  5. oracle 添加自增索引

    1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...

  6. codevs1690开关灯

    #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #def ...

  7. Java 8 Lambda表达式10个示例【存】

    PS:不能完全参考文章的代码,请参考这个文件http://files.cnblogs.com/files/AIThink/Test01.zip 在Java 8之前,如果想将行为传入函数,仅有的选择就是 ...

  8. ASP.NET-FineUI开发实践-13(二)

    1.加图标和事件 上次已经通过DataSimulateTreeLevelField属性控制了树节点的显示,不用看也知道就是给指定列数据前面加个图标的HTML 可以在SimulateTreeHeper类 ...

  9. 开源的Android开发框架-------PowerFramework使用心得(三)内置浏览器BrowserActivity

    使用内置浏览器必须是引用源码的方式(因为jar中不能打包布局文件等资源).内置浏览器是一个继承自BaseActivity的普通Activity,使用WebView实现. 1.简单的打开内置浏览器 In ...

  10. 【转】iOS6中的Auto Layout:通过代码添加约束

        最近做的项目用到了Auto Layout,于是经过了一番大量的google,这是我看到的讲用代码创建约束最清晰的一篇教程,于是想跟更多的人分享一下.原文也比较简单,可以直接过去看,如果我翻译的 ...