{"id":1803,"date":"2015-08-31T15:43:42","date_gmt":"2015-08-31T15:43:42","guid":{"rendered":"http:\/\/cssreset.com\/?p=1803"},"modified":"2015-08-31T15:43:42","modified_gmt":"2015-08-31T15:43:42","slug":"csscalcfunction","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/csscalcfunction\/","title":{"rendered":"Understanding The CSS Calc() Function"},"content":{"rendered":"<p dir=\"ltr\">The CSS calc() function is a vital part of many responsive websites these days. It allows developers to have the benefits of both absolute units and percentages in CSS positioning. Things that can seem daunting, such as aligning elements equidistant from each other in any given browser width, are made simple with this pain-relieving function.<\/p>\n<p dir=\"ltr\">Here is a demo of the problem mentioned earlier, aligning elements inside of a rectangular parent div. We are going to use 4 child elements in this case.<\/p>\n<p>To start, create a parent div element then set it\u2019s width to 100%, height to 60 pixels, and background color to #4679bd.<\/p>\n<p>HTML:<\/p>\n<p dir=\"ltr\"><img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"https:\/\/lh4.googleusercontent.com\/F3fySBTEcSefZUdqrNtjyvN7Uq0_ruwvdfzpWLkRTqjloLo3v-ckprdGLCUOCI2ndbTcYYshfLwwYKtcXtM1b2qKR6XHj_MkufMmcid0tTLSs7sjM5VkE6mbKJeewjokr6HwgWI\" width=\"478px;\" height=\"65px;\" \/><\/p>\n<p dir=\"ltr\">CSS:<\/p>\n<p>\u00a0<img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"https:\/\/lh3.googleusercontent.com\/w2yFgLh_Ehdz3Uigyp3AfjTstG2k0tbh95d7w6HWm9h74HuKBXrsFIwyWnST5oGanGjjC0QN2t0rpPEhG7A2i8PzoT7W2fEcEsybEzsPceKQgeRMkG3Q9xSfmLarW-Ezp1GRXeQ\" width=\"477px;\" height=\"139px;\" \/><\/p>\n<p dir=\"ltr\">Next, append four new &lt;div&gt;&lt;\/div&gt; elements to the existing #parentdiv. Give each one a class of \u201cchilddiv\u201d.<\/p>\n<p>HTML:<\/p>\n<p dir=\"ltr\"><img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"https:\/\/lh6.googleusercontent.com\/50E9VcNClMbEwgXA0XQRCk9pFuMXfpq_hMwMhvKW_eNwzaZfPth5Bi_fgMS7n9NiLjP_QY5344eXp2zL2-D0Qs1EGZvR_vYBQYWSe5_1O7Mg-QiP7J3c2FC5fIbVtwZXKKJaZUo\" width=\"524px;\" height=\"172px;\" \/><\/p>\n<p dir=\"ltr\">Great, almost done. Now its time to use the calc() function! Open up your CSS file and add in the following:<\/p>\n<p dir=\"ltr\"><img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"https:\/\/lh5.googleusercontent.com\/g_Clcp2Doxlm0UpmkI5ttrixgn590ZxXRTcTAHOujZZSTtWm6Qi_lk0vLrNH4R-8yKywt2u9UHHopnMzV13NaBWDnx7qaFZsiz1yM3uWe2ro4Qwsr6-WxmN9xlKMhHimRG-EnhA\" width=\"624px;\" height=\"215px;\" \/><\/p>\n<p dir=\"ltr\">We used the calc function to determine the spacing between each element. The 20% &#8211; 24px numbers were determined by imagining the output like this:<\/p>\n<p dir=\"ltr\"><img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"https:\/\/lh4.googleusercontent.com\/8re4vyWQecf7wTZNdRY6_6c5LEWKlUxtF9OuZNELp22BvXw_IUUJJs53WmYdLFho01ztpn6RHkZzD2pdc6a04tKnIlz-BgWaifABfRu6IXS75TuYmeFzG5kW3e36n0NnxXQsg4M\" width=\"624px;\" height=\"159px;\" \/><\/p>\n<p dir=\"ltr\">That demonstration shows that calc(100% &#8211; Ypx)\/(Z + 1) = X where \u201cX\u201d is our result, \u201cY\u201d is the total width of our elements, and \u201cZ\u201d is the # of elements . In this case \u201cX\u201d would be equal to (20% &#8211; 24px).<\/p>\n<p dir=\"ltr\">View the result and code at <a href=\"https:\/\/jsfiddle.net\/MorganMeliment\/La9su80z\/\">https:\/\/jsfiddle.net\/MorganMeliment\/La9su80z\/<\/a><\/p>\n<p dir=\"ltr\">Now, this example only showed elements with equal sizes, but it also works with elements with irregular sizes as well. Just remember to fill out the formula again with the new total width of your elements. Here is an example of that:<\/p>\n<p><a href=\"https:\/\/jsfiddle.net\/MorganMeliment\/505rjm6L\/\">https:\/\/jsfiddle.net\/MorganMeliment\/505rjm6L\/<\/a><\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p dir=\"ltr\">The CSS calc() function is a vital part of many responsive websites these days. It allows developers to have the benefits of both absolute units and percentages in CSS positioning. Things that can seem daunting, such as aligning elements [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/csscalcfunction\/\" title=\"Click to read 'Understanding The CSS Calc() Function'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61,6,36,7,8],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1803"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=1803"}],"version-history":[{"count":1,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1803\/revisions"}],"predecessor-version":[{"id":1804,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1803\/revisions\/1804"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=1803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=1803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=1803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}