首页
/
每日頭條
/
圖文
/
css居中是什麼代碼
css居中是什麼代碼
更新时间:2024-10-08 03:25:53

css居中是什麼代碼?我是poetry,點擊上方“關注”,每天為你分享前端進階與個人精進幹貨,下面我們就來說一說關于css居中是什麼代碼?我們一起去了解并探讨一下這個問題吧!

css居中是什麼代碼(CSS居中解決方案荟萃)1

css居中是什麼代碼

我是poetry,點擊上方“關注”,每天為你分享前端進階與個人精進幹貨。

水平居中方案方案一:text-align inline-block

<div id="parent1"> <div class="child">水平居中</div> </div>

#parent1{ text-align: center; background:#ddd; margin-bottom:20px; } #parent1 .child{ display: inline-block; background:#666; color:#fff; }

方案二:margin:0 auto

<div id="parent2"> <div class="child">水平居中</div> </div>

#parent2{ text-align: center; background:#ddd; margin-bottom:20px; } #parent2 .child{ display: table; margin: 0 auto; background:#666; color:#fff; }

方案三:absolute transform

<div id="parent3"> <div class="child">水平居中</div> </div>

#parent3{ position: relative; background:#ddd; margin-bottom:20px; } #parent3 .child{ position: absolute; left: 50%; transform: translateX(-50%); background:#666; color:#fff; }

方案四:justify-content

<div id="parent4"> <div class="child">水平居中</div> </div>

#parent4{ display: flex; justify-content: center; background:#ddd; margin-bottom:20px; } #parent4 .child{ margin:0 auto; background:#666; color:#fff; }

垂直居中方案方案一: 利用 line-height 實現垂直居中
  • 這種方法适用于單行文本垂直居中,如果文本内容太長,出現了換行,換行後的内容會溢出

<div id="example1"> 單行文字垂直居中 </div>

#example1 { height: 100px; line-height: 100px; background: #161616; color: #fff; width: 200px; }

方案二 利用 display: table 實現垂直居中

<div id="example2"> <div class="inner">塊區域垂直居中</div> </div>

#example2 { height: 100px; background: #161616; color: #fff; width: 400px; overflow: hidden; display: table; margin-bottom:20px; } #example2 .inner{ display: table-cell; vertical-align: middle; height: 50px; background:#999; }

方案三 margin 填充 這種方法需要知道内外容器的大小

<div id="example3"> <div class="inner">塊區域垂直居中</div> </div>

#example3 { height: 100px; background: #161616; color: #fff; width: 400px; overflow: hidden; margin-bottom:20px; } #example3 .inner{ margin-left: auto; margin-right: auto; margin-top: calc((100px - 50px)/2); height: 50px; background:#999; }

方案四:經典 absolute 布局上下文垂直居中

<div id="example4"> <div class="inner">塊區域垂直居中</div> </div>

#example4 { width: 400px; height: 100px; background: #161616; color: #fff; position: relative; margin-bottom:20px; } #example4 .inner{ height: 50px; width: 200px; position: absolute; left: 50%; top: 50%; margin-top: -25px; margin-left: -100px; background:#999; }

方案五:absolute transform

<div id="example5"> <div class="inner">塊區域垂直居中</div> </div>

#example5 { width: 400px; height: 100px; background: #161616; color: #fff; position: relative; margin-bottom:20px; } #example5 .inner{ position: absolute; left: 50%; top: 50%; background: #999; transform: translateX(-50%) translateY(-50%); }

方案六 利用margin:auto 居中

<div id="expample6"> <div class="inner">Content here</div> </div>

#expample6 { width: 400px; height: 100px; background: #eee; position: relative; margin-bottom:20px; } #expample6 .inner { position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; height: 50px; width: 70%; background: #aaa; color:#222; }

方案七 利用 Flex布局 居中

<div id="expample7"> <div class="inner">Content here</div> </div>

#expample7 { width: 400px; height: 100px; background: #eee; display: flex; justify-content: center; align-items: center; } #expample7 .inner { height: 50px; width: 70%; background: #aaa; color:#222; }

作者介紹:poetry,專注前端進階寫作與個人精進幹貨,目前在上市公司負責小程序等相關的研發。

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
可以家養的龍蝦品種
可以家養的龍蝦品種
可以家養的龍蝦品種?藍色的龍蝦你們吃過嗎?這兩天,上海人自己養的藍龍蝦開捕啦作為吃貨的@魔都新鮮事-新聞晨報第一時間跑到現場嘗鮮,發現這種龍蝦比普通龍蝦更鮮美,肉質也更細嫩一了解才知道,原來這是目前世界上最名貴的淡水經濟蝦種之一今年上海松江...
2024-10-08
山東發布知識産權強省建設綱要
山東發布知識産權強省建設綱要
齊魯網·閃電新聞4月26日訊今天是第22個世界知識産權日,山東省政府新聞辦召開新聞發布會,省市場監管局介紹全省知識産權發展與保護狀況,發布《2021年山東省知識産權發展與保護狀況》白皮書和2021年度知識産權行政保護典型案例。山東省知識産權...
2024-10-08
lv和tiffany
lv和tiffany
記者|黃姗編輯|樓婍沁1月7日,法國奢侈品巨頭LouisVuitton-MoëtHennessy(以下簡稱“LVMH集團”)宣布完成收購美國高級珠寶世家蒂芙尼(Tiffany&Co.)。蒂芙尼正式成為LVMH集團旗下第75個奢侈品牌;LVM...
2024-10-08
支付寶螞蟻莊園什麼時候上線
支付寶螞蟻莊園什麼時候上線
每天早上,小編都有一個鐵打不變的習慣,就是打開支付寶,在螞蟻森林收集能量和在螞蟻莊園給小雞喂食。可是今天早上,小編又被支付寶圈粉了,在螞蟻莊園中卻出現了新的功能,那就是--愛心小屋。小雞有家了!小雞有家了!小雞有家了!點擊小雞後面的小屋,這...
2024-10-08
什麼叫做食色性也
什麼叫做食色性也
原創文章,嚴禁盜用抄襲。如果您喜歡我的文字,歡迎點贊,收藏,關注,轉發,您的支持就是我寫下去的動力中國人在意名聲。活着的時候有争議,希望離開時能蓋棺論定。可現實情況往往不遂人願,棺材蓋上了,“定論”卻遲遲沒有。甚至還要被後人咀嚼個稀巴爛,甚...
2024-10-08
Copyright 2023-2024 - www.tftnews.com All Rights Reserved