0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

css 要素がヘッダーの後ろに隠れてしまう(position: fixed、position: sticky)

Posted at

問題

メッセージを入力すると、入力したメッセージが、ヘッダーの後ろに隠れてしまっていた。

メッセージが隠れている。
image.png

position: fixedの場合
/* position: fixed
要素はビューポート(画面の表示領域)を基準に配置される
headerはbodyのレイアウトに影響を与えず、bodyの高さには含まれなくなる  */

header {
	// 他省略
	position: fixed;
}

原因

position: fixed;を使用しているため、ヘッダーが通常のレイアウトフローから外れ、bodyの高さに含まれなくなっている。
その結果、chat-containerのmargin-topが正しく適用されず、メッセージがヘッダーの後ろに隠れてしまう。

解決方法

position: sticky;を使う

/* position: sticky;
要素がスクロールに応じて動き、特定の位置で固定される
親要素の範囲内でのみ固定される 
*/

header {
	// 他省略
	position: sticky;
}

メッセージを表示できた。
image.png

終わりに

devツールを使いながら細かい部分まで見ることになり、勉強になりました!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?