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

首頁 > 開發 > CSS > 正文

絕對定位元素的水平垂直居中的方法(3種任選)

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

1.css實現居中

缺點:需要提前知道元素的寬度和高度。

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        .box{            width: 600px;             height: 400px;            position: absolute;             left: 50%; top: 50%;            border:1px solid #000;            background:red;            margin-top: -200px;    /* 高度的一半 */            margin-left: -300px;    /* 寬度的一半 */        }    </style></head><body>    <div class="box">    </div></body></html>

2、css3實現水平垂直居中

注意:無論元素的尺寸是多少,都可以居中。不過IE8以上才兼容這種寫法,移動端可忽略。

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        .box{            width: 600px;             height: 400px;            position: absolute;             left: 50%;            top: 50%;            border:1px solid #000;            background:red;            transform: translate(-50%, -50%);    /* 50%為自身尺寸的一半 */        }    </style></head><body>    <div class="box">    </div></body></html>

3、margin:auto實現居中

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        .box{            width: 600px;             height: 400px;            position: absolute;             left: 0;            top: 0;             right: 0;             bottom: 0;            border:1px solid #000;            background:red;            margin: auto;    /* 有了這個就自動居中了 */        }    </style></head><body>    <div class="box">    </div></body></html>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到CSS教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大丰市| 古蔺县| 偏关县| 会昌县| 同心县| 武冈市| 延川县| 湘潭市| 慈利县| 赣榆县| 改则县| 温州市| 宕昌县| 无棣县| 中卫市| 马鞍山市| 吉林省| 绥宁县| 仁怀市| 游戏| 云林县| 门源| 甘肃省| 罗平县| 乐平市| 宝坻区| 日土县| 榆社县| 阿克陶县| 大渡口区| 开封市| 岗巴县| 石景山区| 凤凰县| 潞西市| 大理市| 桦川县| 新营市| 石楼县| 昂仁县| 乐东|