CSS居中方案介紹
1. 水平居中
1.1. transform居中
通過transform
居中的核心思想是讓居中元素先通過margin-left
屬性向右移動50%,然後再利用transform
屬性左移元素寬度的一半,從而達到居中的效果:
.parent {
position:relative;
width: 300px;
height: 400px;
background-color: red;
}
.child {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
height: 200px;
background-color: yellow;
left: 50%;
transform: translateX(-50%);
}
<div class="parent">
<div class="child"></div>
</div>
1.2. flex居中
利用flex居中是一種更為簡單的方式,通過設定父元素的justify-content
為center
即可。
.parent2 {
margin-top: 20px;
width: 300px;
height: 100px;
display: flex;
justify-content: center;
background-color: red;
}
.child2 {
background-color: yellow;
}
<div class="parent2">
<div class="child2">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
1.3. inline-block
通過將塊級子元素
設定inline-block
屬性,然後再將父元素的text-align
屬性設定為center
即可:
.parent1 {
text-align: center;
width: 300px;
height: 30px;
background-color: red;
margin-top: 30px;
}
.child1 {
display: inline-block;
background-color: yellow;
}
<div class="parent1">
<div class="child1">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
1.4. margin居中
通過設定子元素的margin
來使其居中:
.parent3 {
position:relative;
width: 300px;
height: 100px;
background-color: red;
margin-top: 20px;
}
.child3 {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
background-color: yellow;
left: 0;
right: 0;
margin: 0 auto;
}
<div class="parent3">
<div class="child3">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
2. 垂直居中
2.1 translate居中
.parent4 {
position:relative;
width: 300px;
height: 400px;
background-color: red;
margin-top: 20px;
}
.child4 {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
height: 200px;
background-color: yellow;
top: 50%;
transform: translateY(-50%);
}
<div class="parent4">
<div class="child4">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
同樣的,結合1.1
就可以實現水平、垂直居中:
.parent5 {
position:relative;
width: 300px;
height: 300px;
background-color: red;
margin-top: 20px;
}
.child5 {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
height: 200px;
background-color: yellow;
top: 50%;
left: 50%;
transform: translate3D(-50%, -50%, 0);
}
<div class="parent5">
<div class="child5">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
2.2 flex居中
利用flex的align-items
屬性可以解決flex的垂直居中:
.parent6 {
margin-top: 20px;
width: 300px;
height: 100px;
display: flex;
align-items: center;
background-color: red;
}
.child6 {
background-color: yellow;
}
<div class="parent6">
<div class="child6">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
同樣的,結合1.2
可以做到水平、垂直均居中:
.parent7 {
margin-top: 20px;
width: 300px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
background-color: red;
}
.child7 {
background-color: yellow;
}
<div class="parent7">
<div class="child7">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
2.3 絕對定位
利用絕對定位同樣可以實現垂直居中:
.parent8 {
position:relative;
width: 300px;
height: 200px;
background-color: red;
margin-top: 20px;
}
.child8 {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
height: 100px;
background-color: yellow;
top: 0;
bottom: 0;
margin: auto 0;
}
<div class="parent8">
<div class="child8">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
再參考1.4
的方案,可以做到水平,垂直均居中:
.parent9 {
position:relative;
width: 300px;
height: 200px;
background-color: red;
margin-top: 20px;
}
.child9 {
position: absolute; /*第一個position不為static的父元素為其父元素*/
width: 200px;
height: 100px;
background-color: yellow;
top: 0;
bottom: 0;
left: 0;
right: 0px;
margin: auto auto;
}
<div class="parent9">
<div class="child9">fsdjfklajsdklfklasjfkljksdlf</div>
</div>
相關文章
- css居中方案彙總CSS
- 各種CSS居中方案CSS
- css介紹CSS
- 【CSS三種居中方案全解】CSS水平垂直居中常用方法集結CSS
- CSS 建立介紹CSS
- CSS全面介紹CSS
- CSS居中CSS
- CSS 簡單介紹CSS
- CSS 文字格式介紹CSS
- CSS垂直居中和水平居中CSS
- CSS水平居中和垂直居中CSS
- CSS之居中CSS
- CSS div居中CSS
- CSS 文字居中CSS
- CSS 垂直居中CSS
- CSS文件流通俗介紹CSS
- CSS尺寸單位介紹CSS
- 03-CSS初步介紹CSS
- CSS設計模式介紹CSS設計模式
- CSS水平居中和垂直居中的方法CSS
- css實現居中CSS
- css-居中篇CSS
- CSS div居中效果CSS
- css水平垂直居中CSS
- css樣式說明介紹CSS
- div垂直居中-CSS元素垂直居中方法CSS
- CSS居中常見方法CSS
- css居中與佈局CSS
- CSS之居中佈局CSS
- CSS居中方法大全CSS
- css居中對齊大全CSS
- css居中幾種方法CSS
- css中居中總結CSS
- CSS未知高度垂直居中CSS
- CSS 多行文字居中CSS
- CSS垂直居中方法CSS
- 常用前端佈局,CSS技巧介紹前端CSS
- ie 居中方案
- CSS3 clip-path 用法介紹CSSS3