其他分享
首页 > 其他分享> > 实现多种水平垂直居中的布局(定宽高和不定宽高)

实现多种水平垂直居中的布局(定宽高和不定宽高)

作者:互联网

1、定宽高

一、绝对定位和负magin值

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>

二、绝对定位 + transform

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>

三、绝对定位 + left/right/bottom/top + margin

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
display: inline;
top: 0;
left: 0;
right: 0;
bottom: 0px;
background: yellow;
margin: auto;
height: 100px;
width: 100px;
}
</style>

四、flex布局

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.children-box {
background: yellow;
height: 100px;
width: 100px;
}
</style>

五、grid布局

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
width: 100px;
height: 100px;
background: yellow;
margin: auto;
}
</style>

六、table-cell + vertical-align + inline-block/margin: auto

<template>
<div id="app">
<div class="box">
<div class="children-box"></div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children-box {
width: 100px;
height: 100px;
background: yellow;
display: inline-block;// 可以换成margin: auto;
}
</style>

2、不定宽高

一、绝对定位 + transform

<template>
<div id="app">
<div class="box">
<div class="children-box">111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
background: yellow;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>

二、table-cell

<template>
<div id="app">
<div class="box">
<div class="children-box">111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children-box {
background: yellow;
display: inline-block;
}
</style>

三、flex布局

<template>
<div id="app">
<div class="box">
<div class="children-box">11111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.children-box {
background: yellow;
}
</style>

四、flex变异布局

<template>
<div id="app">
<div class="box">
<div class="children-box">11111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
}
.children-box {
background: yellow;
margin: auto;
}
</style>

五、grid + flex布局

<template>
<div id="app">
<div class="box">
<div class="children-box">11111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
background: yellow;
align-self: center;
justify-self: center;
}
</style>

六、gird + margin布局

<template>
<div id="app">
<div class="box">
<div class="children-box">11111111</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
background: yellow;
margin: auto;
}
</style>

七、writing-mode属性布局

<template>
<div id="app">
<div class="box">
<div class="children-box">
<p>11111</p>
</div>
</div>
</div>
</template>
<style type="text/css">
.box {
width: 200px;
height: 200px;
border: 1px solid red;
writing-mode: vertical-lr;
text-align: center;
}

.box>.children-box {
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}

.box>.children-box>p {
display: inline-block;
margin: auto;
text-align: left;
background: yellow;
}
</style>

3、番外(图片定高|不定高水平垂直居中)

一、table-cell

<template>
<div id="app">
<div class="box">
<img src="https://ss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/242dd42a2834349b406751a3ceea15ce36d3beb6.jpg">
</div>
</div>
</template>
<style type="text/css">
.box {
height: 200px;
width: 200px;
display: table-cell;
text-align: center;
border: 1px solid #ccc;
vertical-align: middle;
}
</style>

 

 

一、内联元素居中布局

  1. 水平居中
  1. 垂直居中

二、块级元素居中布局

  1. 水平居中
  1. 垂直居中
链接:https://juejin.im/post/5db706d5f265da4d31073959

标签:居中,box,align,宽高,height,width,不定,display,200px
来源: https://www.cnblogs.com/leo-hxl/p/11763972.html