Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
There are many ways you can use calc() in your projects. This video covers simple examples that demonstrate the basics of calc().
Resources
calc() operators

+
Addition 

Subtraction 
*
Multiplication 
/
Division

0:00
[SOUND] Hey everyone!

0:05
Guil here, one of the front end teachers here at Treehouse.

0:08
Did you know that you can do simple math operations with CSS?

0:12
In this workshop series, I'm going to teach you how to use

0:15
the CSS calc function, to add a little logic to your CSS.

0:20
First, you'll learn the basics of calc.

0:22
Then I'm going to cover 3 useful ways you can use calc in your web projects.

0:26
Like setting background position offsets, displaying full width elements

0:30
inside padded containers, and creating fluid grid columns.

0:35
calc is a CSS function that performs a calculation and returns a result.

0:40
With calc, you use operators like the plus sign, minus sign, asterisk and

0:45
forward slash to write mathematical expressions that add,

0:49
subtract, multiply and divide values.

0:51
[NOISE] You use the calc function and property values wherever CSS, length, and

0:56
number values are accepted.

0:58
There are many ways you can use calc in your projects.

1:01
So let's start with something really simple to demonstrate

1:04
how the calc function works.

1:05
And later on in the workshop, I'll show you a few handy use cases for calc.

1:09
To follow along in work spaces, click the launch workspace button on this page.

1:14
In the workspace, there's a simple webpage link to a style sheet.

1:19
This index.html file contains a div element with the class,

1:23
"column," and when I preview this page in the browser

1:27
we can see that the column element takes up the full width and height of the page.

1:32
So first, I'll show you how to define width and height values using calc.

1:37
Back in my workspace I'm going to open up the style.css file ,and scroll down

1:42
to the column rule.

1:44
In the column rule I'm going to replace the width value

1:47
of 100% with the value calc.

1:50
So I'm going to type calc, followed by a set of parenthesis.

1:55
Inside the parenthesis is where we write mathematical expressions that add,

1:59
subtract, multiply and divide values.

2:02
And the result of the expression is used as the properties value.

2:07
So I'll start with a simple expression to demonstrate how they work.

2:10
So inside the parentheses using a subtraction operator,

2:14
I'm going to write the expression (150px 50px).

2:20
An expression is evaluated from the left to the right.

2:25
So when the browser calculates 150 pixels minus 50 pixels.

2:30
The result is a 100 pixel wide column element.

2:34
Now if I go back and use the addition operator to change the expression to

2:39
(150px + 50px), the result is now a 200 pixel wide column.

2:47
Knowing that CSS can do math is pretty exciting and impressive.

2:51
But in these examples, you're better off using 100 pixels or

2:55
200 pixels as the width value.

2:57
There's no added benefit in using calc for simple pixel based expressions like this.

3:03
What's most useful about using calc, is how you're able to mix units like

3:07
percentages and pixels, or ems and rems in your expressions.

3:11
This gives you greater control over your layouts.

3:15
For instance, say I'm working with a fluid layout,

3:18
where I don't know the exact width of the parent container.

3:21
And in the container, I want to define a column width that's exactly

3:26
50 pixels less than half of the parent container's width.

3:31
Well I can calculate the width within the CSS,

3:35
using the expression (50% 50px).

3:39
In the expression I'm defining the value 50% for

3:44
half the parent container's width, then subtracting 50 pixels.

3:49
So when we take a look at it in the browser we see how the column will

3:53
always be 50 pixels narrower than half the view port width.

3:58
Even though I use a fixed pixel value in the expression,

4:02
I'm still able to define a fluid width in my layout.

4:07
This is what I meant earlier when I said being able to mix units,

4:10
gives you greater control over a layout.

4:12
Now, if I want a column that's slighter wider then 50%,

4:16
I can use an addition operator.

4:18
So I'm going to change the expression to (50% + 2em).

4:24
So here one em is equal to 16 pixels, so

4:28
now the column container is 32 pixels wider than half the view port size.

4:34
I can also use the calc expression to define the height.

4:37
So I'll go back to my column rule and change the height value to calc and

4:43
then inside the parenthesis I'm going to write the expression 100% minus 3em.

4:50
So when I save my file and refresh the browser,

4:54
you can see how the columns height will always be three em, or

4:58
48 pixels, shorter than the full height of it's container.

5:02
In this case, it's the view port.

5:05
When using addition and subtraction operators in expressions,

5:09
there needs to be a space on both sides of the operators,

5:13
otherwise the browser cannot calculate them.

5:16
For example, if I remove the space between the minus operator and the value 3em,

5:23
the expression 100% negative 3em is now considered invalid, because the browser

5:29
interprets this as a percentage value followed by a negative end value.

5:33
So it cannot calculate this.

5:35
So be sure to use white space around the minus and plus operators.

5:40
So that they're not interpreted as positive or negative values.

5:44
So now that you've learned the basics of calc, in the lessons that follow this

5:48
video you're going to learn three useful ways you can use calc in your projects.
You need to sign up for Treehouse in order to download course files.
Sign up