Image Cover

Size
3,914 Kb
Views
42,504

How do I make an image cover?

Using the image tag to do the same thing as background-image in CSS. Possibly useful for the benefits of srcset and picture, especially in some situations.. What is a image cover? How do you make a image cover? This script and codes were developed by Jon Christensen on 03 August 2022, Wednesday.

Image Cover Previews

Image Cover - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Image Cover</title> <script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>Image Cover</h1>
<h2>Using &lt;img&gt; to do the same thing as background-size: cover.</h2>
<p>Why do this? Because the &lt;img&gt; tag gives us the ability to use picture and srcset. You could switch out images with CSS background-image, but that's not as friendly in some systems and can be a pain. A little CSS and JS magic is all it takes.</p>
<div class="container"> <div id="imageContainer" class="image-container"> <img src="https://unsplash.imgix.net/photo-1429371527702-1bfdc0eeea7d?fm=jpg&q=75&w=340"> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Image Cover - Script Codes CSS Codes

img { max-width: 100%; height: auto;
}
.container { margin: 0 auto; display: block; width: 100%;
}
.image-container { display: block; width: 75%; overflow: hidden; height: 400px; margin: 0 auto; position: relative; background-image: url(https://unsplash.imgix.net/photo-1429371527702-1bfdc0eeea7d?fm=jpg&q=75&w=340); background-size: cover; background-position: center;
}
.image-container img { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);
}
.image-container.portrait img { height: 100%; width: auto; max-width: none; max-height: 100%;
}
.image-container.landscape img { height: auto; width: 100%; max-width: 100%; max-height: none;
}
/* Transition mixin, cause lazy */
/* W00t border-box */
* { box-sizing: border-box;
}
*:after,
*:before { box-sizing: border-box;
}
/* Make things perty */
html { height: 100%;
}
body { font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; background: url(http://www.jmchristensendesign.com/wp-content/themes/jmcdsn/images/intro_default-background.jpg); color: #fff; height: 100%; padding-top: 2em; text-align: center;
}
h1,
h2 { margin: 0; text-transform: uppercase; text-shadow: 0 0 0.5em black;
}
h2 { font-weight: 300;
}
input { border: 1px solid #666; background: #333; color: #fff; padding: 0.5em; box-shadow: none; outline: none !important; margin: 1em auto; text-align: center;
}
a { color: orange; text-decoration: none; -webkit-transition: color 250ms ease-in-out; transition: color 250ms ease-in-out;
}
a:hover { color: yellow;
}
p { max-width: 40em; margin: 1em auto;
}

Image Cover - Script Codes JS Codes

$(window).resize( function() { checkImage(); });
$(window).load( function() { checkImage(); });
var checkImage = function(){ var	$container = $('#imageContainer'), $img = $container.find('img'), contWidth = $container.width(), contHeight = $container.height(), contAspectRatio = ( contWidth / contHeight ), imgWidth = $img.width(), imgHeight = $img.height(), imgAspectRatio = ( imgWidth / imgHeight );	$container.removeClass('landscape portrait');	if ( contAspectRatio > imgAspectRatio ){ // wide $container.addClass('landscape'); }	else { // tall $container.addClass('portrait'); }
};
Image Cover - Script Codes
Image Cover - Script Codes
Home Page Home
Developer Jon Christensen
Username JMChristensen
Uploaded August 03, 2022
Rating 3
Size 3,914 Kb
Views 42,504
Do you need developer help for Image Cover?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jon Christensen (JMChristensen) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!