{"id":515,"date":"2020-08-09T03:30:59","date_gmt":"2020-08-09T03:30:59","guid":{"rendered":"https:\/\/www.cssportal.com\/blog\/?p=515"},"modified":"2024-02-28T23:25:46","modified_gmt":"2024-02-28T23:25:46","slug":"css-transitions","status":"publish","type":"post","link":"https:\/\/www.cssportal.com\/blog\/css-transitions\/","title":{"rendered":"CSS Transitions"},"content":{"rendered":"<p>CSS transitions allow you to animate the original value of a CSS property to a new value over time, controlling the rate at which property values \u200b\u200bchange. Most properties change their values \u200b\u200bin 16 milliseconds, so the recommended standard transition time is 200ms.<\/p>\r\n<!--more-->\r\n<p>The change of properties occurs when a certain event occurs, which is described by the corresponding pseudo-class. The most commonly used pseudo-class is <span class=\"code\"><a href=\"https:\/\/www.cssportal.com\/css-pseudo-class\/hover.php\">:hover<\/a><\/span>. However, this pseudo-class does not work on mobile devices such as iPhone or Android. A generic solution that works in both desktop and mobile browsers would be JavaScript event handling (like switching classes on click).<\/p>\r\n\r\n<p>Transitions apply to all elements as well as pseudo elements <span class=\"code\"><a href=\"https:\/\/www.cssportal.com\/css-pseudo-class\/before.php\">:before<\/a><\/span> and <span class=\"code\"><a href=\"https:\/\/www.cssportal.com\/css-pseudo-class\/after.php\">:after<\/a><\/span>. To specify all transition properties, we use the shorthand property which is &#8211; <span class=\"code\"><a href=\"https:\/\/www.cssportal.com\/css-properties\/transition.php\">transition<\/a><\/span>.<\/p>\r\n<h3>CSS transition-property Property<\/h3>\r\n<p>Contains the name of the CSS properties to which the transition effect will be applied. A property value can contain either one property or a comma-separated list of properties. When creating a transition, you can use both the initial and final states of the element. The property is not inherited.<\/p>\r\n\r\n<p>The effects you create should be subtle. Not all properties require a gradual change over time due to user experience. For example, when we hover over a link, we want to see an instant change of the link color or the color and underline style. Therefore, transitions should be used for those properties to which you really need to draw attention.<\/p>\r\n\r\n<p>For a list of values that can be used, please see our <a href=\"https:\/\/www.cssportal.com\/css-properties\/transition-property.php\">transition-property<\/a> CSS property page.<\/p>\r\n\r\n<h4>Example Code<\/h4>\r\n<pre class=\"language-css\"><code>div {\r\nwidth: 100px;\r\ntransition-property: width;\r\n}\r\ndiv:hover {\r\nwidth: 300px;\r\n}<\/code><\/pre>\r\n<h3>CSS transition-duration Property<\/h3>\r\n<p>Sets the amount of time during which the transition should take place. If different properties have different transition values, they are separated by commas. If the transition duration is not specified, then the animation will not occur when changing property values. The property is not inherited.<\/p>\r\n\r\n<p>For a list of values that can be used, please see our <a href=\"https:\/\/www.cssportal.com\/css-properties\/transition-duration.php\">transition-duration<\/a> CSS property page.<\/p>\r\n\r\n<h4>Example Code<\/h4>\r\n<pre class=\"language-css\"><code>div {\r\ntransition-duration: .2s;\r\n}<\/code><\/pre>\r\n<h4>Visual Example<\/h4>\r\n\r\n<div class=\"tabs\"><input type=\"radio\" name=\"tabs\" id=\"tabone\"><label for=\"tabone\">HTML<\/label>\r\n<div class=\"tab\"><pre class=\"language-html\"><code>&#x3C;div class=&#x22;wrap&#x22;&#x3E;\r\n  &#x3C;h1&#x3E;Hover over block&#x3C;\/h1&#x3E;\r\n  &#x3C;p&#x3E;transition-duration&#x3C;\/p&#x3E;\r\n  &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n    &#x3C;span&#x3E;0.5s&#x3C;\/span&#x3E;\r\n    &#x3C;div class=&#x22;box1 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n   &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n    &#x3C;span&#x3E;1s&#x3C;\/span&#x3E;\r\n    &#x3C;div class=&#x22;box2 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n   &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n    &#x3C;span&#x3E;2s&#x3C;\/span&#x3E;\r\n    &#x3C;div class=&#x22;box3 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n&#x3C;\/div&#x3E;<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs\" id=\"tabtwo\"><label for=\"tabtwo\">CSS<\/label>\r\n<div class=\"tab\"><pre class=\"language-css\"><code>body {font-family: 'sans-serif', serif; margin: 0;}\r\n.wrap {text-align: center;}\r\n.container {\r\n  display: inline-block;\r\n}\r\nh1 {\r\n  color: #3A262F;\r\n  font-weight:normal; \r\n  text-align: center;\r\n}\r\np {text-align:center;margin-bottom:20px;}\r\n.box {\r\n  height: 40px;\r\n  width: 40px;\r\n  border-radius: 50%;\r\n  margin: 20px 50px 0;\r\n}\r\n.wrap:hover .box {\r\n  -webkit-transform: scale(2);\r\n  -ms-ransform: scale(2);\r\n  transform: scale(2);\r\n}\r\n.box1 {\r\n  background: #FFD592;\r\n  transition: .5s;\r\n}\r\n.box2 {\r\n  background: #F9A88B;\r\n  transition: 1s;\r\n}\r\n.box3 {\r\n  background: #B39190;\r\n  transition: 2s;\r\n}<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs\" id=\"tabthree\" checked=\"checked\"><label for=\"tabthree\">Result<\/label>\r\n<div class=\"tab\"><div class=\"iframe-container\"><iframe style=\"width:100%;height:265px\" src=\"..\/..\/blog-examples\/transition-1.php\"><\/iframe><\/div><\/div>\r\n<\/div>\r\n<h3>CSS transition-timing-function Property<\/h3>\r\n<p>The property defines a time function that describes the speed at which an object transitions from one value to another. If you are defining more than one transition for an element, such as the element&#8217;s background color and position, you can use different functions for each property. The property is not inherited.<\/p>\r\n\r\n<p>For a list of values that can be used, please see our <a href=\"https:\/\/www.cssportal.com\/css-properties\/transition-timing-function.php\">transition-timing-function<\/a> CSS property page.<\/p>\r\n\r\n<p>Check out our new generator &#8211; <a href=\"https:\/\/www.cssportal.com\/css-cubic-bezier-generator\/\">CSS Cubic Bezier Generator<\/a>, this generator will help you visualize animation timing.<\/p>\r\n\r\n<h4>Example Code<\/h4>\r\n<pre class=\"language-css\"><code>div {\r\ntransition-timing-function: linear;\r\n}<\/code><\/pre>\r\n\r\n<h4>Visual Example<\/h4>\r\n<div class=\"tabs\"><input type=\"radio\" name=\"tabs1\" id=\"tabone1\"><label for=\"tabone1\">HTML<\/label>\r\n<div class=\"tab\"><pre class=\"language-html\"><code>&#x3C;div class=&#x22;animation_box&#x22;&#x3E;\r\n&#x9;&#x3C;p&#x3E;Hover over container to view transition-function-timing.&#x3C;\/p&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: ease&#x22;&#x3E;ease&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: linear&#x22;&#x3E;linear&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: ease-in&#x22;&#x3E;ease-in&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: ease-out&#x22;&#x3E;ease-out&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: ease-in-out&#x22;&#x3E;ease-in-out&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: step-start&#x22;&#x3E;step-start&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: step-end&#x22;&#x3E;step-end&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: steps(6, end)&#x22;&#x3E;steps(6, end)&#x3C;\/div&#x3E;\r\n&#x9;&#x3C;div class=&#x22;elem&#x22; style=&#x22;transition-timing-function: cubic-bezier(0, 1.2, 1, -0.6)&#x22;&#x3E;cubic-bezier(0, 1.2, 1, -0.6)&#x3C;\/div&#x3E;\r\n&#x3C;\/div&#x3E;<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs1\" id=\"tabtwo1\"><label for=\"tabtwo1\">CSS<\/label>\r\n<div class=\"tab\"><pre class=\"language-css\"><code>.animation_box {\r\n  padding: 0 15px 15px 15px;\r\n  }\r\n.elem {\r\n  background: #3366ff;\r\n  color: white;\r\n  width: 190px;\r\n  padding: 5px 0 5px 0;\r\n  text-indent: 5px;\r\n  margin: 5px 0;\r\n  transition: width 2s;\r\n}\r\n.animation_box:hover .elem {\r\n  width: 100%;\r\n}<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs1\" id=\"tabthree1\" checked=\"checked\"><label for=\"tabthree1\">Result<\/label>\r\n<div class=\"tab\"><div class=\"iframe-container\">\r\n<iframe style=\"width:100%;height:265px\" src=\"..\/..\/blog-examples\/transition-2.php\"><\/iframe><\/div><\/div>\r\n<\/div>\r\n\r\n<h3>CSS transition-delay Property<\/h3>\r\n\r\n<p>An optional property allows you to make the property change not instantly, but with some delay. Not inherited.<\/p>\r\n\r\n<p>For a list of values that can be used, please see our <a href=\"https:\/\/www.cssportal.com\/css-properties\/transition-delay.php\">transition-delay<\/a> CSS property page.<\/p>\r\n\r\n<h4>Example Code<\/h4>\r\n<pre class=\"language-css\"><code>div {\r\ntransition-delay: .5s;\r\n}<\/code><\/pre>\r\n\r\n<h4>Visual Example<\/h4>\r\n<div class=\"tabs\"><input type=\"radio\" name=\"tabs2\" id=\"tabone2\"><label for=\"tabone2\">HTML<\/label>\r\n<div class=\"tab\"><pre class=\"language-html\"><code>&#x3C;div class=&#x22;wrap&#x22;&#x3E;\r\n  &#x3C;h1&#x3E;Hover over block&#x3C;\/h1&#x3E;\r\n  &#x3C;p&#x3E;transition-delay&#x3C;\/p&#x3E;\r\n  &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n  &#x3C;span&#x3E;0s&#x3C;\/span&#x3E;\r\n  &#x3C;div class=&#x22;box1 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n  &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n    &#x3C;span&#x3E;0.5s&#x3C;\/span&#x3E;\r\n  &#x3C;div class=&#x22;box2 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n  &#x3C;div class=&#x22;container&#x22;&#x3E;\r\n    &#x3C;span&#x3E;1s&#x3C;\/span&#x3E;\r\n  &#x3C;div class=&#x22;box3 box&#x22;&#x3E;&#x3C;\/div&#x3E;\r\n  &#x3C;\/div&#x3E;\r\n&#x3C;\/div&#x3E;<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs2\" id=\"tabtwo2\"><label for=\"tabtwo2\">CSS<\/label>\r\n<div class=\"tab\"><pre class=\"language-css\"><code>body {font-family: 'sans-serif', serif; margin: 0;}\r\n.wrap {\r\n  margin: 20px 0 0;\r\n  text-align: center;\r\n}\r\n.container {display:inline-block}\r\nh1 {\r\n  color: #3A262F;\r\n  font-weight:normal; \r\n}\r\np{margin-bottom:15px;}\r\n.box {\r\n  border-radius: 8px;\r\n  margin: 20px;\r\n  width: 60px;\r\n  height: 60px;\r\n}\r\n.wrap:hover .box {\r\n  transform: scale(1.5);\r\n}\r\n\r\n.box1 {\r\n  background:#BED3E4;\r\n  transition: 1s linear;\r\n}\r\n.box2 {\r\n  background:#BCB4D9;\r\n  transition: 1s linear .5s;\r\n}\r\n.box3 {\r\n  background:#EBC0AD;\r\n  transition: 1s linear 1s;\r\n}<\/code><\/pre><\/div>\r\n<input type=\"radio\" name=\"tabs2\" id=\"tabthree2\" checked=\"checked\"><label for=\"tabthree2\">Result<\/label>\r\n<div class=\"tab\"><div class=\"iframe-container\">\r\n<iframe style=\"width:100%;height:265px\" src=\"..\/..\/blog-examples\/transition-3.php\"><\/iframe><\/div><\/div>\r\n<\/div>\r\n\r\n<h3>Transition Shorthand Property<\/h3>\r\n\r\n<p>All properties responsible for changing the appearance of an element can be combined into one CSS property &#8211; <span class=\"code\">transition<\/span>.<\/p>\r\nThe syntax is:\r\n<pre class=\"no-code syntax\"><code>transition: &#x3C;transition-property&#x3E; &#x3C;transition-duration&#x3E; &#x3C;transition-timing-function&#x3E; &#x3C;transition-delay&#x3E;;<\/code><\/pre>\r\n\r\n<p>Using the default values, the entry:<\/p>\r\n<pre class=\"language-css\"><code>div {\r\n  transition-property: all;\r\n  transition-duartion: 1s;\r\n  transition-timing-function: ease;\r\n  transition-delay: 0s;\r\n}<\/code><\/pre>\r\n\r\n<p>would be equivalent to:<\/p>\r\n<pre class=\"language-css\"><code>div {transition: all 1s ease 0s;}<\/code><\/pre>\r\n\r\n<h2>Smooth transition of several properties<\/h2>\r\n<p>For an element, you can specify several consecutive transitions by listing them separated by commas. Each transition can be decorated with its own temporary function.<\/p>\r\n\r\n<pre class=\"language-css\"><code>div {transition: background 0.3s ease, color 0.2s linear;}<\/code><\/pre>\r\n<p>or<\/p>\r\n<pre class=\"language-css\"><code>div {\r\ntransition-property: height, width, background-color;\r\ntransition-duration: 3s;\r\ntransition-timing-function: ease-in, ease, linear;\r\n}<\/code><\/pre>","protected":false},"excerpt":{"rendered":"CSS transitions allow you to animate the original value of a CSS property to a new value over time, controlling the rate at which property values \u200b\u200bchange. Most properties change their values \u200b\u200bin 16 milliseconds, so the recommended standard transition time is 200ms.","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-css","no-wpautop"],"_links":{"self":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":3,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions\/556"}],"wp:attachment":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}