【发布时间】:2021-12-01 19:08:02
【问题描述】:
我需要在我的网站中居中放置一个调查表,但是当我尝试时,它不起作用。我不知道怎么了。有人可以帮帮我吗?
这是我的Pen。这是我的 CSS 代码:
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.4;
color: var(--color-white);
margin: 0;
}
/* mobile friendly alternative to using background-attachment: fixed */
body::before {
content: '';
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
background: var(--color-darkblue);
background-image: linear-gradient(
115deg,
rgba(58, 58, 158, 0.8),
rgba(136, 136, 206, 0.7)
),
url(https://cdn.freecodecamp.org/testable-projects-fcc/images/survey-form-background.jpeg);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.container {
width: 100%;
margin: 3.125rem auto 0 auto;
}
【问题讨论】:
-
你为什么要绝对定位 body 元素?这样做会使 body 的宽度缩小到其内容所需的宽度。因此,您的表单 在正文中居中 - 但您没有“看到”这一点,因为当两侧没有空间时“居中”当然毫无意义。
-
@Magda 如果以下答案之一对您来说足够清楚,请投票并验证答案