Skip to content

Instantly share code, notes, and snippets.

@elfassy
Created September 24, 2011 15:40
Show Gist options
  • Select an option

  • Save elfassy/1239460 to your computer and use it in GitHub Desktop.

Select an option

Save elfassy/1239460 to your computer and use it in GitHub Desktop.
better image_tag
#Orverwrite the image_tag based on the idea from Designer Wall (http://webdesignerwall.com/tutorials/css3-image-styles)
def image_tag(source, options = {})
options.symbolize_keys!
if size = options.delete(:size)
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
end
options[:class] = "image-wrap " + options[:class]
options[:style] = "position:relative; display:inline-block; background:url(#{super(source)}) no-repeat center center; width: #{options.delete(:width)}; height: #{options.delete(:height)};" + options[:style]
content_tag("span", super(source, {:style => "opacity: 0;"}) ,options)
end
#Example output
#<span class="image-wrap " style="position:relative; display:inline-block; #background:url(image.jpg) no-repeat center center; width: 150px; height: 150px;">
# <img src="image.jpg" style="opacity: 0;">
#</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment