日本語

CSS calc() Mixing % and px in Width

​​CSS​custom properties,Flexbox,Sass

When you want a fixed-width sidebar and a main column that fills the remaining width side by side, write width: calc(100% - 240px) on the main column. % is a value that depends on the parent's width, and px is a fixed length, but inside calc() you can add and subtract them together. The browser calculates the value when it renders the page, so even when the parent's width changes, the main column is always the parent's width minus 240px.

Note

My test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, headless Chromium 153, and Sass (Dart Sass) 1.108.2. I measured element widths with getBoundingClientRect(). I did not test Firefox or Safari.

Subtracting a Fixed Width from the Parent's Width

This example uses a 240px sidebar, with the main column taking the remaining width.

html
<div class="layout">
  <aside>サイドバー</aside>
  <main>本文</main>
</div>
css
.layout {
  display: flex;
}
.layout aside {
  width: 240px;
  flex-shrink: 0;
}
.layout main {
  width: calc(100% - 240px);
}

In my test environment, I changed only the width of the outer container to 600px, 1000px, and 1400px, and measured the width of main.

text
サイドバー型 container=600px aside=240px main=360px
サイドバー型 container=1000px aside=240px main=760px
サイドバー型 container=1400px aside=240px main=1160px

At every width, main was the outer container's width minus 240px.

Two side-by-side layouts, one above the other. Both have "aside 240px" on the left and a light blue "main" on the right. The upper outer container is 520px wide, and main is 280px. The lower outer container is 756px wide, and main is 516px
When only the outer container’s width changes, main is the container’s width minus 240px

The MDN calc() page also says that the values in an expression can use different units. calc() works anywhere a length can be written, not only for width but also for height, margin, and padding. For example, to make an element fill the screen height below a 60px header, write height: calc(100vh - 60px). In my test environment, with an 800px-tall screen, this gave an element height of 740px.

% Is Based on the Parent's Content Width

For width, % is a percentage of the containing block's width, as described on the MDN width page. If the parent has padding, the base is the content width, which excludes the padding.

In my test environment, I placed a child with width: calc(100% - 100px) inside a parent with width: 500px; padding: 20px; and measured it.

text
親 width:500px; padding:20px (content-box) の子 calc(100% - 100px) = 400px
親 width:500px; padding:20px (border-box、中身の幅 460px) の子 calc(100% - 100px) = 360px

When the parent's box-sizing is the default content-box, width: 500px is the content width, so the child is 500 − 100 = 400px. When the parent is border-box, 500px includes the padding, so the content width shrinks to 460px and the child becomes 360px. I compare box-sizing in more detail in this blog's article on box-sizing and padding.

Spaces Are Required Around + and –

A calc() expression is invalid unless there are spaces on both sides of + and -. In my test environment, I tried different spellings with the parent at 300px wide. CSS.supports() is a function that returns whether the browser accepts a value as valid.

text
width: calc(100% - 240px)             supports=true style.width="calc(100% - 240px)" 親300pxでの幅=60px
width: calc(100%-240px)               supports=false style.width="" 親300pxでの幅=300px
width: calc(100% -240px)              supports=false style.width="" 親300pxでの幅=300px
width: calc(100%- 240px)              supports=false style.width="" 親300pxでの幅=300px

A space on only one side is also invalid. In that case, width is treated as if it were not set, and the element stretched to the full 300px of the parent. The CSS specification (CSS Values and Units Level 4) also says that + and - need spaces on both sides, while * and / can be used without spaces. In fact, calc(100%*2) and calc(100%/3) were valid without spaces.

When a declaration is invalid, the browser ignores that line and uses the value written before it. With a 1000px-wide parent and the following code, width: 50% was used, giving 500px.

css
.box {
  width: 50%;
  width: calc(100%-240px); /* invalid because there is no space */
}

If a width is not what you expect, first check the spaces around -.

For * and /, Use a Plain Number on One Side

