muwonge nicholus
muwonge nicholus

Reputation: 144

How to have a fixed sidebar that is scrollable vertically and fixed horizontally

My sidebar has content that may need to scroll downwards to read. I have tried to search online and I have failed to find a resource to get me started. Your help will be much appreciated.

Update

I am sorry for not asking properly. I meant to add that the sidebar should move at the same velocity as the main page scroll as they scroll vertically.

Upvotes: 1

Views: 1096

Answers (2)

Marios Nikolaou
Marios Nikolaou

Reputation: 1336

I have created a sample to help you.

.content-section {
  min-height: 2000px;
}
.sidebar-section {
  position: absolute;
  height: 100%;
  width: 100%;
}


.sidebar-item {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	
	/* Position the items */
	// &:nth-child(2) { top: 25%; }
	// &:nth-child(3) { top: 50%; }
	// &:nth-child(4) { top: 75%; }
}


.make-me-sticky {
  position: -webkit-sticky;
	position: sticky;
	top: 0;
  max-height:400px;
  overflow-y:scroll;
  
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>

<html>
<head>
 <title>Test</title>
</head>
<body>

  <div class="container-fluid">

 <div class="row">
             <div class="col-5">

              <div class="sidebar-item">
                <div class="make-me-sticky">
                        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
                </div>
              </div>
         
            </div>
            <div class="col-7">

                <div class="content-section">
                    <h2>Content Section</h2>
                </div>
            </div>

</div>
</div>
</body>

</html>

Upvotes: 1

Mike G
Mike G

Reputation: 64

Try looking into CSS position: sticky with top: 10px, where 10px is the distance of the of sticky container from the top of the screen.

Be aware it does have some backward compatibility issues with IE11, if that is a problem for you.

https://www.w3schools.com/howto/howto_css_sticky_element.asp#:~:targetText=An%20element%20with%20position%3A%20sticky,(like%20position%3Afixed).

Upvotes: 0

Related Questions