picture_tag
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
picture_tag(*sources, &block)
public
Returns an HTML picture tag for the sources. If sources is a string, a single picture tag will be returned. If sources is an array, a picture tag with nested source tags for each source will be returned. The sources can be full paths, files that exist in your public images directory, or Active Storage attachments. Since the picture tag requires an img tag, the last element you provide will be used for the img tag. For complete control over the picture tag, a block can be passed, which will populate the contents of the tag accordingly.
Options
When the last parameter is a hash you can add HTML attributes using that parameter. Apart from all the HTML supported options, the following are supported:
Examples
picture_tag("picture.webp") # => <picture><img src="/images/picture.webp" /></picture> picture_tag("gold.png", :image => { :size => "20" }) # => <picture><img height="20" src="/images/gold.png" width="20" /></picture> picture_tag("gold.png", :image => { :size => "45x70" }) # => <picture><img height="70" src="/images/gold.png" width="45" /></picture> picture_tag("picture.webp", "picture.png") # => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img src="/images/picture.png" /></picture> picture_tag("picture.webp", "picture.png", :image => { alt: "Image" }) # => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img alt="Image" src="/images/picture.png" /></picture> picture_tag(["picture.webp", "picture.png"], :image => { alt: "Image" }) # => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img alt="Image" src="/images/picture.png" /></picture> picture_tag(:class => "my-class") { tag(:source, :srcset => image_path("picture.webp")) + image_tag("picture.png", :alt => "Image") } # => <picture class="my-class"><source srcset="/images/picture.webp" /><img alt="Image" src="/images/picture.png" /></picture> picture_tag { tag(:source, :srcset => image_path("picture-small.webp"), :media => "(min-width: 600px)") + tag(:source, :srcset => image_path("picture-big.webp")) + image_tag("picture.png", :alt => "Image") } # => <picture><source srcset="/images/picture-small.webp" media="(min-width: 600px)" /><source srcset="/images/picture-big.webp" /><img alt="Image" src="/images/picture.png" /></picture>
Active Storage blobs (images that are uploaded by the users of your app):
picture_tag(user.profile_picture) # => <picture><img src="/rails/active_storage/blobs/.../profile_picture.webp" /></picture>