Create modern CSS3 hover effects
Create a modern CSS3 hover effect by using CSS transitions, transforms and delays
A CSS transition gives us the ability to animate changes to a CSS property value. This can be used to smoothly change a value, and by using transition delays we can cue the transition of elements. A CSS transform allows us to transform elements in two or three dimensional space. In this tutorial, we will be using 2D transformations.
Step 1 – creating the base HTML
The first step is to create a simple HTML base.
- <div class="block_holder">
- <div class="block">
- <img src="250.jpg" />
- <div class="hover_info">
- </div>
- </div>
- </div>
With this we have built a holder for our block. The block contains our image and a hover_info div which, as the name suggests, will contain the information we want to display on hover.
Step 2 – creating the base CSS
The next step is to create the CSS to style the base and create the first transition. For ease of readability, this tutorial will be written without prefixes.
- .block_holder {
- padding: 7px;
- background: #f6f6f6;
- width: 350px;
- height: 250px;
- box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.15);
- float: left;
- margin: 15px;
- }
- .block {
- width: 350px;
- height: 250px;
- position: relative;
- overflow: hidden;
- }
- .hover_info {
- width: 350px;
- height: 250px;
- position: absolute;
- top: 0;
- left: 0;
- background: rgba(0, 0, 0, 0.8);
- width: 100%;
- height: 100%;
- position: absolute;
- left: -86%;
- top: -19%;
- transition: all 200ms linear;
- transform: rotate(90deg);
- }
After adding this CSS we now have a block holding an image and a div called hover_info. In this CSS we have added some basic styling to our block_holder and block. We added a background with an opacity of 80% to the hover_info div, used absolute positioning to place the div away from the block, and used overflow hidden on the block to ensure it doesn’t appear.
We have then used the CSS transition property, which allows us to specify the elements to which we would like to apply a transition. For this element we have used all, so the transition is applied to all elements where a value is changed. We have set a duration of 200ms - this is how long all changed elements will take to transition. Finally, we have used linear as our timing function, which means our transition will have a constant speed from start to end. We then use transform to rotate the div 90 degrees.
The final bit of code for this step is to set our CSS to be used on hover of the block.
Get the Creative Bloq Newsletter
Daily design news, reviews, how-tos and more, as picked by the editors.
- .block:hover .hover_info {
- top: 0;
- left: 0;
- transform: rotate(0deg);
- }
In this CSS we are changing the top, left and transform properties of the hover_info div on hover of the block. This will animate the div to being overlaid on top of the image. On hover, the overlaid div should rotate into place, as seen in this demo.
Step 3 – adding the final HTML
Now that we have a base with an initial hover transition we will add content to the hover_info div, in the form of a title, paragraph and link.
- <div class="block_holder">
- <div class="block">
- <img src="250.jpg" />
- <div class="hover_info">
- <h1>Article Title</h1>
- <p>Lorem ipsum dolor. Aenean scelerisque odio ut dui feugiat commodo. Nulla blandit erat vel nisi consectetur ac pharetra augue consectetur.</p>
- <a href="#">Read more</a>
- </div>
- </div>
- </div>
Step 4 – adding the final CSS
Now that we have the HTML, we will add styling to all of our elements.
- .hover_info h1 {
- font-size: 2em;
- padding: 10px 20px;
- background: rgba(207, 138, 5, 0.7);
- color: #E7E7E7;
- position: absolute;
- top: 25px;
- left: -100%;
- transition: left 200ms linear;
- }
- .hover_info:hover h1 {
- left: 0;
- transition-delay: 200ms;
- }
We have positioned the H1 25px from the top and initially 100% left to ensure that it's hidden at the start. On hover we change the left position to 0 so the H1 slides in from the left. We use the transition-delay property on hover with a setting of 200ms so it happens after the initial transition of the overlay.
We have applied this on the hover class and not the initial class so it only applies when a user initially hovers over the area. When the mouse is moved off the area the delay isn’t applied and the transition happens instantly.
- .hover_info p {
- font-size: 1.5em;
- line-height: 1.4;
- color: #fff;
- position: absolute;
- top: 85px;
- left: 10%;
- width: 80%;
- text-align: center;
- opacity: 0;
- transition: opacity 200ms linear;
- }
- .hover_info:hover p {
- opacity: 1;
- transition-delay: 600ms;
- }
We have positioned the paragraph 85px from the top and 10% from the left with a width of 80%. This will give the impression that the paragraph is in the centre of the block when it spans over 3 lines. We have set the opacity to 0 so it doesn’t appear initially, and set a transition on the opacity with a speed of 200ms using the linear timing function.
On hover we then set the opacity to 1, which makes the text fade in. We set a delay of 600ms, which will mean that the transition will happen after the initial rotation and the slide-in of the heading.
- .hover_info a {
- font-size: 1.5em;
- padding: 10px 20px;
- background: rgba(207, 138, 5, 0.7);
- color: #E7E7E7;
- position: absolute;
- bottom: 25px;
- right: -100%;
- text-decoration: none;
- }
- .hover_info:hover a {
- right: 0;
- transition: right 200ms linear 700ms, color, background 200ms linear;
- }
- .hover_info a:hover {
- background: rgba(207, 138, 5, 1.0);
- color: #F7F7F7;
- }
We have styled this tag the same as the heading tag, except this time it is in the bottom right, and, again, initially it is positioned off to the right. On hover we then transition from the right with a speed of 200ms using the linear timing function, this time with a delay of 700ms. This delay means that the transition will begin halfway through the paragraph’s transition. We then have a final pair of transitions for the colour and background with the same speed and timing function but no delay. This is for when the user hovers on the tag - it will change background and text colour.
In this tutorial we have used a variety of transitions, transform and transition-delays to create a chain of animations that reveal a title paragraph and link overlaid on an image.
Follow the link at the top of this article to download the files and view a full working demo.
Update: Thomas has created a collection of 25 different hover effects. You can see them and download the code on his website.
Thomas Hardy is a web developer based in Newcastle upon Tyne. He currently works at Union Room.
Thank you for reading 5 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1
The Creative Bloq team is made up of a group of design fans, and has changed and evolved since Creative Bloq began back in 2012. The current website team consists of eight full-time members of staff: Editor Georgia Coggan, Deputy Editor Rosie Hilder, Ecommerce Editor Beren Neale, Senior News Editor Daniel Piper, Editor, Digital Art and 3D Ian Dean, Tech Reviews Editor Erlingur Einarsson and Ecommerce Writer Beth Nicholls and Staff Writer Natalie Fear, as well as a roster of freelancers from around the world. The 3D World and ImagineFX magazine teams also pitch in, ensuring that content from 3D World and ImagineFX is represented on Creative Bloq.