{"id":3007,"date":"2023-11-17T14:45:27","date_gmt":"2023-11-17T13:45:27","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3007"},"modified":"2023-11-17T14:45:27","modified_gmt":"2023-11-17T13:45:27","slug":"how-to-use-the-css-calc-function-to-optmise-our-stylesheet","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/11\/17\/how-to-use-the-css-calc-function-to-optmise-our-stylesheet\/","title":{"rendered":"How to use the CSS calc() function to optmise our stylesheet"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>The calc() function in CSS allows us to perform mathematical calculations directly in CSS expressions. It can be used to calculate values ??for various CSS properties such as width, height, padding, etc.<\/p>\n\n\n\n<p>The syntax of the calc() function is relatively simple. We can use mathematical operations such as addition (+), subtraction (-), multiplication (*), and division (\/) to calculate values. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>div {\r\n  width: calc(100% - 20px);\r\n  height: calc(50vh + 10%);\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>This example sets the width of the div element to 100% of the available width minus 20 pixels. The height is set at 50% of the available amount plus 10%.<\/p>\n\n\n\n<p>The calc() function also supports the use of units such as pixels (px), percent (%), viewport height (vh), and viewport width (vw). We can also combine different units in one calculation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ combine different units\nheight: calc(50vh - 20px);\n\n\/\/With different units and operations\nfont-size: calc(16px + 2vw);\n\n\/\/In combination with variables\r\n:root {\r\n  --margin: 10px;\r\n}\r\nmargin: calc(var(--margin) * 2);\r<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Fazit:<\/h4>\n\n\n\n<p>The calc() function is particularly useful if we want to create responsive designs or calculate dynamic values ??based on other CSS properties.<\/p>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-red-color\">but, It is important to note that the calc() function cannot be used in all CSS properties. For example, it cannot be used in properties such as <strong>content<\/strong>, <strong>display<\/strong> or <strong>position<\/strong>.<\/span><\/em><\/p>\n\n\n\n<p>Using the calc() function can greatly improve the flexibility and convenience of creating CSS layouts, as it allows complex calculations to be performed directly in CSS<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Let\u2019s see one last time the\u00a0<kbd>calc()<\/kbd>\u00a0function in use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html>\r\n  &lt;head>\r\n    &lt;title>Title of the document&lt;\/title>\r\n    &lt;style>\r\n      .content-container {\r\n        display: flex;\r\n        padding: 40px 0;\r\n        background-color: #d7dade;\r\n      }\r\n      .right {\r\n        width: 100px;\r\n        background-color: #759ac9;\r\n      }\r\n      .left {\r\n        width: calc(100% - 100px);\r\n        background-color: #7cd47b;\r\n      }\r\n    &lt;\/style>\r\n  &lt;\/head>\r\n  &lt;body>\r\n    &lt;div class=\"content-container\">\r\n      &lt;div class=\"left\">Some text&lt;\/div>\r\n      &lt;div class=\"right\">Some text&lt;\/div>\r\n    &lt;\/div>\r\n  &lt;\/body>\r\n&lt;\/html><\/code><\/pre>\n\n\n\n<h5>Result:<\/h5>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"75\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/11\/image-1024x75.png\" alt=\"\" class=\"wp-image-3008\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/11\/image-1024x75.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/11\/image-300x22.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/11\/image-768x56.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/11\/image.png 1297w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the example above, we set the\u00a0<a href=\"https:\/\/www.w3docs.com\/learn-css\/display.html\">display<\/a>\u00a0property of the container to \u201cflex\u201d. For the right\u00a0<a href=\"https:\/\/www.w3docs.com\/learn-html\/html-div-tag.html\">&lt;div><\/a>\u00a0element, we specified the width, while for the left &lt;div>, we used the\u00a0<kbd>calc()<\/kbd>\u00a0function to calculate its width.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The calc() function in CSS allows us to perform mathematical calculations directly in CSS expressions. It can be used to calculate values ??for various CSS properties such as width, height, padding, etc. The syntax of the calc() function is relatively simple. We can use mathematical operations such as addition (+), subtraction (-), multiplication (*), and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3009,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[89],"tags":[757,758,760,759],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3007"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3007"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3007\/revisions"}],"predecessor-version":[{"id":3010,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3007\/revisions\/3010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/3009"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}