-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第204题:如何实现元素的水平垂直居中? #515
Comments
水平居中1. 行内元素若是行内元素,给其父元素设置 <div class="parent">
<span class="child">测试</span>
</div>
<style>
.parent {
background-color: aqua;
text-align: center; /* 水平居中 */
}
.child {
color: #fff;
background-color: blueviolet;
}
</style> 2. 块级元素2.1 宽高固定当宽高固定时,以下 html 示例: <div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
height: 100px;
background-color: aqua;
}
.child {
width: 100px;
height: 100px;
background-color: blueviolet;
}
</style> 以下四种方式显示效果均为: 方式一:margin:0 auto<style>
.child {
margin:0 auto;
}
</style> 方式二:absolute + margin-left<style>
.child {
position: absolute;
margin-left: -50px;
left: 50%;
}
</style> 方式三:absolute + calc<style>
.child {
position: absolute;
left: calc(50% - 50px);
}
</style> 方式四:absolute + left + right + margin-left<style>
.child {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
</style> 2.2 宽高不定当宽高不定时,以下测试示例: <div class="parent">
<div class="child">测试示例</div>
</div>
<style>
.parent {
height: 100px;
background-color: aqua;
}
.child {
background-color: blueviolet;
color: #fff;
}
</style> 以下三种方式显示效果均为: 方式一:使用 CSS3 中新增的 transform 属性<style>
.child {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
</style> 方式二:flex 布局<style>
.child {
display: flex;
justify-content: center;
}
</style> 方式三:width: fit-content<style>
.child {
width: fit-content;
margin: 0 auto;
}
</style>
垂直居中1. 行内元素代码示例: <div class="parent">
<span class="child">测试示例</span>
</div>
<style>
.parent {
height: 100px;
background-color: aqua;
text-align: center; /* 水平居中 */
}
.child {
color: #fff;
background-color: blueviolet;
}
</style> 方式一:line-height(单行文本)<style>
.child {
line-height: 100px;
}
</style> 当多行时会样式错乱,需要用到 方式二:display: table-cell + vertical-align (多行文本)可用 vertical-align 属性, 而 为了使用 <style>
.parent {
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}
</style> 方式三:display: inline-block + vertical-align 隐式幽灵节点设置幽灵节点的高度以及幽灵节点的基线(通过 <style>
.parent {
line-height: 100px; /* 通过 line-height 设置幽灵节点的基线 */
}
.child {
vertical-align: middle;
line-height: normal; /* 块级元素时需要加 */
display: inline-block; /* 重点,要把 line-height 设置成 normal, 要不然会继承100 */
}
</style> 方式四:display: grid 布局<style>
.parent {
display: grid;
}
.child {
margin: auto;
}
</style> 效果如上 方式五:writing-mode 布局
<style>
.parent {
writing-mode: vertical-lr;
}
.child {
writing-mode: horizontal-tb;
}
</style> 效果如上 2. 块级元素参照 水平居中的块级元素布局 ,同样把对水平方向的转换为垂直方向的 2.1 宽高固定示例代码: <div class="parent">
<div class="child"></div>
</div>
<style>
body {
background-color: aqua;
}
.child {
width: 50px;
height: 50px;
background-color: blueviolet;
}
</style> 以下几种方式显示效果均为: 方式一:absolute + margin-top<style>
.child {
position: absolute;
margin-left: -25px;
left: 50%;
margin-top: -25px;
top: 50%;
}
</style> 方式二:absolute + calc<style>
.child {
position: absolute;
left: calc(50% - 25px);
top: calc(50% - 25px);
}
</style> 方式三:absolute + left + right + top + bottom<style>
.child {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style> 2.2 宽高不定当宽高不定时,以下测试示例: <div class="parent">
<div class="child">测试示例</div>
</div>
<style>
.parent {
height: 100px;
background-color: aqua;
}
.child {
background-color: blueviolet;
}
</style> 以下两种方式显示效果均为: 方式一:使用 CSS3 中新增的 transform 属性需要设定 parent 为相对定位( <style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50px;
transform: translate(-50%, -50%);
}
</style> 方式二:flex 布局<style>
.parent {
display: flex;
height: 100%;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
</style> |
No description provided.
The text was updated successfully, but these errors were encountered: