Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active August 29, 2015 14:05
Show Gist options
  • Save swapnilshrikhande/eecd9eb1a6ae469762b3 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/eecd9eb1a6ae469762b3 to your computer and use it in GitHub Desktop.
Fix Alt text not showing when image width too low to fit
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
.alt-div{
text-align : center;
vertical-align:middle;
min-height : 200px;
min-width : 200px;
}
</style>
<!-- PUT WHERE IS APPEARS CURRENTLY -->
ADD class my-img TO IMAGE TAG
<div class="alt-div" style="display:none;"/>
<script>
$(document).ready(function(event){
updateImageAlt();
});
var updateImageAlt = function(event){
$("img").each(function(index,image){
if(!$.trim($(image).attr("src"))){
var altDiv = $(image).closest("a").find('.alt-div');
$(image).hide();
altDiv.height($(image).height());
altDiv.height($(image).width());
altDiv.text($(image).attr("alt"));
altDiv.show();
}
})
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment