Shake buttons on hover using HTML and CSS
In this tutorial, we will learn to use HTML and CSS to shake a button on user hover. Creating a shake button can make your app's user experience more engaging.
We need to create a custom animation using CSS "keyframe" rules to shake any HTML element. We can then use custom keyframes as the value of the "animation" CSS property to shake the button when the user hovers over it.
grammar
Users can shake the hover button using HTML and CSS following the following syntax.
.btn:hover { animation: key_frame_name animation_time repetition; } @keyframes key_frame_name { 0% { transform: rotate(0deg); } 100% { transform: rotate(10deg); } }
In the above syntax, we created a custom CSS rule to add a shaking animation to the button. Users can replace "animation_time" with a time unit and "repetition" with a number to repeat the animation.
The Chinese translation ofExample
is:Example
In the example below, we shake the button vertically. We created a normal HTML button using the "button" tag and gave it a "btn" class name. We use the class name to access the button and style it.
In CSS, we use the "animation" property to add a "shake" keyframe to the button when the user hovers over it. In the Shake keyframe, we rotate the button "0 degrees" at 0% of the animation time, "5 degrees" at 20% of the time, "0 degrees" at 50% of the time, and "0 degrees" at 50% of the animation time. Time rotation button "5 degrees" 70% of the time is "0 degrees" and 100% of the time is "0 degrees".
In the output, the user can observe the button shaking in the vertical direction.
<html> <style> .btn { justify-content: center; align-items: center; height: fit-content; padding: 10px 20px; border: 1px solid #000; border-radius: 5px; background-color: red; color: white; font-size: 40px; } .btn:hover {animation: shaking 0.5s infinite;} @keyframes shaking { 0% {transform: rotate(0deg);} 20% {transform: rotate(-4deg);} 50% {transform: rotate(0deg);} 70% {transform: rotate(4deg);} 100% {transform: rotate(0deg);} } </style> <body> <h2 id="Shaking-the-button-vertically-using-HTML-and-CSS"> Shaking the button vertically using HTML and CSS </h2> <p> Please hover the cursor over the button below to see the shaking effect.</p> <div> <button class = "btn"> Submit </button> </div> </body> </html>
Example
is:Example
In the example below, we use HTML and CSS to shake the button horizontally.
We used the CSS property 'transform: translateX()' to shake the button horizontally. First, we move the button in the negative direction. Next, we move the button to its original position. Then, we move the button in the positive direction, and finally, we use CSS's 'keyframes' rule to move the button to its original position.<html> <style> .btn { justify-content: center; align-items: center; height: fit-content; padding: 10px 20px; border: 1px solid #000; border-radius: 5px; background-color: black; color: white; font-size: 40px; } .btn:hover {animation: shaking 0.4s infinite;} @keyframes shaking { 0% {transform: translateX(-10px);} 20% {transform: translateX(-5px);} 50% {transform: translateX(-5px);} 70% {transform: translateX(-5px);} 80% {transform: translateX(10px);} 90% {transform: translateX(-10px);} } </style> <body> <h2 id="Shaking-the-button-Horizontally-using-HTML-and-CSS"> Shaking the button Horizontally using HTML and CSS </h2> <p> Please hover the cursor over the button below to see the shaking effect.</p> <div> <button class = "btn"> Hover the Button </button> </div> </body> </html>
Example
is:Example
In the example below, we will learn how to shake the button horizontally and vertically. We use ‘translateX()’ together with ‘rotate()’ as the value of the ‘transform’ CSS property.
'translateX()' moves the button horizontally, and the 'rotate()' function moves the button vertically. In the output, the user can observe that when they hover over the button, it moves slightly horizontally and vertically. However, the user can increase the parameter value of the ‘translateX()’ function to dither more in the horizontal direction.
<html> <style> .btn { justify-content: center; align-items: center; height: fit-content; padding: 10px 20px; border: 1px solid #000; border-radius: 5px; background-color: green; color: white; font-size: 25px; } .btn:hover {animation: shaking 0.4s infinite;} @keyframes shaking { 0% {transform: translateX(0) rotate(0deg);} 25% {transform: translateX(15px) rotate(5deg);} 50% {transform: translateX(0px) rotate(0deg);} 75% {transform: translateX(-15px) rotate(-5deg);} 100% {transform: translateX(0px) rotate(0deg);} } </style> <body> <h3 id="Shaking-the-button-Horizontally-and-vartically-using-HTML-and-CSS"> Shaking the button Horizontally and vartically using HTML and CSS</h3> <div> <button class = "btn"> Point out the Button </button> </div> </body> </html>
Conclusion
In this tutorial, the user learned to jitter an HTML button using only CSS. In the first example, we learned to shake a button vertically. In the second example, we learned to shake the button horizontally; in the last example, we learned to shake the button horizontally and vertically.
The above is the detailed content of Shake buttons on hover using HTML and CSS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are two common ways to hide rows in XML: Use the display property in CSS to set to none Use XSLT to skip hidden rows via conditional copying

XML is widely used to build and manage user interfaces. It defines and displays the interface content through the following steps: Define interface elements: XML uses tags to define interface elements and their properties. Building a hierarchy: XML organizes interface elements according to hierarchical relationships to form a tree structure. Using Stylesheets: Developers use stylesheet languages such as CSS or XSL to specify the visual appearance and behavior of elements. Rendering process: A browser or application uses an XML parser and stylesheet to parse an XML file and render interface elements to make it visible on the screen.

How to achieve the playback of pictures like videos? Many times, we need to implement similar video player functions, but the playback content is a sequence of images. direct...

How to achieve upward scrolling loading similar to WeChat chat records? When developing applications similar to WeChat chat records, a common question is how to...

How to realize the function of playing pictures like videos? Many times, we need to achieve similar video playback effects in the application, but the playback content is not...

Using react-transition-group in React to achieve confusion about closely following transition animations. In React projects, many developers will choose to use react-transition-group library to...

Regarding the problem of inconsistent width of emsp spaces in HTML and Chinese characters in many web tutorials, it is mentioned that occupying the width of a Chinese character, but the actual situation is not...

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics
