---
title: "div按钮CSS"
author: "uule"
date: "2015-02-12T11:07:59Z"
tags: []
categories: ["Web前端"]
slug: "div-css"
draft: false
lastmod: "2015-02-12T11:11:03Z"
id: 2185321
url: "https://uule.iteye.com/blog/2185321"
---

整体效果：

![](http://dl2.iteye.com/upload/attachment/0106/0259/5c90b5e0-6d71-3d4e-a0fb-9c8434917ebe.jpg)  
 

HTML：

```
<div class="sav">
	<span class="left"><div class="btn3"><a href="javascript:void(0)" onclick="thxFriend()">感谢好友</a></div></span>
	<span class="right"><div class="btn"><a href="/activity/2015love/toAddLove.html">表白好友</a></div></span>				
</div>
<div class="sav">
	<div class="btn_tnk"><a href="/activity/2015love/thxList.html">查看接受感谢好友</a></div>
</div>
```

 

分析： 

一、![](http://dl2.iteye.com/upload/attachment/0106/0261/da8d9992-6105-35e1-a3c7-e1b6c2d14dc5.jpg)

 

sav区域为图中深色部分，中间浅色部分为其内两个span间距，黄色阴影为外边距

**外层DIV设置width：90%，然后再设置margin-left:5%，即可使其居中**

 

**二、span**

![](http://dl2.iteye.com/upload/attachment/0106/0267/bbd79e19-7f72-37f7-9424-5b580dcb4f63.jpg)  
 

外层 DIV sav宽度为90%，里面两个span若都为50%，则其中就没有间距，所以设置每个span为48%，其中**左span向左浮动，右span向右浮动**

 

**三、DIV模拟按钮**

![](http://dl2.iteye.com/upload/attachment/0106/0271/7c1e8a09-e3dc-3a12-8533-aabb955a14d4.jpg)  
**外层同样是样式为sav的DIV（黑色阴影部分）**。 

![](http://dl2.iteye.com/upload/attachment/0106/0273/7a2d9479-68b6-3e9d-8671-c625192a3eaa.jpg)  
 

 

**1、按钮都使用DIV模拟**

外层一个DIV定位，内层用DIV模拟按钮，**如图中下方按钮样式为 btn\_tnk，上方按钮样式为btn和btn3**

 

2、div设置height/line-height:

height是DIV的高度

line-height是行高

line-height等于height时，可使DIV中字垂直居中

 

 

整体CSS：

```
<div class="sav">
	<span class="left"><div class="btn3"><a href="javascript:void(0)" onclick="thxFriend()">感谢好友</a></div></span>
	<span class="right"><div class="btn"><a href="/activity/2015love/toAddLove.html">表白好友</a></div></span>				
</div>
<div class="sav">
	<div class="btn_tnk"><a href="/activity/2015love/thxList.html">查看接受感谢好友</a></div>
</div>

总结：
.sav { width: 90%; margin-left: 5%; margin-top: 10px; float: left;}
.sav span.left { width: 48%; float:left;}
.sav span.right { width: 48%; float:right;}

.btn { 
	width: 100%;
	height: 36px;
	line-height: 36px;
	text-align: center;
	background: #fe8855;
	color:#fff;
	font-size: 18px;
	/*border-radius:5px;
	-moz-border-radius:5px;
	-webkit-border-radius:5px;*/
	}
.btn a:link, .btn a:visited { color:#fff; text-decoration: none; height: 36px; display: block;}

.btn3 { 
	width: 100%;
	height: 36px;
	line-height: 36px;
	text-align: center;
	background: #7de1bb;
	color:#fff;
	font-size: 18px;
	/*border-radius:5px;
	-moz-border-radius:5px;
	-webkit-border-radius:5px;*/
	}
.btn3 a:link, .btn3 a:visited { color:#fff; text-decoration: none; height: 36px; display: block;}

.btn_tnk { 
	width: 100%;
	height: 36px;
	line-height: 36px;
	text-align: center;
	background: #3bca94;
	color:#fff;
	font-size: 18px;
	/*border-radius:5px;
	-moz-border-radius:5px;
	-webkit-border-radius:5px;*/
	}
.btn_tnk a:link, .btn_tnk a:visited { color:#fff; text-decoration: none; height: 36px; display: block;}
```

 。。。