如何實現水平垂直居中?
實現居中效果的方法:
1、用absolute + 負margin
father{
> position: relative;//父元素設定絕對定位
> }
> .son{
> position: absolute;
> width:100px;
height:100px;
> top: 50%;
> left: 50%;
> margin-left: -50px;
> margin-top: -50px;
> }
2、absolute + margin auto
.father{
position: relative;//父元素設定絕對定位
}
.son{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
3、absolute + transform
.father{
position: relative;//父元素設定絕對定位
}
.son{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
4、absolute + calc,
.father{
position: relative;//父元素設定絕對定位
}
.son{
position: absolute;
width:100px;
height:100px;
top: calc(50%-50px);
left: calc(50%-50px);
}
5、flex佈局
.father{
display: flex;
justify-content: center; /*顯示在主軸的中間*/
align-items: center; /*子項在側軸中間位置*/
}
6、grid佈局
.father{
display:grid;
align-items:center;
justify-items:center;
}
7、css新增的table屬性
.father {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.son {
display: inline-block;
}
8、text-align和line-height
.father{
text-align: center;
width: 100px;
height: 100px;
background: indianred;
line-height: 100px;
}
相關文章
- 16種方法實現水平居中垂直居中
- css 水平垂直居中實現方式CSS
- 水平垂直居中的實現方法
- 淺談居中問題(水平居中、垂直居中、水平垂直居中)
- 水平居中和垂直居中
- CSS垂直居中和水平居中CSS
- CSS水平居中和垂直居中CSS
- 水平居中、垂直居中、水平垂直居中、浮動居中、絕對定位居中…….幫你搞定
- 如何實現婚戀app原始碼中元素水平垂直居中?APP原始碼
- 元素水平垂直居中三種方法實現
- css實現垂直水平居中的幾種方法CSS
- div實現水平垂直居中的幾種方法
- css實現水平垂直居中的幾種方式CSS
- CSS實現水平垂直居中的方式有哪些?CSS
- 元素垂直水平居中
- div 水平垂直 居中
- css水平垂直居中CSS
- 元素水平居中,垂直居中方法
- 水平垂直居中佈局的多種實現方式
- 三行CSS程式碼實現水平垂直居中CSS
- CSS水平居中和垂直居中的方法CSS
- CSS實現水平垂直居中的1010種方式(史上最全)CSS
- CSS 實現元素在當前視窗水平垂直居中CSS
- CSS視窗垂直水平居中CSS
- CSS實現水平、垂直居中,N種方法,徹底說透!CSS
- 設定圖片水平垂直居中
- 元素自適應水平垂直居中
- 不定寬度下,元素的垂直居中,水平居中
- 【20190129】CSS-垂直水平居中相關CSS
- CSS div水平垂直居中效果詳解CSS
- web前端技巧-文字如何垂直居中?多行文字如何實現上下居中?Web前端
- CSS實現垂直居中的問題CSS
- 網頁設計如何優雅的實現垂直居中網頁
- 討論下垂直水平居中的多種方案
- CSS元素(文字、圖片)水平垂直居中方法CSS
- 面試題:水平垂直居中的17種方法面試題
- 直播系統app原始碼,元素水平垂直居中APP原始碼
- 一起搞懂 CSS 水平居中與垂直居中的16個方法CSS