最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

CSS的各種居中——如何書寫高質(zhì)量代碼

  發(fā)布時(shí)間:2012-11-20 08:58:13   作者:佚名   我要評(píng)論
CSS的居中有水平居中和垂直居中,這兩種居中又分為行內(nèi)元素居中和塊級(jí)元素居中,不同的居中用不同方法。 水平居中 1、行內(nèi)元素水平居中(文本,圖片) 給父層設(shè)置 text-align:center; 可以實(shí)現(xiàn)行內(nèi)元素水平居中。 [code] <!DOCTYPE HTML> <html lang=&qu
CSS的居中有水平居中和垂直居中,這兩種居中又分為行內(nèi)元素居中和塊級(jí)元素居中,不同的居中用不同方法。
水平居中
1、行內(nèi)元素水平居中(文本,圖片)
給父層設(shè)置 text-align:center; 可以實(shí)現(xiàn)行內(nèi)元素水平居中。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.center{text-align:center;}
</style>
</head>
<body>
<div class="center">
<a >谷歌搜索</a><br/><br/>
<img src="cat.jpg" width="248" height="162" alt=""/>
</div>
</body>
</html>

2、確定寬度塊級(jí)元素水平居中
確定寬度的塊級(jí)元素水平居中,常用的有 margin:0 auto; 相信很多開發(fā)人員都用的是這個(gè),不過本人認(rèn)為還有更好的寫法:margin-left:auto;margin-right:auto; 因?yàn)?margin-top 和 margin-bottom 在重置 css 時(shí)就已經(jīng)寫了為 0 了。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.center{width:100px;height:100px;margin-left:auto;margin-right:auto;background:green;}
</style>
</head>
<body>
<div class="center"></div>
</body>
</html>

