liveBook · Manning

Calc()

As explained in Tiny CSS Projects

The CSS calc() function computes a property value from arithmetic expressions involving CSS values and units.

The CSS calc() function computes property values from arithmetic expressions involving CSS values and units. It is useful when a layout needs a relationship rather than a fixed number. section 5.4.2

Use calc() to:

  • Subtract padding and margins from a percentage: height: calc(100% - 4rem) fills a card without exceeding its available space. section 5.4.2
  • Create a negative offset: calc(-1 * var(--imageSize) / 3) moves an image upward by one third of its size. section 6.5.2
  • Reserve space for that offset: calc(var(--imageSize) / 3 + 24px) keeps the image from being clipped on short screens. section 6.5.2

Sizing and positioning a card

A card’s inner container can use height: calc(100% - 4rem) with grid-template-rows: min-content auto min-content. The header and link take only the space they need, while the paragraph receives the remainder. A profile card can similarly combine calc() with --imageSize to let a circular image overlap the card while preserving space above it. section 5.4.2 section 6.5.2

Listing 5.14 Inner container layout adjustments
main > section > div {
  background-color: rgba(0, 0, 0, .75);
  margin: 1rem;
  padding: 1rem;
  color: whitesmoke;
  text-align: center;
  height: calc(100% - 4rem);
  display: grid;
  grid-template-rows: min-content auto min-content;
  align-items: center;
}
 
section h2 {
  font-size: 1.3rem;
  font-weight: bold;
  line-height: 1.2;
  margin-bottom: 0;
}

View the complete inner-container rule using calc() alongside CSS Grid row sizing and centered alignment. section 5.4.2

Listing 6.7 Positioning the image
.card {
  ...
  text-align: center;
  margin: calc(var(--imageSize) / 3 + 24px) 24px;   #1
}
 
img {
  width: var(--imageSize);
  height: var(--imageSize);
  object-fit: cover;
  border-radius: 50%;
  margin-top: calc(-1 * var(--imageSize) / 3);      #2
}

View the profile-card rule using calc() with a custom property for both image overlap and protective card spacing. section 6.5.2

How Tiny CSS Projects covers it

How deep it goes
Explains subtraction, division, multiplication, negative results, and custom-property reuse through two worked card layouts.
Examples
Demonstrates card height subtraction, grid rows, image overlap, negative margins, and spacing that prevents clipping on short screens.
What it assumes
Assumes basic CSS declarations, units, margins, percentages, and custom properties while teaching the arithmetic through concrete layouts.
What you can do after
Apply calc() to build flexible card dimensions and image offsets that respond to available space and reusable size variables.
How it connects
Connects arithmetic expressions with CSS Grid, margins, CSS custom properties, responsive design, and card layout.

Also explained in

Compare how 3 books explain calc() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.