css before和after伪类的一些用法

一  :after清除浮动
浮动布局导致高度坍塌影响到整体布局时元素需要清除浮动,使用:after就是其中的一种方法:

.row:after { 
	width:0;
	height:0;
	content:'';
	display: block;
	clear: both;
}

二  :before和:after圆环旋转

css before和after伪类的一些用法_第1张图片
html:

圆环旋转

  • 登录
css:
* {
	box-sizing: border-box;
}

.ring li {
	position: relative;
	float: left;
	width: 100px;
	height: 100px;
	border-radius: 50%;
	margin: 20px;
	padding: 10px;
	list-style: none;
}

.ring li:hover {
	cursor: pointer;
}

.ring li:before {
	content: '';
	display: block;
	position: absolute;
	width: 100%;
	height: 100%;
	left: -6px;
	top: -6px;
	border-radius: 50%;
	border: 6px dashed rgb(204, 204, 204);
}

.ring li > div {
	height: 100%;
	background: rgb(158, 234, 106);
	border-radius: 50%;
	box-shadow: 0 0 3px rgba(0,0,0,0.3);
	text-align: center;
	line-height: 80px;
	font-size: 16px;
	color: rgb(255, 255, 255);
}

@-webkit-keyframes circle{
	0%{ transform:rotate(0deg); }
	100%{ transform:rotate(-360deg); }
}

.ring li:hover:before {
	-webkit-animation: circle 4s infinite linear;
}

.ring li:first-child:before {
	-webkit-animation: circle 4s infinite linear;
}

.ring li:hover:before {
	border-color: rgba(204, 204, 204, .8);
}

.ring li:active:before {
	border-color: orange;
}

.ring li:hover > div {
	background: rgba(158, 234, 106, .8);
}

.ring li:active > div {
	background: rgba(0, 0, 255, .5);
}

.ring li:last-child,
.ring li:last-child > div,
.ring li:last-child:before {
	border-radius: 0;
}

三  :before和:after小三角

css before和after伪类的一些用法_第2张图片
 html:

仿微信聊天

  • 吃了没?
  • 嗯嗯,在吃呢,你呢。
css:
* {
	box-sizing: border-box;
}

li {
    list-style: none;
}

.textRight {
    text-align: right;
}

.triangle {
	width: 300px;
}

.triangle li {
    padding: 10px;
    margin-bottom: 10px;
}

.triangle li span {
    position: relative;
    border-radius: 7px;
    background-color: #9EEA6A;
    padding: 6px 10px 8px 10px;
    z-index: 1;
}

.triangle li span:before, 
.triangle li span:after {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border: 8px solid transparent;
    position: absolute;
    top: 10px;
}

.triangle li.textLeft span:before {
    border-right: 8px solid #9EEA6A;
    left: -16px;
}

.triangle li.textRight span:after {
    border-left: 8px solid #9EEA6A;
    right: -16px;
}
css before和after伪类的一些用法_第3张图片
额外添加以下样式:
.triangle li span {
    border: 1px solid #ccc;
}

.triangle .textLeft span:after {
    border-right: 8px solid #ccc;
    left: -17px;
    z-index: -1;
}

.triangle .textRight span:before {
    border-left: 8px solid #ccc;
    right: -17px;
    z-index: -1;
}

四  :before和:after小图标

css before和after伪类的一些用法_第4张图片

html:

图标

css:
.container {
	width: 650px;
}

.container > div {
	box-sizing: content-box;
}

.icon-power{
	width: 30px;
	height: 30px;
	margin:20px;
	border: 6px solid #9EEA6A;
	border-radius: 50%;
	position: relative;
	display: inline-block;
}

.icon-power:before{
	width: 7px;
	height: 22px;
	background: #9EEA6A;
	position: absolute;
	left: 8px;
	top: -13px;
	content: "";
	border: 3px solid #fff;
}

