Created
September 12, 2013 18:14
-
-
Save EtienneLem/6541666 to your computer and use it in GitHub Desktop.
Responsive images container (Bonus: The content flow stays the same whether the image is loaded or not (or broken))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def image_container(path, width, height) | |
%(<div class="image-container" style="max-width: #{width}px"> | |
<div style="padding-top: #{(height.to_f / width.to_f) * 100}%"> | |
#{image_tag(path)} | |
</div> | |
</div>).html_safe | |
end | |
# Note: If you want the image to scale up, remove the `max-width` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= image_container('foo.png', 500, 400) %> | |
<!-- | |
<div class="image-container" style="max-width: 500px"> | |
<div style="padding-top: 80%"> | |
<img src="/assets/foo.png" alt="Foo"> | |
</div> | |
</div> | |
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Inline image? Sure */ | |
/* .image-container { display: inline-block; width: 100% } */ | |
/* Centered? Done */ | |
/* .image-container { margin: auto } */ | |
.image-container > div { position: relative } | |
.image-container img { | |
position: absolute; | |
top: 0; left: 0; | |
width: 100%; height: 100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment