Skip to content

CSS if() Function Proposal: 条件逻辑进入 CSS 早期讨论

The CSS if() function proposal has evolved quickly in recent years. This article digs into how it works and how to apply it in practice.

Basic Concepts ​

See the following code for specific usage:

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;
  }
}

It is recommended to unify conventions within the team to reduce inconsistency.

Core Implementation ​

Let's look at the specific implementation:

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;
  }
}

This implementation is concise and efficient, suitable for most scenarios.

Practical Application ​

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 real projects, you need to make appropriate adjustments based on specific requirements.

Best Practices ​

The core code is as follows:

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;
  }
}

Be sure to handle edge cases and exceptions properly.

Summary ​

  • Base performance optimization on real data and avoid premature optimization
  • CSS if() 函数提案的核心在于理解底层原理,而非仅仅记住 API
  • In real projects, choosing the right solution matters more than chasing the newest technology

MIT Licensed