{"id":9809,"date":"2024-01-12T18:09:00","date_gmt":"2024-01-12T18:09:00","guid":{"rendered":"https:\/\/codehim.com\/?p=9809"},"modified":"2024-01-22T16:12:37","modified_gmt":"2024-01-22T11:12:37","slug":"input-error-shake-animation-in-css","status":"publish","type":"post","link":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/","title":{"rendered":"Input Error Shake Animation in CSS"},"content":{"rendered":"<p>This CSS code snippet helps you to create an error shake animation for input elements. When an error occurs in the input field, it triggers a shake animation to visually highlight the issue. Basically, the code provides classes to trigger shaking animation that you can add\/remove to enable\/disable animation during <a href=\"https:\/\/codehim.com\/text-input\/form-validation-in-javascript-code\/\" target=\"_blank\" rel=\"noopener\">form validation<\/a>.<\/p>\n<p>This animation draws attention to the problematic input, making it easier for users to identify and correct errors during form submission.<\/p>\n<h2>How to Create Input Error Shake Animation in CSS<\/h2>\n<p>1. Inside the <code>&lt;div&gt;<\/code> with the class &#8220;form,&#8221; create a label and an input field with an associated ID. Add a button for demonstration purposes, which triggers the error animation.<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;div class=\"form\"&gt;\r\n  &lt;label for=\"name\"&gt;What is your quest?&lt;\/label&gt;&lt;br&gt;\r\n  &lt;input id=\"name\" type=\"text\" onChange=\"removeError()\" placeholder=\"Nothing is right\" \/&gt;\r\n  \r\n  &lt;div&gt;\r\n    &lt;button onclick=\"addError();\"&gt;Try anyway&lt;\/button&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>2. Use the following CSS code to style your form. Ensure that the input field has a transition property for smooth animation. The keyframes &#8216;shake&#8217; in the CSS create the animated effect when the &#8216;error&#8217; class is added to the input.<\/p>\n<pre class=\"prettyprint linenums lang-css\">* {\r\n  box-sizing: border-box;\r\n}\r\n\r\nhtml {\r\n  font-family: Helvetica, Arial, sans-serif;\r\n  font-size: 28px;\r\n}\r\n\r\nheader {\r\n  font-size: 14px;\r\n  border-bottom: 1px solid #ccc;\r\n  margin-bottom: 2rem;\r\n}\r\nheader h1 {\r\n  margin-bottom: 0;\r\n}\r\nheader p {\r\n  margin-top: 0;\r\n}\r\n\r\n.form {\r\n  width: 50vw;\r\n  margin: 1rem auto;\r\n}\r\n.form label {\r\n  font-size: 1rem;\r\n  vertical-align: middle;\r\n}\r\n.form input {\r\n  width: 100%;\r\n  font-size: 1rem;\r\n  padding: 0.25rem;\r\n  vertical-align: middle;\r\n  box-shadow: 0 0 0 transparent;\r\n  border: 1px solid #999;\r\n  outline: 0 none;\r\n  transition: box-shadow 0.5s;\r\n}\r\n.form input.error {\r\n  -webkit-animation: shake 0.2s ease-in-out 0s 2;\r\n          animation: shake 0.2s ease-in-out 0s 2;\r\n  box-shadow: 0 0 0.5em red;\r\n}\r\n\r\n@-webkit-keyframes shake {\r\n  0% {\r\n    margin-left: 0rem;\r\n  }\r\n  25% {\r\n    margin-left: 0.5rem;\r\n  }\r\n  75% {\r\n    margin-left: -0.5rem;\r\n  }\r\n  100% {\r\n    margin-left: 0rem;\r\n  }\r\n}\r\n\r\n@keyframes shake {\r\n  0% {\r\n    margin-left: 0rem;\r\n  }\r\n  25% {\r\n    margin-left: 0.5rem;\r\n  }\r\n  75% {\r\n    margin-left: -0.5rem;\r\n  }\r\n  100% {\r\n    margin-left: 0rem;\r\n  }\r\n}<\/pre>\n<p>3. Finally, incorporate the JavaScript code to manipulate the input element. The &#8216;addError&#8217; and &#8216;removeError&#8217; functions toggle the &#8216;error&#8217; class, initiating and halting the shake animation.<\/p>\n<pre class=\"prettyprint linenums lang-js\">var element = document.getElementById('name');\r\nvar addError = function() { element.classList.add('error'); };\r\nvar removeError = function() { element.classList.remove('error'); };<\/pre>\n<p>That&#8217;s all! hopefully, you have successfully created a shake animation for inputs on your website. If you have any questions or suggestions, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This CSS code snippet helps you to create an error shake animation for input elements. When an error occurs in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":9820,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[97],"tags":[],"class_list":["post-9809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-text-input"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Input Error Shake Animation in CSS &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Input Error Shake Animation in CSS &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-12T18:09:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T11:12:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"Input Error Shake Animation in CSS\",\"datePublished\":\"2024-01-12T18:09:00+00:00\",\"dateModified\":\"2024-01-22T11:12:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\"},\"wordCount\":211,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png\",\"articleSection\":[\"Text &amp; Input\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\",\"url\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\",\"name\":\"Input Error Shake Animation in CSS &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png\",\"datePublished\":\"2024-01-12T18:09:00+00:00\",\"dateModified\":\"2024-01-22T11:12:37+00:00\",\"description\":\"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png\",\"width\":1280,\"height\":960,\"caption\":\"Input Error Shake Animation In CSS\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Text &amp; Input\",\"item\":\"https:\/\/codehim.com\/category\/text-input\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Input Error Shake Animation in CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Input Error Shake Animation in CSS &#8212; CodeHim","description":"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/","og_locale":"en_US","og_type":"article","og_title":"Input Error Shake Animation in CSS &#8212; CodeHim","og_description":"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.","og_url":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-01-12T18:09:00+00:00","article_modified_time":"2024-01-22T11:12:37+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"Input Error Shake Animation in CSS","datePublished":"2024-01-12T18:09:00+00:00","dateModified":"2024-01-22T11:12:37+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/"},"wordCount":211,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png","articleSection":["Text &amp; Input"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/","url":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/","name":"Input Error Shake Animation in CSS &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png","datePublished":"2024-01-12T18:09:00+00:00","dateModified":"2024-01-22T11:12:37+00:00","description":"Here is a free code snippet to create a Input Error Shake Animation in CSS. You can view demo and download the source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Input-Error-Shake-Animation-In-CSS.png","width":1280,"height":960,"caption":"Input Error Shake Animation In CSS"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/text-input\/input-error-shake-animation-in-css\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Text &amp; Input","item":"https:\/\/codehim.com\/category\/text-input\/"},{"@type":"ListItem","position":3,"name":"Input Error Shake Animation in CSS"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":2057,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=9809"}],"version-history":[{"count":0,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9809\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/9820"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=9809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=9809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=9809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}