Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

The CSS Calc() Function For A More Consistent Responsive Design

10/21/2017

0 Comments

 
When I'm designing a web application that I want to be responsive, I frequently use the units vh and vw (which stand for viewport  height and viewport width). These are great because they allow you to size things based on a percentage of the viewport height. However, sometimes you want things sized at 100% of the height or width minus a constant, and that's where the calc function comes in handy!

The Problem With Vh and Vw

The css units vh and vw are awesome for sizing things based on a multiple of the height or width of the window. For example, here's a div whose height and width will be exactly half the viewport's height and width, and it will stretch to resize itself even when you resize the window. Now that's nifty! However, it realistically doesn't make sense to have EVERYTHING scale as a percentage of viewport height. For example, suppose you want to create a div that takes up basically the entire page but has a 10px margin on all sides. You could make it's height 98vh or something like that, but the margin is going to be shrinking and expanding along with the div- not what we wanted.

The Calc Philosophy- Why Not Both?

The beautiful thing about the CSS calc function is that it allows you to combine different types of CSS units! So my tough-to-size div described above could be expressed trivially with this css code:
height: calc(100vh - 20px);
width: calc(100vw - 20px);
And here's a nicely embedded plunkr demo for you to play with:

It's Regular CSS!

It's pretty funny because at first I was trying to figure out how I would add Sass to my React project so that I could use the calc function in my css code. I was happy to then find out that I didn't need sass at all to use calc! Browsers just support it as straight up css! And at this point it's really supported pretty much everywhere (only not supported by mobile opera, IE 8, blackberry browser, and really old versions of android os.
source: http://caniuse.com/calc/embed).

Building Responsive Sites & Applications

Sometimes you use bitmap images. Sometimes you have headers, footers, or other elements with a fixed height or width. A key part of responsive design is having elements sized and/or positioned proportionally to the height or width of the device size. Now with something like calc(90vh - 20px) we can add in that fixed pixel exception and have pixel perfect precision of our UI elements  with any device in a way that is easy to reason about and needs very little code.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021