3、不確定寬度的塊級(jí)元素水平居中
不確定寬度的塊級(jí)元素有三種方法實(shí)現(xiàn)。
方法一:

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;}
table{margin-left:auto;margin-right:auto;}
.demo li{float:left;display:inline;margin-right:5px;}
.demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
.demo a:hover{background:white;color:#316ac5;}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<ul class="demo">
<li><a href="#">1</a></li>
</ul>
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>
</td>
</tr>
</tbody>
</table>
</body>
</html>

這里用到了 table 標(biāo)簽來實(shí)現(xiàn)不確定寬度的塊級(jí)元素水平居中。table 本身不是塊級(jí)元素,如果不給它設(shè)定寬度的話,會(huì)由內(nèi)部元素的寬度“撐開”,但即使不設(shè)定它的寬度,僅設(shè)置 margin-left:auto 和 margin-right:auto 就可以實(shí)現(xiàn)水平居中。
這種方法很巧妙,但是增加了無語義標(biāo)簽,加深了標(biāo)簽的嵌套層數(shù)。
方法二

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;}
.wrapper{width:500px;height:100px;background:black;}
.demo{text-align:center;padding:5px;}
.demo li{display:inline;}
.demo a{padding:2px 6px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
.demo a:hover{background:white;color:#316ac5;}
</style>
</head>
<body>
<div class="wrapper">
<ul class="demo">
<li><a href="#">1</a></li>
</ul>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>
</div>
</body>
</html>

方法二是改變?cè)氐?display 值,使塊級(jí)元素變成行內(nèi)元素,然后使用 text-align:center 使其居中。相對(duì)于方法一,它不用增加無語義標(biāo)簽,簡(jiǎn)化了標(biāo)簽的嵌套深度,但它也存在一定的問題:它將塊級(jí)元素變成了行內(nèi)元素,這樣就失去了一些塊級(jí)元素的功能,比如設(shè)置寬度,高度。
方法三

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;}
.wrapper{width:500px;height:100px;background:black;}
.demo{clear:both;padding-top:5px;float:left;position:relative;left:50%;}
.demo li{display:inline;float:left;margin-right:5px;position:relative;left:-50%;}
.demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
.demo a:hover{background:white;color:#316ac5;}
</style>
</head>
<body>
<div class="wrapper">
<ul class="demo">
<li><a href="#">1</a></li>
</ul>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
<ul class="demo">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>
</div>
</body>
</html>

方法三通過給父層設(shè)置浮動(dòng)和相對(duì)定位以及 left:50%,子元素設(shè)置相對(duì)定位和 left:-50% 來實(shí)現(xiàn)水平居中。它可以保留塊級(jí)元素的功能,而且不會(huì)添加無語義標(biāo)簽,不增加嵌套深度,但是設(shè)置了相對(duì)定位,會(huì)帶來一定的副作用。
這三種方法各有優(yōu)缺點(diǎn),具體使用哪種方法可以視具體情況而定。
垂直居中
1、父層高度不確定的垂直居中
通過給父層設(shè)置相同的上下內(nèi)邊距實(shí)現(xiàn)。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
.demo{width:500px;color:white;margin-bottom:10px;padding-top:20px;padding-bottom:20px;background:black;}
.content{width:200px;height:50px;background:red;}
</style>
</head>
<body>
<div class="demo">hello world</div>
<div class="demo"><img src="cat.jpg" width="248" height="162" alt=""/></div>
<div class="demo"><div class="content"></div></div>
</body>
</html>

2、父層高度確定的單行文本垂直居中
通過給父層設(shè)置行高來實(shí)現(xiàn),行高和父層高度相同。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
.demo{width:500px;color:white;background:black;height:100px;line-height:100px;}
</style>
</head>
<body>
<div class="demo">hello world</div>
</body>
</html>

3、父層高度確定的多行文本、圖片、塊級(jí)元素垂直居中
方法一
說到垂直居中,css 中有個(gè)用于垂直居中的屬性 vertical-align,但只有在父層為 td 或者 th 時(shí),這個(gè)屬性才會(huì)生效,對(duì)于其他塊級(jí)元素,例如 div、p 等,默認(rèn)情況是不支持的。在 firefox 和 ie8 下,可以設(shè)置塊級(jí)元素的 display 值為 table-cell,來激活 vertical-align 屬性,但 ie6,7 并不支持,所以這種方法沒有辦法跨瀏覽器兼容。但是我們可以使用 table。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
.wrapper{background:black;width:500px;color:white;height:100px;}
.demo{width:200px;background:red;height:50px;}
</style>
</head>
<body>
<table>
<tr>
<td class="wrapper">
hellow world<br/>
hellow world<br/>
hellow world<br/>
</td>
</tr>
</table>
<table>
<tr>
<td class="wrapper">
<img src="cat.jpg" alt=""/>
</td>
</tr>
</table>
<table>
<tr>
<td class="wrapper">
<div class="demo"></div>
</td>
</tr>
</table>
</body>
</html>

table 可以很好的實(shí)現(xiàn)垂直居中效果,但是它添加了無語義標(biāo)簽,增加了嵌套深度。
方法二
對(duì)支持 display:table-cell 的 ie8 和 firefox 用 display:table-cell 和 vertical-align:middle 來實(shí)現(xiàn)居中,對(duì)不支持 display:table-cell 的 ie6 和 ie7 使用 hack 寫法。

復(fù)制代碼
代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
.mb{margin-bottom:10px;}
.wrapper{background:black;width:500px;color:white;height:100px;margin-bottom:10px;display:table-cell;vertical-align:middle;*position:relative;}
.demo{width:200px;background:red;height:50px;}
.vam{*position:absolute;*top:50%;}
.va{*position:relative;*top:-50%;}
</style>
</head>
<body>
<div class="mb10">
<div class="wrapper">
<div class="vam">
<div class="va">
hellow world<br/>
hellow world<br/>
hellow world
</div>
</div>
</div>
</div>
<div class="mb10">
<div class="wrapper">
<div class="vam">
<img src="cat.jpg" alt=""/>
</div>
</div>
</div>
<div class="mb10">
<div class="wrapper">
<div class="vam">
<div class="va demo"></div>
</div>
</div>
</div>
</body>
</html>

利用 hack 技術(shù)區(qū)別對(duì)待 firefox、ie8 和 ie6、ie7,在不支持 display:table-cell 的 ie6 和 ie7 下,通過給父子兩層元素分別設(shè)置 top:50% 和 top:-50% 來實(shí)現(xiàn)居中。這種方法的好處是沒有增加額外的標(biāo)簽,但缺點(diǎn)也很明顯,一方面它使用了 hack,不利于維護(hù),另一方面,它設(shè)置了 position:relative 和 position:absolute,帶來了副作用。

相關(guān)文章

最新評(píng)論

安西县| 贵溪市| 凤翔县| 仙游县| 武乡县| 东台市| 南昌县| 黔西| 庐江县| 大足县| 临洮县| 北川| 西安市| 吉木萨尔县| 紫云| 辉县市| 瓮安县| 凌源市| 区。| 曲阜市| 红原县| 万州区| 福清市| 六安市| 郯城县| 凉城县| 敖汉旗| 宿松县| 桂东县| 浦江县| 建平县| 寻甸| 宿迁市| 同心县| 抚顺县| 东方市| 凤山市| 金堂县| 呼和浩特市| 佛山市| 铁岭市|