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?

More than 3 years have passed since last update.

HTML5/埋め込み/source要素/端末ディスプレイサイズで画像や動画を切替えるHTML&CSS

Last updated at Posted at 2022-03-03

動画をレスポンシブで切り替える

スマホには動画Aを表示
PCには動画Bを表示

html
<div id="video-rpsv"> 
  <video id="video-pc" playsinline autoplay loop muted preload="auto" poster="[代替画像URL]" >
    <source type="video/mp4" src="[動画URL A]" data-src="video_pc.mp4">
  </video>
</div>
<div id="video-rpsv"> 
  <video id="video-sp" playsinline autoplay loop muted preload="auto" poster="[代替画像URL]" >
    <source type="video/mp4" src="[動画URL B]" data-src="video_sp.mp4">
  </video>
</div>
css
#video-rpsv {text-align: center;}

#video-pc {width: 100%;}
@media only screen and (max-width: [表示上限]px) {
  #video-pc {display:none;}
  #video-sp {width: 100%;}
}

画像をレスポンシブで切り替える

スマホには縦画像を表示
PCには横画像を表示

<div id="img"> 
<picture>

<!-- 縦画像 -->
<source media="(max-width: [表示上限]px)" srcset="[画像URL:A]" alt="">
<!-- 横画像 -->
<img src="[画像URL:B]" alt="">
</picture>
</div>
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?