【发布时间】:2021-09-26 08:09:30
【问题描述】:
我不知道如何使背景颜色适合视口大小。我使用了 100vh,它正确地覆盖了高度。但是,当我使用 100vw 时,背景只覆盖屏幕的右侧而不是左侧。
这是我的小提琴代码的link。
这是我的代码:
HTML
<div id="main-doc">
<section id="welcome-section">
<div class="center-text">
<h1 id="title">Hello, I am Lee</h1>
<p>xxx</p>
</div>
</section>
</div>
CSS
#main-doc{
text-align: center;}
#welcome-section{
background-color: #2A3D45;
background-size: cover;
padding-top: 25px;
width: 100vw;
height: 100vh;
.center-text{
position: absolute;
width: 200px;
height: 50px;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -25px;}
【问题讨论】:
-
我刚刚测试了您的代码,一切正常,它确实覆盖了整个页面,包括宽度。我不知道你为什么说它只涵盖右边
-
@Anonymous 在我的屏幕上没有......你知道为什么吗?
-
hmm @RynellLee 可能是因为边距和填充,参考下面的my answer
-
@RynellLee 请检查我的回答,希望对您有所帮助。这是应用浏览器默认 CSS 的问题,为此我们需要 CSS -resets。