≪ Today I learned.
RSS購読
    公開日
    タグ
    CSS
    著者
    ダーシノ

    CSSだけでスクロールバーの幅を取得する

    100vw - 100cqwでスクロールバーの幅がわかる。

    <length>の意味

    スクロールバーのwidthを取得する

    html {
      container-type: size;
    }
    
    body {
      --scrollbar: calc(100vw - 100cqw);
    }
    <======= 100vw ========>
    +--------------------+-+
    |                    | |
    |<===== 100cqw =====>| |
    |                    | |
    +--------------------+-+
                          ^
                      scrollbar

    デモ

    @property --scrollbar {
      syntax: "<length>";
      inherits: false;
      initial-value: 0px; 
    }
    html {
      container-type: size;
    }
    body {
      --scrollbar: calc(100vw - 100cqw);
    }
    
    body:before {
      content: counter(val) "px";
      counter-reset: val tan(atan2(var(--scrollbar),1px));
    }

    参考サイト

    ブラウザ対応状況