当前位置: 首页>編程日記>正文

居中对齐的几种方法

居中对齐的几种方法

居中对齐的几种方法

看面试题,自己总结了下,顺便写了对应例子,加深印象。

水平居中

div设置一个宽度,再添加margin: 0 auto

必须要添加宽度,只对块级元素有效

<style>.container {width: 400px;height: 400px;background-color: pink;color: #fff;}.box {width: 100px;height: 100px;background-color: purple;margin: 0 auto;}.inline-block-box {display: inline-block;width: 100px;height: 100px;background-color: purple;margin: 0 auto;}
</style><div class="container"><div class="box">块级元素</div><span class="inline-block-box">行内块元素</span><br><span>行内元素</span>
</div>

image-20220324213321588


给父元素添加text-aligin: center

只对行内块元素、行内元素有效

.container {width: 400px;height: 400px;background-color: pink;color: #fff;text-align: center;
}.box {width: 100px;height: 100px;background-color: purple;
}.inline-block-box {display: inline-block;width: 100px;height: 100px;background-color: purple;
}span {background-color: skyblue;
}

image-20220324214157484


水平垂直居中

计算法

父元素跟着子元素margin-top移动问题

开始之前,先看下一个小问题

下面的例子中,我们想要让子元素离父元素有距离

<style>.container {width: 600px;height: 400px;background-color: pink;color: #fff;}.box {width: 100px;height: 100px;background-color: purple;margin: 150px auto;}
</style><div class="container"><div class="box"></div>
</div>

image-20220324220516244


结果,子元素并没有外边距效果,反而是父元素出现了外边距的效果。

这是因为,根据规范,父元素的子元素的上边距(margin-top),如果碰不到有效的border或者padding,就会一层一层的找自己的祖先元素,直到找到祖先元素有有效的borderborder为止


解决方案:

  1. 给父元素添加padding-top
  2. 给父元素添加border-top
  3. 给父元素添加overflow: hidden(推荐)
.container {width: 600px;height: 400px;background-color: pink;color: #fff;/* padding-top: 1px; *//* border-top: 1px solid transparent; */overflow: hidden;
}

开始

首先margin左右可以直接设置auto实现居中,但是的上下不行。

计算法:margin上下值 = (父元素高度-子元素高度)/2

在这个例子中,父元素的高度为400px,子元素的高度为100px,所以margin上下值设置为150px

.container {width: 600px;height: 400px;background-color: pink;color: #fff;overflow: hidden;
}.box {width: 100px;height: 100px;background-color: purple;margin: 150px auto;
}

image-20220324222200326


绝对定位四0法

设置四个方向都为0,然后设置marginauto,因为宽高固定,所以对应方向平分,可以实现水平垂直居中

.container {position: relative;width: 600px;height: 400px;background-color: pink;color: #fff;
}.box {position: absolute;width: 200px;height: 100px;top: 0;right: 0;bottom: 0;left: 0;margin: auto;background-color: purple;
}

绝对定位 + 计算法(margin负值)

.container {position: relative;width: 600px;height: 400px;background-color: pink;color: #fff;
}.box {position: absolute;width: 200px;height: 100px;top: 50%;left: 50%;margin: -50px 0 0 -100px;/* margin-top、margin-left 分别是该子元素高宽的一半(负值)*/background-color: purple;
}

绝对定位 + transform

.container {position: relative;width: 600px;height: 400px;background-color: pink;color: #fff;
}.box {position: absolute;width: 200px;height: 100px;top: 50%;left: 50%;transform: translate(-50%, -50%);background-color: purple;
}

flex布局法

.container {display: flex;width: 600px;height: 400px;background-color: pink;/* 垂直居中 */align-items: center;/* 水平居中 */justify-content: center;}.box {width: 200px;height: 100px;background-color: purple;
}

对于宽高不定的元素,后面两种方法(绝对定位+transformflex布局法),可以实现元素的水平垂直居中。


参考资料:

  • 104道 CSS 面试题,助你查漏补缺

  • 父盒子跟随子盒子margin-top移动问题


https://www.fengoutiyan.com/post/14514.html

相关文章:

  • 左右居中对齐怎么设置
  • 居中对齐
  • 如何居中对齐
  • 居中对齐在哪里
  • 居中对齐怎么设置
  • 居中左对齐
  • 居中对齐是什么
  • 居中右对齐
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機