凌优教育
您的当前位置:首页css3中关于伪类的使用_html/css_WEB-ITnose

css3中关于伪类的使用_html/css_WEB-ITnose

来源:凌优教育


目标:

css中after伪类,last-child伪类的使用。以及部分css3的属性。

过程:

在制作导航时,经常会遇到在每一个li后面添加一个分割符号,到最后一个元素的时候,分割符就会去掉的一种效果。

如图

那么制作这样的一个效果,怎么用纯css很简单的完成了。这里用到了css的伪类。

html部分

	 	
  • Home
  • About Me
  • Portfolio
  • Blog
  • Resources
  • Contact Me
  • 然后调用css样式
    body{	 background: #ebebeb;	}	.nav{	 width:560px;	 height: 50px;	 font:bold 0/50px Arial;	 text-align:center;	 margin:40px auto 0;	 background: #f65f57;	 /*制作圆*/	 border-radius:9px; /*制作导航立体风格*/ box-shadow:0px 5px #911;	}	.nav a{	 display: inline-block;	 -webkit-transition: all 0.2s ease-in;	 -moz-transition: all 0.2s ease-in;	 -o-transition: all 0.2s ease-in;	 -ms-transition: all 0.2s ease-in;	 transition: all 0.2s ease-in;	}	.nav a:hover{	 -webkit-transform:rotate(10deg);	 -moz-transform:rotate(10deg);	 -o-transform:rotate(10deg);	 -ms-transform:rotate(10deg);	 transform:rotate(10deg);	}	.nav li{	 position:relative;	 display:inline-block;	 padding:0 16px;	 font-size: 13px;	 text-shadow:1px 2px 4px rgba(0,0,0,.5);	 list-style: none outside none;	}	/*使用伪元素制作导航列表项分隔线*/	.nav li:after{ 	 content:""; position:absolute; top:15px; right:0px; width:1px; height:15px; background:linear-gradient(to bottom, #f82f87,#B0363F,#f82f87);	} /*删除第一项和最后一项导航分隔线*/	.nav li:last-child:after{ 	 width:0px; height:0px;	}	.nav a,	.nav a:hover{	 color:#fff;	 text-decoration: none;	}

    css中的.nav li:after表明了在每一个li后面添加一个元素,正是content内容(制作渐变时,不需要有内容被添加,所以为空)。

    background:linear-gradient(to bottom ,#f82f87,#bo363f,#f82f87) //css3中的渐变样式

    对每一个li后面添加了一个渐变后,需要清除最后一个li的。

    这里面使用了.nav li:last-child:after的伪类,将其宽高设置为0。


    结果:

    通过对伪类的使用,很简单的制作了导航中经常碰到的问题。

    案例中,还有css3中的transition动画的使用,transform变形,background:linear-gradient();渐变的设置。

    显示全文