gpt4 book ai didi

html - 为什么是top :50% in css not working?

转载 作者:技术小花猫 更新时间:2023-10-29 11:53:15 25 4
gpt4 key购买 nike

我正在制作一个网站,我正在尝试垂直居中:

 position: absolute;
width:1200px;
height:600px;
top: 50%;
left: 50%;
margin-left: -600px;

我的 HTML 是单个 div

最佳答案

使用位置 - 需要静态高度

i{
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
position: absolute;
top: 50%;
left: 50%;
}
<i>center</i>

编辑:使用 Flex (2021-10-19)

i {
display: flex;
align-items: center; /* vertical centering if flex-direction: row */
justify-content: center; /* horizontal centering if flex-direction: row */

/* extra styles */
background-color: limeGreen;
min-height: 160px;
}
<i>center</i>

enter code here

关于html - 为什么是top :50% in css not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com