我不是任何类型的CSS专家。但是,除了下面提供的ul/li列表之外,空白区似乎在任何其他部分都不起作用。
我玩过位置和其他的div。
html, body {
margin: 0;
padding: 0;
}
body {
background-color: rgb(184, 184, 184);
background-repeat: no-repeat;
background-position: center;
background-size: auto;
background-image: url('/images/landing.jpg');
font-family: Montserrat;
}
.header {
top: 0;
position: fixed;
background-color: gray;
width: 100%;
height: 60px;
justify-content: center;
align-items: center;
display: flex;
}
.menu {
position: fixed;
top: 6px;
right: 0;
}
.menu ul li {
display: inline;
}
.menu ul li a {
color: white;
text-decoration: none;
font-size: 16px;
font-weight: lighter;
padding: 0 20px;
}
.welcome {
position: absolute;
color: white;
font-size: 50px;
top: 459px;
margin: auto;
}HTML:
<div class='welcome'>
<span>Hello!</span>
</div>正在尝试将欢迎div设置为中心
发布于 2019-04-01 14:30:57
您正在绝对定位的元素上设置margin: auto。我认为您正在尝试将'welcome'元素居中(根据您的问题,这一事实并不清楚)。绝对定位的元素依赖于它的祖先位置和大小。
如果要将绝对定位的元素从父元素的左上角移动到50%,然后应用平移将其推到中心,则将其居中的众多解决方案之一:top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%)。
发布于 2019-04-01 14:28:18
尝试将您的welcome css类更改为:
.welcome {
position: absolute;
left: 50%;
transform: translate(-50%);
}发布于 2019-04-01 14:31:51
欢迎类的位置是绝对的,所以欢迎类的父类应该相对于中心的欢迎div。另外,如果您想水平居中放置div,则应该将margin: auto;更改为margin: 0 auto;
或者,您可以使父div显示为flex,然后使用align-items, justify-content to center。
.parent { display: flex, align-items: center, justify-content: center }https://stackoverflow.com/questions/55444516
复制相似问题