.icon-play{
	width: 30px;
	height: 30px;
	margin: 20px;
	border: 6px solid #9EEA6A;
	border-radius: 50%;
	position: relative;
	display: inline-block;
	background: #9EEA6A;
}

.icon-play:before{
	border: 11px solid transparent;
	border-left: 17px solid #fff;
	content: "";
	position: absolute;
	left: 9px;
	top: 3px;
}

.icon-pause{
	width: 30px;
	height: 30px;
	margin: 20px;
	border: 6px solid #9EEA6A;
	border-radius: 50%;
	position: relative;
	display: inline-block;
	background: #9EEA6A;
}

.icon-pause:before{
	height: 20px;
	width: 5px;
	border: 0px solid transparent;
	border-left: 8px solid #fff;
	border-right: 8px solid #fff;
	content: ""; 
	position: absolute;
	left: 4px;
	top: 5px;
}

.icon-stop{
	width: 30px;
	height: 30px;
	margin: 20px;
	border: 6px solid #9EEA6A;
	border-radius: 50%;
	position: relative;
	display: inline-block;
	background: #9EEA6A;
}

.icon-stop:before{
	height: 17px;
	width: 17px;
	background: #fff;
	content: "";
	position: absolute;
	left: 6px;
	top: 6px;
}

.icon-comment{
	width: 50px;
	height: 25px;
	margin: 20px;
	border: 6px solid #9EEA6A;
	border-radius: 20%;
	position: relative;
	display: inline-block;
	background: #9EEA6A;
}

.icon-comment:before{
	border: 10px solid transparent;
	border-top: 10px solid #9EEA6A;
	content: "";
	position: absolute;
	left: 28px;
	top: 28px;
}

.icon-comment:after{
	content: "....";
	position: absolute;
	color: #fff;
	font-size: 26px;
	top: -10px;
	left: 2px;
}

.icon-like{
	width: 50px;
	height: 30px;
	margin:20px;
	border-radius: 55%;
	transform: rotate(55deg);
	-webkit-transform: rotate(55deg);
	position: relative;
	display: inline-block;
	background: #9EEA6A;
}

.icon-like:before{
	width: 50px;
	height: 30px;
	border-radius: 55%;
	transform: rotate(-110deg);
	-webkit-transform: rotate(-110deg);
	background: #9EEA6A;
	content: "";
	position: absolute;
	left: 8px;
	top: -12px;
}

.icon-search{
	width: 20px;
	height: 20px;
	border:4px solid #9EEA6A;
	border-radius: 50%;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: -5px;
	left: -5px;
}

.icon-search:before{
	width: 20px;
	height: 5px;
	background: #9EEA6A;
	transform: rotate(45deg);
	-webkit-transform: rotate(45deg);
	content: "";
	position: absolute;
	left: 15px;
	top: 22px;
}

