目录
水平居中
行内元素
父元素是块级元素,给父元素设置text-align:center
不是,先将父元素设置为块级,再给父元素设置text-align:center
块级元素
方案一:宽度定:谁需要居中,就给谁设置,margin:0 auto
宽度不定:默认子元素和父元素宽度一定,需要设置子元素为:display:inline-block;或display:inline,将其转换成行内块级元素/行内元素,给父元素设置taxt-align:center
方案二:使用定位属性(子绝父相 left50% transform: translateX(-50%))
方案三:使用flexbox布局实现(宽度定不定都可以),使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;
垂直居中
单行的行内元素
多行的行内元素:使用给父元素设置display:table-cell;和vertical-align: middle;属即可;
块级元素
方案一:使用定位(子绝父相 top tranform:translateY(-50%);
方案二:使用flexbox布局实现(高度定不定都可以)父元素 display: flex; align-items: center;
水平垂直居中
1.flex布局,父级设置justify-content和align-items
2.flex布局,子级设置margin:auto
3.flex布局,子级设置margin与align-self;其中align-self是会脱离父级的控制
4.grid布局,子级设置margin
5.grid布局,子级设置justify-self和align-self,分别表示水平和垂直
6.calc方式,50%减去子级高度的一半
7.用绝对定位,子绝父相,子级设置margin
8.绝对定位 ,子绝父相top,left 50%.transform:tanslate(-50%,-50%)
9.table-cell,父级vertiacl-align:middle,text-align:center子级display:inline-block
10.一行文字居中
水平居中
行内元素
父元素是块级元素,给父元素设置text-align:center
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
text-align: center;
}
</style>
<div id="father">
<span id="son">我是行内元素</span>
</div
不是,先将父元素设置为块级,再给父元素设置text-align:center
<style>
#father {
display: block;
width: 500px;
height: 300px;
background-color: skyblue;
text-align: center;
}
</style>
<span id="father">
<span id="son">我是行内元素</span>
</span>
块级元素
方案一:宽度定:谁需要居中,就给谁设置,margin:0 auto
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
}
#son {
width: 100px;
height: 100px;
background-color: green;
margin: 0 auto;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
宽度不定:默认子元素和父元素宽度一定,需要设置子元素为:display:inline-block;或display:inline,将其转换成行内块级元素/行内元素,给父元素设置taxt-align:center
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
text-align: center;
}
#son {
background-color: green;
display: inline;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
方案二:使用定位属性(子绝父相 left50% transform: translateX(-50%))
首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,即让子元素的左上角水平居中;
定宽度:设置绝对子元素的 margin-left: -元素宽度的一半px; 或者设置transform: translateX(-50%);
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
position: relative;
}
#son {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 50%;
margin-left: -50px;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
不定宽度:利用css3新增属性transform: translateX(-50%);
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
position: relative;
}
#son {
height: 100px;
background-color: green;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
方案三:使用flexbox布局实现(宽度定不定都可以),使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
display: flex;
justify-content: center;
}
#son {
width: 100px;
height: 100px;
background-color: green;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
垂直居中
-
单行的行内元素
只需要设置单行行内元素的"行高等于盒子的高"即可;
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
}
#son {
background-color: green;
height: 300px;
}
</style>
<div id="father">
<span id="son">我是单行的行内元素</span>
</div>
-
多行的行内元素:使用给父元素设置display:table-cell;和vertical-align: middle;属即可;
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
display: table-cell;
vertical-align:middle;
}
#son {
background-color: green;
}
</style>
<div id="father">
<span id="son">我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素</span>
</div>
-
块级元素
方案一:使用定位(子绝父相 top tranform:translateY(-50%);
首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的top: 50%,即让子元素的左上角垂直居中;
定高度:设置绝对子元素的 margin-top: -元素高度的一半px; 或者设置transform: translateY(-50%);
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
position: relative;
}
#son {
height: 100px;
background-color: green;
position: absolute;
top: 50%;
margin-top: -50px;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
不定高度:利用css3新增属性transform: translateY(-50%);
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
position: relative;
}
#son {
width: 100px;
background-color: green;
position: absolute;
left: 50%;
transform: translateY(-50%);
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
方案二:使用flexbox布局实现(高度定不定都可以)父元素 display: flex; align-items: center;
<style>
#father {
width: 500px;
height: 300px;
background-color: skyblue;
display: flex;
align-items: center;
}
#son {
width: 100px;
height: 100px;
background-color: green;
}
</style>
<div id="father">
<div id="son">我是块级元素</div>
</div>
水平垂直居中
<style>
.wrap {
width: 600px;
height: 600px;
border: 2px solid purple;
}
.item {
width: 150px;
height: 150px;
background-color: seagreen;
}
.allCenter {}
.allCenterChild {}
</style>
<body>
<div class="wrap allCenter">
<div class="item allCenterChild">
</div>
</div>
</body>
效果:
1.flex布局,父级设置justify-content和align-items
.allCenter {
/* 第一种:flex布局 */
display: flex;
/* 水平居中 */
justify-content: center;
/* 垂直居中 */
align-items: center;
}
.allCenterChild {}
2.flex布局,子级设置margin:auto
/* 第二种也是flex布局,子级 */
.allCenter{
display: flex;
}
.allCenterChild{
margin: auto;
}
3.flex布局,子级设置margin与align-self;其中align-self是会脱离父级的控制
/* 第三种 */
.allCenter{
display: flex;
}
.allCenterChild{
/* align-self会脱离父级对他的控制 */
align-self: center;
margin: auto;
}
4.grid布局,子级设置margin
.allCenter{
display: grid;
}
.allCenterChild{
margin: auto;
}
5.grid布局,子级设置justify-self和align-self,分别表示水平和垂直
.allCenter{
display: grid;
}
.allCenterChild{
justify-self: center;
align-self: center;
}
6.calc方式,50%减去子级高度的一半
.allCenter{
}
.allCenterChild{
margin:calc(50% - 75px);
}
7.用绝对定位,子绝父相,子级设置margin
.allCenter{
position: relative
}
.allCenterChild{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
8.绝对定位 ,子绝父相top,left 50%.transform:tanslate(-50%,-50%)
.allCenter{
position: relative
}
.allCenterChild{
position: absolute;
left: 50%;
top: 50%;
/* 移动为子级自身的50% */
transform: translate(-50%,-50%);
}
9.table-cell,父级vertiacl-align:middle,text-align:center子级display:inline-block
.allCenter{
/* 对行级有效 */
display: table-cell;
vertical-align: middle;
text-align: center;
}
.allCenterChild{
display: inline-block;
}
10.一行文字居中
<div class="online">content</div>
.online{
margin-top: 50px;
width: 160px;
height: 160px;
background:pink;
text-align: center;
/* 一行文本垂直居中:行高设置为父亲高度 */
line-height: 160px;
}
更多推荐
水平垂直居中
发布评论