HTML / CSS Div在放大时不会滚动

时间:2016-07-12 13:58:44

标签: html css scroll

我的网页上有一个侧边栏,其中包含一个段落和4个按钮。当我放大我的网页时,我想要一个滚动条显示滚动所有这些,但它没有出现。

这是我的代码:

HTML               

<title>Calendar</title>
<link rel = "stylesheet" type = "text/css" href = "calendarstyle.css" />

</head>

<script src = "calendarapp.js"></script>

<body>

<div id = "sidebar">
    <div id = "viewmode">
        <p id = "viewlabel">View Mode</p>
        <button class = "viewbutton" id = "yearbutton">Year</button>
        <button class = "viewbutton" id = "monthbutton">Month</button>
        <button class = "viewbutton" id = "weekbutton">Week</button>
        <button class = "viewbutton" id = "daybutton">Day</button>
    </div>
</div>

</body>
</html>

CSS

* {
    margin: 0px;
    padding: 0px;
    font-family: Monospace;
}

#sidebar {
    background-color: rgb(243, 243, 243);
    height: 100%;
    width: 20%;
    position: fixed;
}

#viewmode {
    position: absolute;
    width: 100%;
}

#container {
    width: 100%;
    height: 100px;
    top: 0px;
    left: 0px;
    position: absolute;
    overflow-y: scroll;
}

#viewlabel {
    margin-top: 5px;
    font-size: 20px;
    text-align: center;
    font-weight: bold;
    color: rgb(200, 200, 200);
}

.viewbutton {
    display: block;
    width: calc(100% - 10px);
    margin: 5px;
    background-color: rgb(200, 200, 200);
    color: white;
    padding: 3px;
    outline: none;
    border: none;
    font-size: 17px;
    transition-duration: 0.2s;
}

.viewbutton:hover {
    background-color: rgb(106, 234, 116);
}

我一直在尝试使用&#34;容器&#34; div,但没有什么对我有用

3 个答案:

答案 0 :(得分:3)

add `overflow-y:auto;` to sidebar class it will work

* {
    margin: 0px;
    padding: 0px;
    font-family: Monospace;
}

#sidebar {
    background-color: rgb(243, 243, 243);
    height: 100%;
    width: 20%;
    position: fixed;
    overflow-y:auto;
  
}

#viewmode {
    position: absolute;
    width: 100%;
}

#container {
    width: 100%;
    height: 100px;
    top: 0px;
    left: 0px;
    position: absolute;
    overflow-y: scroll;
}

#viewlabel {
    margin-top: 5px;
    font-size: 20px;
    text-align: center;
    font-weight: bold;
    color: rgb(200, 200, 200);
}

.viewbutton {
    display: block;
    width: calc(100% - 10px);
    margin: 5px;
    background-color: rgb(200, 200, 200);
    color: white;
    padding: 3px;
    outline: none;
    border: none;
    font-size: 17px;
    transition-duration: 0.2s;
}

.viewbutton:hover {
    background-color: rgb(106, 234, 116);
}
<title>Calendar</title>
<link rel = "stylesheet" type = "text/css" href = "calendarstyle.css" />

</head>

<script src = "calendarapp.js"></script>

<body>

<div id = "sidebar">
    <div id = "viewmode">
        <p id = "viewlabel">View Mode</p>
        <button class = "viewbutton" id = "yearbutton">Year</button>
        <button class = "viewbutton" id = "monthbutton">Month</button>
        <button class = "viewbutton" id = "weekbutton">Week</button>
        <button class = "viewbutton" id = "daybutton">Day</button>
    </div>
</div>

</body>
</html>

答案 1 :(得分:1)

请参阅此处: jsfiddle

你不需要#container,因为你已经拥有一个包裹你所有东西的div #viewmode

只需将其添加到#viewmode并从css

中删除#container即可
overflow-y: scroll;
height:100px;

css代码:

#viewmode {
position: absolute;
width: 100%;
overflow-y: scroll;
height:100px;
}

答案 2 :(得分:1)

您只需将overflow-y: auto;添加到#sidebar

即可

&#13;
&#13;
* {
  margin: 0px;
  padding: 0px;
  font-family: Monospace;
}
#sidebar {
  background-color: rgb(243, 243, 243);
  height: 100%;
  width: 20%;
  position: fixed;
  overflow-y: auto;
}
#viewmode {
  position: absolute;
  width: 100%;
}
#container {
  width: 100%;
  height: 100px;
  top: 0px;
  left: 0px;
  position: absolute;
  overflow-y: scroll;
}
#viewlabel {
  margin-top: 5px;
  font-size: 20px;
  text-align: center;
  font-weight: bold;
  color: rgb(200, 200, 200);
}
.viewbutton {
  display: block;
  width: calc(100% - 10px);
  margin: 5px;
  background-color: rgb(200, 200, 200);
  color: white;
  padding: 3px;
  outline: none;
  border: none;
  font-size: 17px;
  transition-duration: 0.2s;
}
.viewbutton:hover {
  background-color: rgb(106, 234, 116);
}
&#13;
<div id="sidebar">
  <div id="viewmode">
    <p id="viewlabel">View Mode</p>
    <button class="viewbutton" id="yearbutton">Year</button>
    <button class="viewbutton" id="monthbutton">Month</button>
    <button class="viewbutton" id="weekbutton">Week</button>
    <button class="viewbutton" id="daybutton">Day</button>
  </div>
</div>
&#13;
&#13;
&#13;