国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

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

2024-07-11 08:34:36
字體:
來源:轉載
供稿:網友

CSS的居中有水平居中和垂直居中,這兩種居中又分為行內元素居中和塊級元素居中,不同的居中用不同方法。
水平居中
1、行內元素水平居中(文本,圖片)
給父層設置 text-align:center; 可以實現行內元素水平居中。


<!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 href=”http://huoche.7234.cn/images/jb51/kdzm1c4zsmf.jpg” width=”248″ height=”162″ alt=””/>
</div>
</body>
</html>

2、確定寬度塊級元素水平居中
確定寬度的塊級元素水平居中,常用的有 margin:0 auto; 相信很多開發人員都用的是這個,不過本人認為還有更好的寫法:margin-left:auto;margin-right:auto; 因為 margin-top 和 margin-bottom 在重置 css 時就已經寫了為 0 了。


<!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、不確定寬度的塊級元素水平居中
不確定寬度的塊級元素有三種方法實現。
方法一:


<!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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建昌县| 巴林左旗| 郯城县| 庆元县| 咸丰县| 万盛区| 扎赉特旗| 安丘市| 六盘水市| 英吉沙县| 玉门市| 云林县| 枣阳市| 河曲县| 怀宁县| 侯马市| 成都市| 汉川市| 扎鲁特旗| 安丘市| 饶河县| 灌阳县| 济南市| 思南县| 泾阳县| 饶河县| 乐清市| 淅川县| 湘乡市| 金坛市| 孟州市| 怀宁县| 紫云| 册亨县| 郎溪县| 红河县| 遵义市| 南郑县| 偏关县| 五峰| 乐安县|