Image Cover
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 - 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 <img> to do the same thing as background-size: cover.</h2>
<p>Why do this? Because the <img> 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'); }
};
Developer | Jon Christensen |
Username | JMChristensen |
Uploaded | August 03, 2022 |
Rating | 3 |
Size | 3,914 Kb |
Views | 42,504 |
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!
Name | Size |
Panel Navigation | 4,288 Kb |
CSS Transition End Sequence | 4,980 Kb |
SVG Circle Progress | 3,368 Kb |
Google Button Ripple | 3,524 Kb |
Simple CSS Loaders | 3,841 Kb |
Responsive SVG Overlay | 2,674 Kb |
Graphs and Snap.svg | 2,870 Kb |
Table Cell Slideshow | 2,892 Kb |
Section Diagonal Areas | 2,810 Kb |
CSS Banners | 2,548 Kb |
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!
Name | Username | Size |
Exploring css spinners | Akagr | 3,569 Kb |
Magnus 3 | ARocketfish | 7,944 Kb |
Delete Hover | Chungman93 | 2,557 Kb |
A Pen by Tosh | Panev | 2,586 Kb |
Spilled Paint | Darrylhuffman | 3,894 Kb |
SlideDown FixedMenu | Mp_graphic | 5,602 Kb |
Flower expansion | Sreucherand | 3,425 Kb |
Confirm Close Of Foundation Reveal Modal | Winghouchan | 1,818 Kb |
Perspective Origin Demo | Agelber | 3,614 Kb |
Sony Xperia Z3 Flat MockUp | Dapinitial | 4,379 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!