用CSS3做个好看的按钮(2)

转载须写明出处,附带本文链接。关于文中的示例,请用Firefox 3.6、Chrome 4.x、Safari 4.x查看。

点击查看演示

上一篇文章中提到了用CSS3的gradient做按钮,后来朋友指出HTML用的标签太多,趁着有空又仔细的看了下官方文档,上面说到Mozilla currently only supports CSS gradients as values of the background-image property, as well as within the shorthand background. You specify a gradient value instead of an image URL. 原来gradient是代替background-image的,那么就可以用gradient做遮罩,然后再用background-color设定按钮背景色,这样就可以去掉三个标签。代码如下:

.button {
	position: relative;
	width: 160px;
	height: 40px;
	line-height: 40px;
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	border-radius: 5px;
	-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, .5);
	-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, .5);
	box-shadow: 0 1px 3px rgba(0, 0, 0, .5);
	
	background: -moz-linear-gradient(
		270deg,
		transparent,
		transparent 50%,
		rgba(0, 0, 0, .3) 50%				
	);
	background: -webkit-gradient(
		linear,
		left top,
		left bottom,
		from(transparent), 
		to(rgba(0, 0, 0, .3)),
		color-stop(.5 ,transparent),
		color-stop(.5 ,rgba(0, 0, 0, .3))
	);
}
.mask {			
	top: 1px;
	left: 1px;
	right: 1px;
	bottom: 1px;
	position: absolute;

	text-align: center;
	color: #FFF;
	text-decoration: none;
	
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	border-radius: 5px;
	background: -moz-linear-gradient(
		270deg,
		rgba(117, 117, 117, .4),
		rgba(94, 94, 94, .4) 50%,
		transparent 50%
	);
	background: -webkit-gradient(
		linear,
		left top,
		left bottom,
		from(rgba(117, 117, 117, .4)), 
		to(transparent),
		color-stop(.5 ,rgba(94, 94, 94, .4)),
		color-stop(.5, transparent)
	);
	border: 1px solid rgba(255, 255, 255, .3);
	border-bottom-color: rgba(255, 255, 255, .1);
}

点击查看演示

发表评论