loading..

loading, please wait...
Mohit Aneja a.k.a CSSJockey

CSSJockey.com

Hello! I am Mohit Aneja, Designer and Developer based in India.
I believe in creating a Unique & Practical Web Presence not just html pages.
If you need some cool stuff, let's talk :)

Create Smarter Layout with CSS calc() Function

A web page always start with specifying size of an element, that’s why its always been a most anticipated feature to calculate size of an element with ease. Well here is a new addition to CSS3 Values as a new function calc() allows you to define element size using custom method. You can perform calculation for any CSS property which supports lengths in expressions. For example, take a look at following syntax:

<style type="text/css">
#test{
   border:1px solid rgba(0,0,0,0.4);
   width:400px;
   padding:0px 10px;
}
#test p{
   width: -webkit-calc(350px - 50px);
   background:#f0f0f0;
   padding:5px;
}
</style>
<div id="test">
   <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div>

Using this code produces the following result:

Don’t forget to use -vendor prefixes to make it work, for Google Chrome and Safari you should use -webkit and for Mozilla Firefox you should use -moz. With calc() function you can use any of the following arithmetic operators:

Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)

Please not that if you using * or / you must not define a value to the length, otherwise it won’t work. For example:

#test p{
   width: -webkit-calc(350px / 5);
}

You can use CSS calc() function anywhere a length or number can be used. In near future there will be more properties added to calc() but as of now it supports only these. When it comes to browser compatibility, current stable version of Google Chrome 18 doesn’t supports it, but Chrome Canary 19+ supports it very well. Mozilla Firefox also supports it but don’t forget to use -moz prefix before calc(). Let me know what do you think of new CSS calc() function and how do you plan to use it?

Please note that calc() function doesn’t works in current stable of version of Google Chrome, you can use Chrome Canary or latest version of Mozilla Firefox to check out this function.

You may also like to read..

2 Comments on Create Smarter Layout with CSS calc() Function


  1. Jul 31, 2012 / Chris:
    Thanks for posting this info. Interesting. I haven't tried this, but I suppose you could use the calc() function to set the width of multi-column layouts. I have a layout that almost looks like the designer set it up as a table. It is 980px containing max of 4 content columns with 20px gutters on left (1st col) and right (all cols). There are also 5 "rows" of these columns with the columns of varying widths but had to be of equal height. I set a min-height on the each col and they are floated. This was a challenge and although the layout works, I'm not thrilled with it as it seems I'll be tweaking the column widths/heights, padding and margins as content changes. Using table markup would have been faster and more intuitive (imo), but it couldn't be used for layout. At any rate, I suppose in that scenario I would use calc(). Any thoughts? Reply
  2. May 22, 2012 / Shahbaz Ahmed Bhatti:
    i dont clear this tutorial can u please put a demo also it a request u plase put a demo also on your webiste so new user can see eaisly Reply

Leave a Reply

Stay up-to-date

Get latest tutorials and articles directly in your email.

Subscribe in RSS Reader

Connect via Facebook

Follow me on Twitter

Visit our Google+ Profile

Connect via Skype

I keep my Skype open all day long while I work on my computer.
Feel free to ping me to start conversation.

I love good conversations!!
If you have some big ideas, suggestions, feedback,
or want to discuss your project give me a shout.

 
%d bloggers like this: