准备
创建元素
<div class="parent"> <div class="child">child</div> </div>
垂直水平居中方案一:知道宽度的情况下 absolute+margin负值
.parent {
width:400px;
height:400px;
background: red;
position: relative;
}
.child {
position: absolute;
left:50%;
top:50%;
background: yellow;
width:50px;
height:50px;
margin-left:-25px;
margin-top:-25px;
}
垂直水平居中方案二:不知道宽高的情况下 absolute+transform
.parent {
width:400px;
height:400px;
background: red;
position: relative;
}
.child {
position: absolute;
left:50%;
top:50%;
transform: translate(-50%,-50%);
}
垂直居中方案三:position+margin:auto
.parent {
position:relative;
width:200px;
height:200px;
background: red;
}
.child {
width:80px;
height:40px;
background: yellow;
position: absolute;
left:0;
top:0;
right:0 ;
bottom:0;
margin:auto;
}
垂直居中方案四:+ 多行文本的垂直居中 :table-cell+vertical-align:middle;
.parent {
height: 300px;
width:400px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child {
display: inline-block;
width:50px;
height:50px;
background: blue;
}
/* 或者 */
.parent {
width: 400px;
height: 300px;
display: table-cell;
vertical-align: middle;
border: 1px solid red;
text-align: center;
}
.child {
display: inline-block;
vertical-align: middle;
background: blue;
}
垂直居中方案五:display: flex
.parent {
width:400px;
height:200px;
background:red;
display: flex;
justify-content:center;
align-items:center;
}
.child {
height:100px;
width:100px;
background:green;
}
垂直居中方案六:伪元素
.parent {
width:200px;
height:200px;
background:red;
text-align: center;
}
.child {
height:100px;
width:100px;
background:yellow;
display: inline-block;
vertical-align: middle;
}
.parent:before {
content:"";
height:100%;
vertical-align: middle;
display: inline-block;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
css水平垂直居中,垂直水平居中
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“CSS水平垂直居中解决方案(6种)”评论...
更新动态
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]