-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv-css-calc.html
67 lines (61 loc) · 2.06 KB
/
adv-css-calc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Properties</title>
<style>
/* The calc() CSS function lets you perform arithmetic inside CSS.
Supports simple expressions using operators: +, -, *, /
The + and - operators must be surrounded by whitespace
For instance, calc(50% -8px) will be parsed as a percentage followed by a negative length
— an invalid expression
Also, units must be addable. The following is an invalid expression:
width: calc(10px + 2); the number 2 is undetermined.
However, we can combine units
width: calc(10px + 1rem);
width: calc(50% + 1rem);
It can be used anywhere a <length>, <frequency>, <angle>, <time>, <percentage>,
<number>, or <integer> is allowed.
*/
:root {
--width: calc(100% - 2 * var(--gutter));
--gutter: 1rem;
--primary-margin: 10px;
/* useful trick, create a generic spacer property */
--spacer: 10;
}
h2 {
/* calc sets the unit of the spacer property */
/* converts the spacer to rem */
margin: calc(var(--spacer) * 0.3rem);
/* converts the spacer to px */
margin: calc(var(--spacer) * 1px);
/* converts the spacer to % */
margin: calc(var(--spacer) * 1%);
}
div {
/* margin: auto; centers the div in the viewport */
margin: auto;
border: 1px solid #ccc;
width: calc(10px + 100px);
width: calc(50% - 30px);
width: var(--width);
width: calc(var(--width) - 30px);
}
p {
/* with calc, we can customize css variables */
/* this allows adjusting globally variables for individual elements */
margin: calc(var(--primary-margin) + 2px);
}
</style>
</head>
<body>
<h2>Heading</h2>
<div>
<p>Paragraph 1</p>
<p>Paragragh 2</p>
</div>
</body>
</html>