Use full width excluding overflow scrollbar with “position: absolute”

前端 未结 2 1022
轮回少年
轮回少年 2021-01-18 06:03

I would like to have a small red div with full width at a fixed top position, inside another div that has overflow: scroll. I hope the jsFiddle makes it clear:

2条回答
  •  粉色の甜心
    2021-01-18 06:14

    Seems like this isn't possible with pure CSS, so here's a JavaScript (jQuery) hack:

    $(function() {
      var $container = $("
    ").css({ height: 1, overflow: "scroll" }).appendTo("body"); var $child = $("
    ").css({ height: 2 }).appendTo($container); window.SCROLLBAR_WIDTH = $container.width() - $child.width(); $container.remove(); });

    then:

    $("div.red-banner").css({
      right: SCROLLBAR_WIDTH
    });
    

提交回复
热议问题