Header Div Stays at Top, Vertical Scrolling Div Below with Scrollbar Only Attached to That Div

Header div stays at top, vertical scrolling div below with scrollbar only attached to that div

HTML:

​<div class="header">This is the header</div>
<div class="content">This is the content</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

CSS:

​.header
{
height:50px;
}
.content
{
position:absolute;
top: 50px;
left:0px;
right:0px;
bottom:0px;
overflow-y:scroll;
}​

How can I keep a grid header row aligned with scrolling rows below?

It's not a perfect solution but how about something like this?

I set the heading column as sticky-top, and gave it a background colour.
Then set the container to overflow-auto.

.max-height {
max-height: 100px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-md border m-3 overflow-auto ">
<div class="row sticky-top bg-white">
<div class="col">
Some Heading
</div>
<div class="col">
Some Heading
</div>
<div class="col">
Some Heading
</div>
</div>
<div class="row max-height">
<div class="col">
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
<div class="row">
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
<div class="col">
Some Content
</div>
</div>
</div>
</div>
</div>

Making a div vertically scrollable using CSS

You have it covered aside from using the wrong property. The scrollbar can be triggered with any property overflow, overflow-x, or overflow-y and each can be set to any of visible, hidden, scroll, auto, or inherit. You are currently looking at these two:

  • auto - This value will look at the width and height of the box. If they are defined, it won't let the box expand past those boundaries. Instead (if the content exceeds those boundaries), it will create a scrollbar for either boundary (or both) that exceeds its length.

  • scroll - This values forces a scrollbar, no matter what, even if the content does not exceed the boundary set. If the content doesn't need to be scrolled, the bar will appear as "disabled" or non-interactive.

If you always want the vertical scrollbar to appear:

You should use overflow-y: scroll. This forces a scrollbar to appear for the vertical axis whether or not it is needed. If you can't actually scroll the context, it will appear as a"disabled" scrollbar.

If you only want a scrollbar to appear if you can scroll the box:

Just use overflow: auto. Since your content by default just breaks to the next line when it cannot fit on the current line, a horizontal scrollbar won't be created (unless it's on an element that has word-wrapping disabled). For the vertical bar,it will allow the content to expand up to the height you have specified. If it exceeds that height, it will show a vertical scrollbar to view the rest of the content, but will not show a scrollbar if it does not exceed the height.

Header extends over scrollbar when width=100%

::-webkit-scrollbar {
width: 40px;
}

Given width of scrollbar is 40px & scrollbar present at right side of page , so add right:40px in:

.sticky-heading {
right: 40px; //or 30px whatever you want but more than scrollbar width
}

How can I make a div stick to the top of the screen once it's been scrolled to?

You could use simply css, positioning your element as fixed:

.fixedElement {
background-color: #c0c0c0;
position:fixed;
top:0;
width:100%;
z-index:100;
}

Edit: You should have the element with position absolute, once the scroll offset has reached the element, it should be changed to fixed, and the top position should be set to zero.

You can detect the top scroll offset of the document with the scrollTop function:

$(window).scroll(function(e){ 
var $el = $('.fixedElement');
var isPositionFixed = ($el.css('position') == 'fixed');
if ($(this).scrollTop() > 200 && !isPositionFixed){
$el.css({'position': 'fixed', 'top': '0px'});
}
if ($(this).scrollTop() < 200 && isPositionFixed){
$el.css({'position': 'static', 'top': '0px'});
}
});

When the scroll offset reached 200, the element will stick to the top of the browser window, because is placed as fixed.

CSS and DIV with header and body with scrollable content, and fixed sidebar to right

I created a simple fiddle, that doesn't use calc (support isn't great - http://caniuse.com/calc, and then there's the big unknown of any performance penalty you may/may not hit using it..)

It's very straight forward, using simple CSS.

http://jsfiddle.net/ruYGH/3/



Related Topics



Leave a reply



Submit