LoginSignup
4
2

More than 3 years have passed since last update.

ページ内リンクが固定ヘッダーに被るのを防ぐ

Posted at

概要

fixedやstickyなどで作った固定ヘッダーがある場合、ページ内リンクへ飛んだ際にコンテンツと被ってしまいます。
これを解決する方法を紹介します。

解決方法

cssの scroll-margin-top を使います。
scroll-margin-top を使うとスクロールにmarginを追加する、みたいなことができるようになります。

イメージとしてはこんな感じになります。
これでページ内リンクへ飛んだ際に100px分多めにスクロールするので、ヘッダーと被らずコンテンツが表示されます。

header {
  position: sticky;
  top: 0;
  width: 100%;
  height: 100px;
}

.anchor-item {
  scroll-margin-top: 100px;
}

対応ブラウザ

scroll-margin-top はIEには対応していません。
IEに対応するなら別の方法をとる必要があります。

参考リンク

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2