国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 河北区| 湖南省| 瑞丽市| 天气| 特克斯县| 泸州市| 沂源县| 澄城县| 龙岩市| 通渭县| 白沙| 甘洛县| 四子王旗| 韶关市| 柳州市| 韩城市| 金昌市| 理塘县| 鹤峰县| 昌平区| 和政县| 高安市| 博白县| 瑞昌市| 务川| 临汾市| 衡东县| 澄江县| 北票市| 海城市| 荆门市| 黔西县| 芦山县| 北流市| 大城县| 年辖:市辖区| 井研县| 雷州市| 沙雅县| 哈尔滨市| 东海县|