赞
踩
我们在日常的开发中,经常会遇到这样一个问题,就是如何实现水平垂直居中对齐。并且在面试中也会出现这样的问题,但是我们往往回答的不是很全部,而导致没有得到面试加分。接下来我们通过不同的方式来实现。
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
position: relative;
}
.children {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
position: relative;
}
.children {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
position: relative;
}
.children {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
display: inline;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: flex;
justify-content: center;
align-items: center;
}
.children {
width: 100px;
height: 100px;
background-color: red;
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: grid;
}
.children {
width: 100px;
height: 100px;
background-color: red;
margin: auto;
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children {
width: 100px;
height: 100px;
background-color: red;
/* 下方这两个用哪个都可以 */
margin: auto;
/* display: inline-block;s */
}
<div class="parent">
<div class="children"></div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
position: relative;
}
.children {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
position: relative;
}
.children {
background-color: red;
color: white;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children {
background-color: red;
color: white;
display: inline-block;
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: flex;
justify-content: center;
align-items: center;
}
.children {
background-color: red;
color: white;
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: flex;
}
.children {
background-color: red;
color: white;
margin: auto;
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: grid;
}
.children {
background-color: red;
color: white;
align-self: center;
justify-self: center;
}
<div class="parent">
<div class="children">楠♥竹</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
display: grid;
}
.children {
background-color: red;
color: white;
margin: auto;
}
<div class="parent">
<div class="children">
<p>楠♥竹</p>
</div>
</div>
.parent {
width: 300px;
height: 300px;
border: 1px solid rgb(1, 131, 227);
writing-mode: vertical-lr;
text-align: center;
}
.parent>.children {
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}
.parent>.children>p {
display: inline-block;
margin: auto;
text-align: left;
background-color: red;
color: white;
}
writing-mode 属性定义了文本在水平或垂直方向上如何排布。兼容性上还有些小瑕疵,但大部分浏览器已经支持。
如果你感觉文章不咋地
//(ㄒoㄒ)//
,就在评论处留言,作者继续改进;o_O???
如果你觉得该文章有一点点用处,可以给作者点个赞;\\*^o^*//
如果你想要和作者一起进步,可以微信扫描二维码,关注前端老L;~~~///(^v^)\\\~~~
谢谢各位读者们啦(^_^)∠※
!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。