.icon-home{
	width: 30px;
	height: 30px;
	background: #9EEA6A;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}

.icon-home:before{
	width: 6px;
	height: 12px;
	background: #fff;
	content: "";
	position: absolute;
	left: 12px;
	top: 20px;
}

.icon-home:after{
	border: 25px solid transparent;
	border-bottom: 20px solid #9EEA6A;
	content: "";
	position: absolute;
	top: -38px;
	left: -10px;
}

.icon-photo{
	width: 40px;
	height: 30px;
	background: #9EEA6A;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}

.icon-photo:before{
	width: 13px;
	height: 13px;
	border: 4px solid #fff;
	border-radius: 50%;
	background: #9EEA6A;
	content: "";
	position: absolute;
	left: 10px;
	top: 5px;
}

.icon-photo:after{
	width: 15px;
	height: 10px;
	background: #9EEA6A;
	content: "";
	position: absolute;
	top: -7px;
	left: 13px;
}

.icon-photo{
	width: 40px;
	height: 30px;
	background: #9EEA6A;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}
.icon-email{
	width: 50px;
	height: 35px;
	background: #9EEA6A;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}

.icon-email:before{
	border: 25px solid transparent;
	border-top: 20px solid #fff;
	content: "";
	position: absolute;
	left: 0px;
	top: 2px;
}

.icon-email:after{
	border: 25px solid transparent;
	border-top: 20px solid #9EEA6A;
	content: "";
	position: absolute;
	top:0px;
}

.icon-file{
	width: 30px;
	height: 45px;
	border: 4px solid #9EEA6A;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}

.icon-file:before{
	border: 10px solid #fff;
	border-right: 10px solid #9EEA6A;
	border-bottom: 10px solid #9EEA6A;
	content: "";
	position: absolute;
	left: -4px;
	top: -4px;
}

.icon-file:after{
	width: 20px;
	height: 5px;
	border-top: 3px solid #9EEA6A;
	border-bottom: 3px solid #9EEA6A;
	content: "";
	position: absolute;
	left: 5px;
	top: 25px;
}

.icon-history{
	width: 35px;
	height: 35px;
	border: 4px solid #9EEA6A;
	border-radius: 50%;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 5px;
}

.icon-history:before{
	width: 14px;
	height: 14px;
	border-bottom: 4px solid #9EEA6A;
	border-left: 4px solid #9EEA6A;
	content: "";
	position: absolute;
	left: 14px;
	top: 3px;
}

.icon-video{
	width: 50px;
	height: 35px;
	background: #9EEA6A;
	border-radius: 20%;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: -5px;
}

.icon-video:before{
	width: 6px;
	height: 6px;
	border: 11px solid transparent;
	border-right: 11px solid #9EEA6A;
	content: "";
	position: absolute;
	left: 35px;
	top: 4px;
}

.icon-video:after{
	width: 10px;
	height: 10px;
	border: 6px solid transparent;
	border-top: 6px solid #9EEA6A;
	border-left: 6px solid #9EEA6A;
	transform: rotate(45deg);
	-webkit-transform: rotate(45deg);
	content: "";
	position: absolute;
	left: 13px;
	top: 35px;
}

.icon-tags{
	width: 50px;
	height: 25px;
	background: #9EEA6A;
	border-radius: 35% 0% 0% 35%;
	transform: rotate(45deg);
	-webkit-transform: rotate(45deg);
	margin: 20px;
	margin-left: 35px;
	position: relative;
	display: inline-block;
	top: -5px;
}

.icon-tags:before{
	width: 10px;
	height: 10px;
	border-radius: 50%;
	background: #fff;
	content: "";
	position: absolute;
	left: 7px;
	top: 7px;
}

.icon-phone{
	width: 50px;
	height: 50px;
	border-left: 6px solid #9EEA6A;
	border-radius: 20%;
	transform: rotate(-30deg);
	-webkit-transform: rotate(-30deg);
	margin: 20px;
	margin-right: 0px;
	position: relative;
	display: inline-block;
	top: -5px;
}

.icon-phone:before{
	width: 15px;
	height: 15px;
	background: #9EEA6A;
	border-radius: 20%;
	content: "";
	position: absolute;
	left: -2px;
	top: 1px;
}

.icon-phone:after{
	width: 15px;
	height: 15px;
	background: #9EEA6A;
	border-radius: 20%;
	content: "";
	position: absolute;
	left: -3px;
	top: 34px;
}

.icon-profile{
	width: 40px;
	height: 15px;
	background: #9EEA6A;
	border-radius: 45% 45% 0 0;
	margin: 20px;
	position: relative;
	display: inline-block;
	top: 0px;
}

.icon-profile:before{
	width: 20px;
	height: 22px;
	background: #9EEA6A;
	border-radius: 40%;
	content: "";
	position: absolute;
	left: 10px;
	top: -22px;
}
更多图标可查看

Pure CSS GUI icons

http://nicolasgallagher.com/pure-css-gui-icons/

你可能感兴趣的:(css before和after伪类的一些用法)