为什么我的网站上会显示水平滚动条?

时间:2016-02-28 03:12:49

标签: html css

我正在制作一个网站和一个水平滚动条显示。当然,我可以使用overflow-x来解决我的所有问题,但我想知道问题的根源。有人能告诉我滚动条出现的原因吗?这是我的css:

body {
    background-image: url("rain.jpg");
    background-repeat: no-repeat;
    background-size: 100% 800px;
}

#header {
    width: 83%;
    height: 70px;
    background-color: blue;
    border: cyan solid 3px;
    border-radius:20px;
    position: relative;
    top: 20px;
    margin: auto;
}

p {
    font-size:30px;
    font-weight: bold;
    color: yellow;  
    position: relative;
    right: -450px;
    top: -8px;
}

#container {
    position: relative;
    width: 83%;
    height: 1000px;
    top: 50px;
    margin: auto;
    background-color: blue;
}

.img {
    height: 150px;
    width: 225px;
    padding: 0px;
    padding-top: 30px;
    cursor: pointer;
    opacity: 1;
}

.click {
    height: 400px;
    width: 600px;
    position: relative;
    right: -200px;
    cursor: pointer;
}

li {
    display: inline-block;
}

2 个答案:

答案 0 :(得分:5)

请注意,您的<p>元素位于相对位置,并使用right: -450px向右移动。

使用position: relative保留元素的原始空间。因此,当您向右移动元素450px时,布局中的原始空间保持不变,文档水平延长。这就是滚动条的原因。

enter image description here

删除或调整right: -450px规则以查看差异。

另外,仅仅为了对比,使用relative定位切换absolute,从文档流中删除元素,并消除原始空间。

Read more about CSS positioning at MDN.

答案 1 :(得分:3)

我指出这一点。

Method
相关问题