3D image hover with pure CSS

Source: Internet
Author: User

Let's take a look at the 3D effect of a pure CSS image. Please go to my codepen to see the effect, as shown in.


Start status on the left and hover on the right. The implementation principle is shown in. Use div. img loads the upper half of the image ,. img: fefore pseudo class to implement the lower half of the image and present the image title. img: after pseudo class implements Image Shadow.


Okay. Let's look at the code. The first is the very simple HTML

  

The second is CSS. CSS code is critical. We need to complete the layout first.

/* Import font */@ import url ( http://fonts.googleapis.com/css?family=Montserrat );/* Get a gradient in the background */body {min-height: 600px; background: linear-gradient (hsl (40%,), # E0D7A3 );} /* to achieve the overall animation effect, we place a container outside the image ,*/. container {width: 300px; height: pixel PX; margin: 50px auto;/* realize the depth of 3D animation in a unified manner */perspective: 1000px ;}. img {width: 100%; height: 100%; background: url (" http://www.cmen.cc/uploadfile/2013/1212/20131212044827530.jpg "); Background-size: 100%; position: relative ;}. img: before {content: attr (data-text); width: 100%; height: 40px; position: absolute; top: 100%; left: 0; background: url (" http://www.cmen.cc/uploadfile/2013/1212/20131212044827530.jpg ") Left bottom; background-size: 100%;/* set the font style of the image title */font-size: 1.6vw; font-family: Montserrat, Arial, sans-serif; color: # fff; text-shadow: 1px 1px 3px #000; text-align: center; line-height: 40px;/* fold down the lower half of the image */transform-style: preserve-3d; transform-origin: center top; transition: all 1 s transform; transform: rotateX (-80deg );}. img: after {content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; box-shadow: 0 0 100px 50px rgba (0, 0, 0 ,. 2), inset 0 0 250px 250px rgba (0, 0, 0 ,. 2);/* Animation Parameters for the shadow Section */perspective: 1000px; transform-style: preserve-3d; transform-origin: center bottom; transform: rotateX (90deg) translateZ (-80px) scale (0.75);}/* Fold the image after hovering */. container: hover. img {transform: rotateX (75deg) translateZ (50px);}/* shadow animation After hovering */. container: hover. img: after {box-shadow: 0 0 25px 25px rgba (0, 0, 0, 0.5), inset 0 0 250px 250px rgba (0, 0, 0, 0.5 ); transform: rotateX (-5deg) translateZ (-80px) scale (1 );}
I believe that you can understand the code writing process with comments. You can go to codepen to edit or download the results online.

---------------------------------------------------------------

Front-end development of whqet, focus on web Front-end development technology, and share webpage-related resources.
---------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.