Skip to content
⚠️ This article was written in 2020. Some content may be outdated.

CSS Print Optimization Techniques

CSS print-optimization tricks come up often in everyday work, so I've put together a structured summary to help you understand and apply them more easily.

Basic Concepts ​

Here is a practical example:

css
:root {
  --primary: #2563eb;
  --surface: #f8fafc;
  --text: #1e293b;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.component {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(1rem, 3vw, 2rem);
  color: var(--text);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #1e293b;
    --text: #f1f5f9;
  }
}

In a real project, you'll need to adjust it to your actual requirements.

Core Implementation ​

The core code looks like this:

css
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
}

.card {
  container-type: inline-size;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.2s ease;
}

.card:hover {
  transform: translateY(-2px);
}

@container (min-width: 400px) {
  .card__body {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 1rem;
  }
}

Make sure you handle edge cases and exceptions properly.

Practical Application ​

Here's how we can implement it:

css
:root {
  --primary: #2563eb;
  --surface: #f8fafc;
  --text: #1e293b;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.component {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(1rem, 3vw, 2rem);
  color: var(--text);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #1e293b;
    --text: #f1f5f9;
  }
}

This pattern improves the code's maintainability.

Best Practices ​

See the code below for the specific usage:

css
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
}

.card {
  container-type: inline-size;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.2s ease;
}

.card:hover {
  transform: translateY(-2px);
}

@container (min-width: 400px) {
  .card__body {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 1rem;
  }
}

I'd recommend standardizing conventions across the team to reduce inconsistencies.

Summary ​

  • In real projects, picking the right approach matters more than chasing the latest tech.
  • Keep a consistent code style across the team to lower maintenance cost.
  • Keep up with the community and update your technical approach in a timely manner.

MIT Licensed