I have a strange issue with my html page (see code below). The problem is that the scrollbar on the "left-section" does not cover the entire height of the section. It only shows when the window is made small enogh to cover "Link 18". At that point the scrollbar shows up, but does not scroll beyond "Link 18" (in fact shows only half of it).
The problem goes away if I remove the code within the <header>
tags. I.e., the scorllbar covers the entire height of the column. I’m sure I am not setting up my page correctly. Please help.
- Kamran
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two Columns with Independent Scrollbars</title>
<style>
/* Styles for the container */
.container {
display: flex;
height: 100vh;
}
/* Styles for the left section */
.left-section {
width: 30%;
padding: 20px;
background-color: #f0f0f0;
overflow-y: auto; /* Add a vertical scrollbar to the left section */
}
/* Styles for the right section */
.right-section {
width: 100%;
padding: 20px;
background-color: #e0e0e0;
overflow-y: auto;
}
/* Style for links */
.left-section a {
display: block;
margin-bottom: 10px;
text-decoration: none;
color: blue;
}
/* Hide the main page scrollbar */
body {
overflow: hidden;
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Web Page Welcome to My Web Page Welcome to My Web Page Welcome to My Web Page </h1>
<p>This is the banner section of the page.</p>
</header>
<!-- Banner Section -->
<!-- Container for both sections -->
<div class="container">
<!-- Left Section with Independent Scrollbar -->
<div class="left-section">
<h3>Links</h3>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
<a href="#">Link 5</a>
<a href="#">Link 6</a>
<a href="#">Link 7</a>
<a href="#">Link 8</a>
<a href="#">Link 9</a>
<a href="#">Link 10</a>
<a href="#">Link 11</a>
<a href="#">Link 12</a>
<a href="#">Link 13</a>
<a href="#">Link 14</a>
<a href="#">Link 15</a>
<a href="#">Link 16</a>
<a href="#">Link 17</a>
<a href="#">Link 18</a>
<a href="#">Link 19</a>
<a href="#">Link 20</a>
</div>
<!-- Right Section with Independent Scrollbar -->
<div class="right-section">
<!-- Content for the right section goes here -->
</div>
</div>
</body>
</html>
3
Answers
I found the answer. I had to subtract the height of the from the bottom divs.
height: calc(100vh - 80px);
html, body
need to be set to100dvh
(dynamic viewport height)body
also needs to bedisplay: flex;
set to directioncolumn
(and does not necessarily need to be set tooverflow: hidden
but does not hurts)padding
instead of margin for your anchors. One plus is that the tap area is largerflex
property shorthand to expand an element to the available space.container
also needs to haveoverflow
other than visibleYou have to set the .left-section height. like 76vh or using ‘px’.