Fixed header inside scrolling block
Asked Answered
M

3

1

I'm trying to create a block which may or may not have a scrollbar, with a header that does not scroll. The trick is that the width of the header should be affected by the presence of a scrollbar.

I'm worried that this is one of those CSS use cases which should be trivial, but might, in fact, be impossible. Anyone willing to prove me wrong?

Moneymaking answered 8/3, 2011 at 3:48 Comment(0)
M
1

I haven't figured out how to do this with CSS alone. So, here's a solution which uses JavaScript (here, jQuery), but only runs when he content changes. If the size of your wrapper depends on the size of the window, you may also need to run it on resize. Here's the heart of it:

$.fn.fitTo = function(target){
    var $el = $(this);
    $(target).bind('refit', function(){
        $el.width(this.clientWidth);
    });
}

Call $header.fitTo($content) to bind the header to a custom refit event on the element with the content. Now, whenever the content changes such that a scroll bar may have appeared or disappeared, do…

$content.trigger('refit');

…and the width of the header is reset to the clientWidth of the element containing content. The header must be outside the scrolling element.

Working example

Moneymaking answered 26/3, 2011 at 18:58 Comment(0)
H
1

Here are a few pointers

http://davidchambersdesign.com/css-fixed-position-headers/

and there involve tables with fixed header and scrolling body

  1. http://imar.spaanjaars.com/357/a-scrollable-table-with-a-fixed-header
  2. http://anaturb.net/csstips/sheader.htm
Hellbent answered 8/3, 2011 at 4:57 Comment(1)
Thanks, Clyde. However, you're missing the key point of the question: that the header is visually inside the containing box. Those links show tables where the header is separate from the scrollbar.Moneymaking
T
1

You cannot do this with CSS alone. We must use javaScript. With jQuery you can do the following

var cw = $('#container').innerWidth(),
    cs = $('#container').scrollTop();

    $('#header').css({
        'width': cw + "px"
    });

    $('#container').scroll(function() {
        $('#header').css({
            'top': $('#container').scrollTop(),
        })
    })

Check working example at http://jsfiddle.net/VswxL/2/

Turkoman answered 8/3, 2011 at 7:7 Comment(3)
This doesn't look too good in my browser (Chrome 10). The header flickers and bounces around as I scroll.Moneymaking
Thanks, Hussein. I just posted an answer which needs JavaScript only when the content or size of the scrolling element changes. Though it's not seamless (you have to let it know when to re-fit the header), I like that the display of the header the rest of the time is handled with CSS, so it won't glitch, and you don't have to execute JavaScript while scrolling.Moneymaking
Hey, Hussein. I'm sorry to keep you waiting, and I love giving out upvotes. This solution causes serious visual glitches in most of the browsers I tested, so I think that, as a guide, it points in the wrong direction.Moneymaking
M
1

I haven't figured out how to do this with CSS alone. So, here's a solution which uses JavaScript (here, jQuery), but only runs when he content changes. If the size of your wrapper depends on the size of the window, you may also need to run it on resize. Here's the heart of it:

$.fn.fitTo = function(target){
    var $el = $(this);
    $(target).bind('refit', function(){
        $el.width(this.clientWidth);
    });
}

Call $header.fitTo($content) to bind the header to a custom refit event on the element with the content. Now, whenever the content changes such that a scroll bar may have appeared or disappeared, do…

$content.trigger('refit');

…and the width of the header is reset to the clientWidth of the element containing content. The header must be outside the scrolling element.

Working example

Moneymaking answered 26/3, 2011 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.