日期:2013-11-30  浏览次数:21005 次

网页制造aiyiweb文章简介:div宽度100%,内部元素浮动,div居中.

01.<div class="pagelist_b"> 
02.    <div class="page"> 
03.        <span class="totlerecord">5</span> 
04.        <span class="totlepages">1/5</span> 
05.        <span class="current">1</span> 
06.        <a href="#">2</a> 
07.        <a href="#">3</a> 
08.        <a href="#">4</a> 
09.        <a href="#">5</a> 
10.        <a href="#">></a> 
11.    </div> 
12.</div> 

在这个结构中,“.pagelist_b”是外部框架,100%宽度。而目的,是为了让“.page”块能够居中显示。可是由于“.page”块中的节点并不固定,无法确认其最终宽度,再者,“.page”块中的内容需求定义特殊的款式,而被附上“display:block;”属于将其块元素化,而导致“.page”中的内容无法完满的居中。

这两天突然无意中看到网上的相关文章,想起了position:relative;(绝对浮动)的玩法。大致非常通俗的说明一下这里的思路:“.page”向右浮动50%,是以外层“.pagelist_b”的宽度为标准的;内层(如A,SPAN)向左浮动50%,是以层“.page”的宽度为标准的,这里无需定义内层的宽度。这样算是一个小技巧,让“.page”居中了。也补足了float没有center属性的缺陷。如果无法正常显示,记得在page_b加多一句overflow:visible;

01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
02.<html xmlns="http://www.w3.org/1999/xhtml"> 
03. <head> 
04.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
05.    <meta http-equiv="Content-Language" content="utf-8" /> 
06.    <title> new document </title> 
07.    <meta name="robots" content="all" /> 
08.    <meta name="generator" content="editplus" /> 
09.    <meta name="author" content="hayden@yeah.net,hayden" /> 
10.    <meta name="keywords" content="" /> 
11.    <meta name="description" content="" /> 
12.    <link rel="stylesheet" rev="stylesheet" href="" type="text/css" media="all" /> 
13.    <script type="text/javascript" src=""></script> 
14.    <style type="text/css"> 
15.* { word-wrap: break-word; margin: 0; padding: 0;} 
16. 
17..pagelist_b {width:100%;height:80px;text-align: center; } 
18..page {position:relative;left:50%;float:left;} 
19..page span {position:relative;left:-50%;border: 1px solid #ddd;display: inline;float:left;padding:0 6px;height:30px;line-height:30px;margin:2px;} 
20..page span.totlerecord{} 
21..page span.current{cursor:pointer;} 
22..page a:link,.page a:visited {border: 1px solid #ccc;display: inline;float:left;padding:0 6px;height:30px;line-height:30px;margin:2px;text-decoration: none;position:relative;left:-50%;color:#666} 
23..page a:hover {border:1px solid #666;} 
24. 
25.    </style>&