CSS完美分页样式
这个教程要说明的是如何为搜索结果或更长记录列表设计分页。因为经常有人问我这个问题,所以我决定发一个帖子来说明如何用少量的 HTML 和 CSS 代码设计出完美的分页样式。
![](http://hiphotos.baidu.com/oceaniver/pic/item/a950fcf257561b07b07ec514.jpg)
这个教程也包含有实现Flickr、Digg式的简洁分页样式的HTML/CSS代码,随时可以应用于你的WEB项目中。
Update as March 17, 2008: My friends Simone Saveri solved some issuse with IE6 and IE7.
![](http://bp0.blogger.com/_TqPdHmAEwTM/R6Ds89u3tkI/AAAAAAAAA0U/biqUma6R5w4/s400/tag_orange.png)
![](http://bp0.blogger.com/_TqPdHmAEwTM/R6Ds89u3tkI/AAAAAAAAA0U/biqUma6R5w4/s400/tag_orange.png)
典型的分页结构
下图显示的是一个典型分页结构
![](http://hiphotos.baidu.com/oceaniver/pic/item/b3470e23fe1dde5a92580715.jpg)
一般你可以看出有四个主要元素:
-前一页/后一页 按钮(不可用)
-当前页
-标准的页面选择器
-前一页/后一页 按钮(可用)
你可以使用包含<li>标签(每个<li>均单独对应一个分页元素)的HTML列表 (<ul>) 来设计这样的结构,并分配一个ID给<ul>列表以便为当前列表应用具体的分页样式。看看下面这些教程以作说明。
Flickr式的分页:HTML 代码
想象你要设计的Flickr式分页样式就像下面这样:
![](http://hiphotos.baidu.com/oceaniver/pic/item/35ef191709c931154a90a710.jpg)
<ul id="pagination-flickr">
<li class="previous-off">«Previous</li>
<li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
</ul><li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
现在,你只需重新定义CSS标签的属性(ul, li, a)。
Flickr式分页: CSS 代码
非常简单:
ul{border:0; margin:0; padding:0;}
#pagination-flickr li{
#pagination-flickr a{
#pagination-flickr .previous-off,
#pagination-flickr .next-off {
#pagination-flickr .next a,
#pagination-flickr .previous a {
#pagination-flickr .active{
#pagination-flickr a:link,
#pagination-flickr a:visited {
#pagination-flickr a:hover{
#pagination-flickr li{
border:0; margin:0; padding:0;
font-size:11px;
list-style:none;
}font-size:11px;
list-style:none;
#pagination-flickr a{
border:solid 1px #DDDDDD;
margin-right:2px;
}margin-right:2px;
#pagination-flickr .previous-off,
#pagination-flickr .next-off {
color:#666666;
display:block;
float:left;
font-weight:bold;
padding:3px 4px;
}display:block;
float:left;
font-weight:bold;
padding:3px 4px;
#pagination-flickr .next a,
#pagination-flickr .previous a {
font-weight:bold;
border:solid 1px #FFFFFF;
} border:solid 1px #FFFFFF;
#pagination-flickr .active{
color:#ff0084;
font-weight:bold;
display:block;
float:left;
padding:4px 6px;
}font-weight:bold;
display:block;
float:left;
padding:4px 6px;
#pagination-flickr a:link,
#pagination-flickr a:visited {
color:#0063e3;
display:block;
float:left;
padding:3px 6px;
text-decoration:none;
}display:block;
float:left;
padding:3px 6px;
text-decoration:none;
#pagination-flickr a:hover{
border:solid 1px #666666;
}
Digg式的分页:HTML 代码
好了,现在你想设计的Digg式的分页样式就像:
![](http://hiphotos.baidu.com/oceaniver/pic/item/9731484a9be9ce3709f7ef12.jpg)
把上一个教程中的HTML 结构复制出来并粘贴。你只需更改<ul> 的ID(替换"pagination-digg" 为 "pagination-flickr")
<ul id="pagination-digg">
<li class="previous-off">«Previous</li>
<li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
</ul><li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
Digg式分页: CSS 代码
CSS代码与前面Flickr式分页的例子非常相似。你只需修改一些属性,并把#pagination-flikr更改为 #pagination-digg,CSS的分页元素不需修改:
ul{border:0; margin:0; padding:0;}
#pagination-digg li{
#pagination-digg a{
#pagination-digg .previous-off,
#pagination-digg .next-off {
#pagination-digg .next a,
#pagination-digg .previous a {
#pagination-digg .active{
#pagination-digg a:link,
#pagination-digg a:visited {
#pagination-digg a:hover{
#pagination-digg li{
border:0; margin:0; padding:0;
font-size:11px;
list-style:none;
margin-right:2px;
}font-size:11px;
list-style:none;
margin-right:2px;
#pagination-digg a{
border:solid 1px #9aafe5
margin-right:2px;
}margin-right:2px;
#pagination-digg .previous-off,
#pagination-digg .next-off {
border:solid 1px #DEDEDE
color:#888888
display:block;
float:left;
font-weight:bold;
margin-right:2px;
padding:3px 4px;
}color:#888888
display:block;
float:left;
font-weight:bold;
margin-right:2px;
padding:3px 4px;
#pagination-digg .next a,
#pagination-digg .previous a {
font-weight:bold;
} #pagination-digg .active{
background:#2e6ab1;
color:#FFFFFF;
font-weight:bold;
display:block;
float:left;
padding:4px 6px;
}color:#FFFFFF;
font-weight:bold;
display:block;
float:left;
padding:4px 6px;
#pagination-digg a:link,
#pagination-digg a:visited {
color:#0e509e
display:block;
float:left;
padding:3px 6px;
text-decoration:none;
}display:block;
float:left;
padding:3px 6px;
text-decoration:none;
#pagination-digg a:hover{
border:solid 1px #0e509e
}
简洁的分页样式:HTML 代码
如果你喜欢小巧,简洁的设计,那么这个例子将告诉你如何设计出如下图所示简洁的分页样式:
![](http://hiphotos.baidu.com/oceaniver/pic/item/a875d716566a230c972b4312.jpg)
HTML 结构和前面两个例子一样。你只需要做的是把<ul> 的ID更改为 "pagination-clean":
<ul id="pagination-clean">
<li class="previous-off">«Previous</li>
<li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
</ul><li class="active">1</li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li><a href="?page=6">6</a></li>
<li><a href="?page=7">7</a></li>
<li class="next"><a href="?page=2">Next »</a></li>
简洁的分页样式:CSS代码
这个分页样式的CSS代码如下:
ul{border:0; margin:0; padding:0;}
#pagination-clean li{
#pagination-clean li, #pagination-clean a{
#pagination-clean .previous-off,
#pagination-clean .next-off {
#pagination-clean .next a,
#pagination-clean .previous a {
#pagination-clean .active{
#pagination-clean a:link,
#pagination-clean a:visited {
#pagination-clean a:hover{
#pagination-clean li{
border:0; margin:0; padding:0;
font-size:11px;
list-style:none;
}font-size:11px;
list-style:none;
#pagination-clean li, #pagination-clean a{
border:solid 1px #DEDEDE
margin-right:2px;
}margin-right:2px;
#pagination-clean .previous-off,
#pagination-clean .next-off {
color:#888888
display:block;
float:left;
font-weight:bold;
padding:3px 4px;
}display:block;
float:left;
font-weight:bold;
padding:3px 4px;
#pagination-clean .next a,
#pagination-clean .previous a {
font-weight:bold;
border:solid 1px #FFFFFF;
} border:solid 1px #FFFFFF;
#pagination-clean .active{
color:#00000
font-weight:bold;
display:block;
float:left;
padding:4px 6px;
}font-weight:bold;
display:block;
float:left;
padding:4px 6px;
#pagination-clean a:link,
#pagination-clean a:visited {
color:#0033CC
display:block;
float:left;
padding:3px 6px;
text-decoration:none;
}display:block;
float:left;
padding:3px 6px;
text-decoration:none;
#pagination-clean a:hover{
text-decoration:none;
}