【发布时间】:2018-07-21 03:53:25
【问题描述】:
当在另一个 div 中居中时,我在桌面上成功了,我尝试在 chrome 的内置“检查”分辨率更改中,但在“移动”上,div 保持在另一个 div 的底部。谁能帮忙,看看为什么 div 以“桌面模式”为中心,而不是以“移动模式”为中心?谢谢
.headercontainer {
overflow-y: hidden;
}
.header-title {
width: 80%;
height: 100px;
left: 50%;
top: 50%;
font-size: 2em;
z-index: -1;
position: fixed;
margin-left: -40%;
margin-top: -50px;
height: 100px;
transform: translateY(-50%);
}
.header-title h1 {
color: white;
text-shadow: 2px 2px 4px #d1d1d1;
font-family: 'Raleway', sans-serif;
white-space: nowrap;
text-transform: uppercase;
text-align: center;
}
.header-image {
background-image: url("images/Westminster.jpg");
background-position: center center;
max-width: 100%;
margin-left: auto;
margin-right: auto;
padding: 5em;
height:60vh;
background-size: cover;
background-attachment: fixed;
background-repeat: no-repeat;
position:relative;
z-index: -3;
}
<div class = "headercontainer">
<div class = "header-image">
<div class = "header-title">
<h1> Anarchists and Autocrats </h1>
</div>
</div>
</div>
【问题讨论】:
-
你应该使用 flex 来居中。
-
您可能需要同时为
head、body和.headercontainer设置height: 100%