With * and /, you multiply or divide a % or px value by a unitless number. Here are my test results.

Expression Valid? Width with parent at 300px
calc(100% / 3 - 20px) Valid About 80px (79.98px)
calc(2 * 100px) Valid 200px
calc(100px * 2px) Invalid 300px (same as not set)
calc(100 / 4)% Invalid 300px (same as not set)
calc(100% - 240) Invalid 300px (same as not set)

The MDN calc() page explains that you cannot multiply two values that both have units, so 100px * 2px is invalid. You also cannot calculate only the numbers and then attach % at the end, as in calc(100 / 4)%. Write calc(100% / 4) instead. Forgetting the unit on the subtracted value, as in calc(100% - 240), was also invalid.

In calc(100% / 3 - 20px), / is evaluated before -, as in normal arithmetic. One third of 300px is 100px, so the calculation is 100 − 20 = 80px. The measured width was 79.984375px.

Three Columns with Gaps: Subtract the Gaps, Then Divide

Here is an example of placing three cards per row, with gap creating spacing between them.

css
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.cards > div {
  width: calc((100% - 2 * 16px) / 3);
}

With three items per row, there are two gaps. So subtract the total gap width, 2 × 16px = 32px, from the parent's width, and then divide by 3. Without the parentheses, 2 * 16px / 3 would be calculated first.

In my test environment, I put six items into an 800px-wide parent and compared the results.

text
3列 calc((100% - 2 * 16px) / 3) gap16px 幅800px: item=255.98px 1行目の数=3 行数=2
3列 calc(100% / 3) gap16px 幅800px: item=266.66px 1行目の数=2 行数=3
Two rows of cards, one above the other. The upper blue row uses width: calc((100% - 2 * 16px) / 3), so three items fit per row across two rows. The lower gray row uses width: calc(100% / 3), so only two items fit per row, creating three rows with empty space on the right
Subtracting the gaps before dividing fits three per row. Without subtracting them, the items wrap to two per row

With calc(100% / 3) and no gap subtraction, the total width of the three items plus the gaps exceeds the parent's width, so the items wrapped to two per row.

Combining with Variables (Custom Properties)

If you store the sidebar width in a variable, you can use it inside calc() with var().

css
.layout {
  --sidebar: 240px;
}
.layout main {
  width: calc(100% - var(--sidebar));
}

With the parent at 1000px, main was 760px.

If you write --sidebar: 240 without a unit, it becomes the same as calc(100% - 240), which is invalid. Unlike forgetting the spaces, in this case width: 50% written before width: calc(100% - var(--sidebar)) was not used.

text
親1000px, --sidebar:240: width:50%; width:calc(100% - var(--sidebar)); → 1000px

According to the MDN var() page, when the value from var() does not fit the property, the property is treated as if it were set to unset. In my test environment, the element stretched to the full 1000px. If you want to store a unitless number in a variable, multiply by 1px, as in calc(100% - var(--sidebar) * 1px). That gave 760px.

In Sass, Wrap the Expression in calc()

In Sass, writing 100% - 240px without calc() causes a compile error.

scss
.main {
  width: 100% - 240px;
}
text
Error: 100% and 240px have incompatible units.
  ╷
2 │   width: 100% - 240px;
  │          ^^^^^^^^^^^^
  ╵
  scss/ng.scss 2:10  root stylesheet

Sass cannot add or subtract % and px directly. When the expression is wrapped in calc(), it stays as a CSS calc() in the output, even when it uses Sass variables.

scss
$sidebar: 240px;
.main {
  width: calc(100% - $sidebar);
  height: calc(100px - 40px);
}
css
.main {
  width: calc(100% - 240px);
  height: 60px;
}

For 100px - 40px, where the units match, Sass computes the result at compile time and outputs 60px. The Sass official documentation on calculations also says that Sass evaluates calculations first when the units can be combined at compile time.

​Posted by 86