{"id":2521,"date":"2017-08-02T20:31:19","date_gmt":"2017-08-02T20:31:19","guid":{"rendered":"http:\/\/cssreset.com\/?p=2521"},"modified":"2017-09-13T20:20:13","modified_gmt":"2017-09-13T20:20:13","slug":"zoom-images-inside-the-container-on-mouse-over-using-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/zoom-images-inside-the-container-on-mouse-over-using-css\/","title":{"rendered":"Zoom images inside the container on mouse over using CSS"},"content":{"rendered":"<p>Zooming in on an image is a functional and appreciated effect to have available. This functionality can be implemented in various ways, including through pure CSS, pure JavaScript or through jQuery. Out of these options, the pure CSS solution is the fastest as the other 2 ways require the execution of a set of code blocks, which involves DOM traversal and is an expensive process. Luckily for us, CSS3 provides a simple and efficient solution just by tweaking some CSS3 properties!<br \/>\nCSS3 has a transform property which allows you to translate, rotate, scale, and skew elements. This pure CSS solution will zoom an image inside the container on mouse over.<\/p>\n<h2>HTML Markup<\/h2>\n<p>First, let\u2019s take a look at the HTML. The HTML markup has an image wrapped inside a container and item div element. The container is the parent element and the item is the child element, which holds the image. The item div element is required to define an absolute position for the image inside the container. The images used in this post are taken from <a href=\"http:\/\/www.freeimages.com\/\">Free Images<\/a>.<\/p>\n<div class=\"container\">\n<div class=\"item\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/images.freeimages.com\/images\/previews\/69f\/f-ish-1379242.jpg\" \/><\/div>\n<\/div>\n<div class=\"container\">\n<div class=\"item\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/images.freeimages.com\/images\/previews\/473\/dogs-1-1482201.jpg\" \/><\/div>\n<\/div>\n<div class=\"container\">\n<div class=\"item\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/images.freeimages.com\/images\/previews\/0ce\/animal-1361355.jpg\" \/><\/div>\n<\/div>\n<h2><\/h2>\n<h2>CSS<\/h2>\n<p>As the image will be scaled to create a zoom effect inside the container, the .container CSS class will define the size of the image. In our case, the size is 480&#215;320 pixels. It is also important to define the overflow property as \u2018hidden\u2019 as it will clip the image to its original width and height.<\/p>\n<pre>.container {\r\n position: relative;\r\n width: 480px;\r\n height: 320px;\r\n overflow: hidden;\r\n }<\/pre>\n<p>The .item CSS class is used to position the image inside the parent container. It sets the top and left position of the image inside the container.<\/p>\n<pre>.item {\r\n position: absolute;\r\n top: 0;\r\n left: 0;\r\n }<\/pre>\n<p>Next, attach a hover event CSS class to the container, which uses the transform CSS3 property to scale the image to 120%. One of the values of the transform property is scale(). This function allows you to scale an element up or down, which in turn makes the element appear bigger or smaller. If the value provided is greater than one, the element is scaled up.<\/p>\n<pre>.container:hover .item img {\r\n -webkit-transform: scale(1.2);\r\n transform: scale(1.2);\r\n }<\/pre>\n<p>Finally, add a transition effect when scaling the image. CSS3 transitions allow you to change property values smoothly, over a given duration. The following CSS code will add a transition duration of 0.6s for zoom animation to complete.<\/p>\n<pre>.item img {\r\n -webkit-transition: 0.6s ease;\r\n transition: 0.6s ease;\r\n }<\/pre>\n<p>The complete CSS code is,<\/p>\n<pre>.container {\r\n position: relative;\r\n width: 480px;\r\n height: 320px;\r\n overflow: hidden;\r\n }\r\n .item {\r\n position: absolute;\r\n top: 0;\r\n left: 0;\r\n }\r\n .item img {\r\n -webkit-transition: 0.6s ease;\r\n transition: 0.6s ease;\r\n }\r\n .container:hover .item img {\r\n -webkit-transform: scale(1.2);\r\n transform: scale(1.2);\r\n }<\/pre>\n<p>You can check out this <a href=\"https:\/\/jsfiddle.net\/wf7twxo1\/embedded\/result,html,css\/\">demo<\/a> to see the code in action!<\/p>\n<h2>Conclusion<\/h2>\n<p>To sum it up, this tutorial examined the process of implementing a zoom effect on images, triggered by mouse over, using pure CSS. This solution zooms an image inside the container with transition effect. Remember, you can change the scaling value and transition duration as per your need. Have fun!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Zooming in on an image is a functional and appreciated effect to have available. This functionality can be implemented in various ways, including through pure CSS, pure JavaScript or through jQuery. Out of these options, the pure CSS solution is [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/zoom-images-inside-the-container-on-mouse-over-using-css\/\" title=\"Click to read 'Zoom images inside the container on mouse over using CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,7],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2521"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=2521"}],"version-history":[{"count":4,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2521\/revisions"}],"predecessor-version":[{"id":2565,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2521\/revisions\/2565"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}