{"id":15342,"date":"2011-11-30T22:01:37","date_gmt":"2011-12-01T05:01:37","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=15342"},"modified":"2011-11-30T22:01:37","modified_gmt":"2011-12-01T05:01:37","slug":"animate-heightwidth-to-auto","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/jquery\/animate-heightwidth-to-auto\/","title":{"rendered":"Animate Height\/Width to “Auto”"},"content":{"rendered":"

It’s not possible to do thing.animate({ \"height\": \"auto\" });<\/code>. So this is Darcy Clarke’s method to allow that to work. You essentially clone the element, remove the fixed heights currently inflicting the element, and measure\/save the value. Then you animate the real element to that value.<\/p>\n

jQuery.fn.animateAuto = function(prop, speed, callback){\r\n    var elem, height, width;\r\n    return this.each(function(i, el){\r\n        el = jQuery(el), elem = el.clone().css({\"height\":\"auto\",\"width\":\"auto\"}).appendTo(\"body\");\r\n        height = elem.css(\"height\"),\r\n        width = elem.css(\"width\"),\r\n        elem.remove();\r\n        \r\n        if(prop === \"height\")\r\n            el.animate({\"height\":height}, speed, callback);\r\n        else if(prop === \"width\")\r\n            el.animate({\"width\":width}, speed, callback);  \r\n        else if(prop === \"both\")\r\n            el.animate({\"width\":width,\"height\":height}, speed, callback);\r\n    });  \r\n}<\/code><\/pre>\n

Usage<\/p>\n

$(\".animateHeight\").bind(\"click\", function(e){\r\n    $(\".test\").animateAuto(\"height\", 1000); \r\n});\r\n\r\n$(\".animateWidth\").bind(\"click\", function(e){\r\n    $(\".test\").animateAuto(\"width\", 1000); \r\n});\r\n\r\n$(\".animateBoth\").bind(\"click\", function(e){\r\n    $(\".test\").animateAuto(\"both\", 1000); \r\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

It’s not possible to do thing.animate({ “height”: “auto” });. So this is Darcy Clarke’s method to allow that to work. You essentially clone the element, remove the fixed heights currently inflicting the element, and measure\/save the value. Then you animate the real element to that value. jQuery.fn.animateAuto = function(prop, speed, callback){ var elem, height, width; […]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3245,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-snippet.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":332875,"url":"https:\/\/css-tricks.com\/almanac\/properties\/a\/aspect-ratio\/","url_meta":{"origin":15342,"position":0},"title":"aspect-ratio","date":"January 20, 2021","format":false,"excerpt":"The CSS property aspect-ratio lets you create boxes that maintain proportional dimensions where the height and width of a box are calculated automatically as a ratio. It's a little math-y, but the idea is that you can divide one value by another on this property and the calculated value ensures\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-29-at-6.23.02-AM.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":376323,"url":"https:\/\/css-tricks.com\/almanac\/properties\/b\/border-image-width\/","url_meta":{"origin":15342,"position":1},"title":"border-image-width","date":"January 12, 2023","format":false,"excerpt":"The CSS border-image-width property is used to determine the width of the border\u2019s image file, as provided by the CSS border-image-source property. .container { border-style: ridge; border-width: 3rem; border-image-source: url('path\/to\/image.jpg'); border-image-slice: 70; border-image-width: 40%; border-image-repeat: repeat; } CodePen Embed Fallback The border-image-width property is defined in the CSS Backgrounds and\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/css-tricks-logo-gradient-outline.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":350477,"url":"https:\/\/css-tricks.com\/almanac\/properties\/b\/block-size\/","url_meta":{"origin":15342,"position":2},"title":"block-size","date":"August 25, 2021","format":false,"excerpt":"block-size is a CSS logical property that defines the height of an element when the writing-mode is horizontal, or the width of the element when the writing-mode is vertical. .element { block-size: 700px; writing-mode: vertical-lr; } block-size is defined in the CSS Logical Properties and Values Level 1 specification which\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/12\/css-tricks-logo-gradient-outline.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":15126,"url":"https:\/\/css-tricks.com\/snippets\/css\/smiley-slider\/","url_meta":{"origin":15342,"position":3},"title":"Smiley Slider","date":"November 20, 2011","format":false,"excerpt":"Requires jQuery and jQuery UI for the actual slider. The face is made from elements made into circles with border-radius. The mouth, indicating happiness level, is another circle just cropped down to size with a parent element with hidden overflow. #face { width: 100px; height: 100px; position: relative; border: 2px\u2026","rel":"","context":"With 6 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":323112,"url":"https:\/\/css-tricks.com\/almanac\/properties\/i\/inline-size\/","url_meta":{"origin":15342,"position":4},"title":"inline-size","date":"October 14, 2020","format":false,"excerpt":"inline-size is a logical property that defines the width of an element when the writing-mode is horizontal, or the height of the element when the writing-mode is vertical. .element { \u00a0 inline-size: 700px; \u00a0 writing-mode: vertical-lr; } As you might have guessed by the example above, the writing-mode property changes\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":321696,"url":"https:\/\/css-tricks.com\/almanac\/properties\/m\/mask-size\/","url_meta":{"origin":15342,"position":5},"title":"mask-size","date":"September 23, 2020","format":false,"excerpt":"In CSS, the mask-size property specifies the size of a mask layer image. In many ways, it works very much like the background-size property. .element { \u00a0 mask-image: url(star.svg); \u00a0 mask-size: 200px 100px; } Masking allows you to display selected parts of an element while hiding the rest. The size\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/15342"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=15342"}],"version-history":[{"count":2,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/15342\/revisions"}],"predecessor-version":[{"id":15344,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/15342\/revisions\/15344"}],"up":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3245"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=15342"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=15342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}