1
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?

画面幅によってファーストビュー画像を切り替える

Posted at

画面幅によってファーストビュー画像を切り替える方法をメモに残しておきます。

<div class="p-fv">
<picture>
<source media="(min-width: 768px)" srcset="./assets/img/fv.png" />
<img src="./assets/img/sp/fv.png" alt="かっこいいサイトだよ" />
</picture>
</div>

ポイント

min-width: 768pxはスマホ幅とPC幅のブレークポイントです。

pictureタグとsourceタグを使用することで、画面幅が768px以上になると、srcsetで設定した画像に切り替わります。

imgタグで指定した画像は、画面幅が条件に合わない場合や、古いブラウザでpictureタグがサポートされていない場合にデフォルトで表示される画像です。

この方法を使えば、画面幅やデバイスに応じて最適な画像を表示できます。

1
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
1
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?