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

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

【技巧】CSS 居中的三種方法

2024-04-27 15:15:04
字體:
供稿:網(wǎng)友

這里寫圖片描述

演示地址:點(diǎn)我

1、完美居中:絕對定位 + transform,適用于元素寬高未知的大多數(shù)情況

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> .parent { position: relative; height: 300px; width: 300px; background: #ddd;; } .child { position: absolute; height: 100px; width: 100px; left: 50%; top: 50%; transform: translate(-50%, -50%); background: #333; } </style></head><body> <div class="parent"> <div class="child"></div> </div></body></html>

2、完美居中:flex布局,需要兼容flex

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> .parent { display: flex; justify-content: center; align-items: center; height: 300px; width: 300px; background: #ddd; } .child { height: 100px; width: 100px; background: #333; } </style></head><body> <div class="parent"> <div class="child"></div> </div></body></html>

3、內(nèi)聯(lián)元素水平居中:text-align,適用于常規(guī)文檔流中的內(nèi)聯(lián)元素(span、a、input等),也包括inline-block元素

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> .parent { text-align: center; height: 300px; width: 300px; background: #ddd; } .child { height: 50px; width: 100px; background: #aaa; } </style></head><body> <div class="parent"> <span class="child">span</span><br> <input type="text" class="child" value="input"><br> <div class="child" style="display: inline-block;">inline-block</div><br> </div></body></html>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 襄樊市| 白玉县| 瑞丽市| 新田县| 汝南县| 阿勒泰市| 余姚市| 雷波县| 凤阳县| 海口市| 临湘市| 邵东县| 宁化县| 玛纳斯县| 新野县| 贵德县| 鄄城县| 紫阳县| 文成县| 丹棱县| 邢台县| 洪湖市| 青冈县| 杭州市| 青浦区| 疏附县| 临潭县| 马龙县| 汉寿县| 阳曲县| 日土县| 靖安县| 公主岭市| 察哈| 依安县| 余江县| 咸丰县| 马鞍山市| 清远市| 青川县| 隆化县|