咱們在平常的開發中,常常會遇到這樣一個問題,就是如何實現居中水平垂直居中對齊。而且在面試中也會出現這樣的問題,可是咱們每每回答的不是很所有,而致使沒有獲得面試加分。接下來咱們經過不一樣的方式來實現,讓咱們成功破解這道面試。css
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
<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>
複製代碼
writing-mode 屬性定義了文本在水平或垂直方向上如何排布。 兼容性上還有些小瑕疵,但大部分瀏覽器已經支持。 面試
<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>
複製代碼
<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 {
width: 200px;
height: 200px;
border: 1px solid red;
text-align: center;
}
.box::after {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
img {
vertical-align: middle;
}
</style>
複製代碼
<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 {
width: 200px;
height: 200px;
border: 1px solid #ccc;
text-align: center;
font-size: 0;
}
.box::before {
display: inline-block;
vertical-align: middle;
content: '';
height: 100%;
}
img {
vertical-align: middle;
}
</style>
複製代碼
這裏總結了不少種垂直水平居中的方法,可是確定不是最徹底的,還有不少其餘中方式。可是整體的思路能夠總結爲如下幾點。 瀏覽器