无法将绝对div对齐在中心[重复]

问题描述 投票:0回答:2

可能它与它的位置是absolute这一事实有关,但你仍然如何将absolute定位于div

使用top,而不是margin-top作品,但left而不是margin-left不会影响它。为什么?

.title
{
  display:block;
  position: absolute;
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  height: 10%;
  top : 80%;
}




<div class="title fontHugeTitle2"><h1> The Best  In Town </h1></div>
css
2个回答
1
投票

left: auto; right: auto不会像margin: 0 auto一样用于居中div,但你有一个固定的宽度,你可以将left属性设置为剩下的一半,例如left: 5%


0
投票

div {
  width: 100%;
}

更改

<div class="title fontHugeTitle2"><h1> The Best  In Town </h1></div>

<div class="fontHugeTitle2"><h1 class="title"> The Best  In Town </h1></div>
© www.soinside.com 2019 - 2024. All rights reserved.