2
1

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.

railsでpictureタグによるwebp出し分けをする

Posted at

概要

webpのを使いたいけど対応していないブラウザの場合にpngを出し分けるようにしたい

実装内容

<picture>
  <source srcset="<%= image_path("slide_01.webp") %>" type="image/webp">
  <%= image_tag('slide_01_400.png') %>
</picture>

pictureタグはsourceで画像を指定していって条件の合うものをだす。ない場合は最後に用意している画像を出す。

pictureタグについて
https://developer.mozilla.org/ja/docs/Web/HTML/Element/picture

pathの指定

imageタグでsourceを出す方法もあるが今回はsourceをそのまま書いている。この場合pathはそのまま書くのではなくimage_pathで生成しないと後ろのハッシュが生成されないのでうまく表示されない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?