惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
CSS Variables With Inline Styles
Ahmad Shadeed · 2019-11-04 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

While working on a demo for an article, I needed an easy way to create a grid layout. For example, laying out a grid of five columns without touching CSS every time I change my mind. Throughout this article, I will explore some use cases and think out loud with you on how to do them.

How It Works

Before diving into the concept, let me explain CSS Variables basics. You might know them as Custom Properties as well.

CSS Variables are supported natively in all major browsers. They allow us to use real variables in the browser. Here is brief of the support table:

To define a CSS Variable, it needs to be added to the :root declaration if it’s global (:root is equivalent to <html> except that :root has a higher specificity). If the variable is specific to a component, it can be defined inside its declaration.

In the below example, I defined a global variable --size which will be used for the width and height of a square element.

:root {
  --size: 50px;
}

.square {
  width: var(--size);
  height: var(--size);
}

What if --size is not defined at all? It’s possible to define a default or fallback variable, in case the passed one is not valid.

In the example below, I added var(--size, 10px). In case --size wasn’t valid, the width and height values will fallback to 10px.

.square {
  width: var(--size, 10px);
  height: var(--size, 10px);
}

Adding on that, it’s possibe to use a CSS variable inside an inline CSS style. For example:

<div class="elem" style="--background: red;"></div>
.elem {
  background: var(--background);
}

All of the examples in this article will be based on the above concept. Now that you understood the basic idea let’s explore some real-life examples.

CSS Grid Examples

In this design, I will use CSS grid for the following: 1- Sidebar and Main 2- Form Items 3- Three Column Layout

The sidebar will take a fixed width, and the main will be fluid. Let’s suppose that the sidebar width is 240px.

<div class="o-grid" style="--columns: 240px 1fr">
  <aside></aside>
  <main></main>
</div>
.o-grid {
  display: grid;
  grid-template-columns: var(--columns);
}

Form Items

As per the design, there are two columns per each row. Here is a sample from the HTML.

<div class="o-grid" style="--columns: 1fr 1fr">
  <div class="form-group"></div>
  <div class="form-group"></div>
  <div class="form-group"></div>
  <div class="form-group"></div>
</div>
.o-grid {
  display: grid;
  grid-template-columns: var(--columns);
}

Three Column Layout

In the below example, I added --repeat-number: 3 and --gap: 8px as inline CSS. Those variables are added to the o-grid class. The result of the grid will be based on those variables.

<div class="o-grid" style="--repeat-number: 3; --gap: 8px;">
  <div></div>
  <div></div>
  <div></div>
</div>
.o-grid {
  display: grid;
  grid-template-columns: repeat(var(--repeat-number), 1fr);
  grid-gap: var(--gap, 0);
}

I like that in CSS Variables, it’s possible to add a default value, in case the variable hasn’t been set. In the above code, I used var(--gap, 0). In case the author didn’t provide the --gap variable, the default will be zero.

Dynamic Grid Items With minmax

For me, this is a widespread use case and is essential. I use Grid minmax a lot, but I face an issue while using it on multiple pages.

Let’s take a basic example without CSS variables.

In CSS, I used minmax to define a minimum width of 250px for each grid item.

.o-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr);
    grid-gap: 16px;
}

Now, what should I do in case there a design that requires the grid item width to be at least 300px. Do I need to create a class variation like the below?

.o-grid--2 {
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
}

Imagine having five different grids, each with a varying width of the item. The above won’t be the right solution.

With CSS Variables, I can do the following:

.o-grid { display: grid; grid-template-columns: repeat(auto-fit,
minmax(var(--item-width), 1fr); grid-gap: var(--gap); }

And in HTML, I can set any width and gap I want.

<!-- Example 1 -->
<div class="o-grid" style="--item-width: 250px;">
  <div></div>
  <div></div>
  <div></div>
</div>

<!-- Example 2 -->
<div class="o-grid" style="--item-width: 350px;">
  <div></div>
  <div></div>
  <div></div>
</div>

<!-- Example 3 -->
<div class="o-grid" style="--item-width: 150px;">
  <div></div>
  <div></div>
  <div></div>
</div>

Isn’t that much better than creating a class variation whenever the width is changed?

See the Pen CSS Variables and Inline Styles by Ahmad Shadeed (@shadeed) on CodePen.

Flexbox Examples

In the example, there is an article header with a row containing the author’s name and tags. I was experimenting in a layout while designing in the browser and needed a quick way to toggle between how those items are placed.

<div class="article-header">
  <h2>Article title</h2>
  <div class="article-header__meta" style="--justify: space-between;">
    <p>By Ahmad Shadeed</p>
    <p>Published under: CSS, Design</p>
  </div>
</div>
.article-header__meta {
  display: flex;
  justify-content: var(--justify);
}

With that in hand, I can tweak the inline style to change the value to another keyword. I found this to be useful while doing quick prototyping or even production websites.

Buttons

Button Width

I found that the technique also works for button elements. Let’s suppose that there is a form with two input fields and a button.

What I’m aiming to do is to control the button’s width by using an inline CSS Variable. Sometimes, a button should take 100% width of its parent.

<button class="c-button" style="--width: 100%;">Submit</button>
.c-button {
  /* Other styles */
  width: var(--width, initial);
}

Button Color

Another useful use for that is when there is a ghost button (outline button). The button color could be anything and by using a CSS Variable for it, changing the color would be easy.

<button class="c-button c-button--ghost" style="--color: #5e35b1;">
  Save Edits
</button>
<button class="c-button c-button--ghost" style="--color: #ec2828;">
  Delete
</button>
.c-button--ghost {
  /* Other styles */
  background: transparent;
  color: var(--color, #000);
  border-color: currentColor;
}

What about hover effects? I will use the same concept for them. When hovered, the button background will become solid, and the font color is white.

.c-button--ghost {
  background: transparent;
  color: var(--color, #000);
  border-color: currentColor;
  transition: 0.3s ease-out;

  &:hover {
    background: var(--color);
    border-color: var(--color);
    color: #fff;
  }
}

See the Pen CSS Variables and Inline Styles - 2 by Ahmad Shadeed (@shadeed) on CodePen.

User Avatars

With a varying size for each avatar, this is a good fit for the concept. Let’s suppose that there are four different sizes of a user’s avatar.

In CSS, I defined the below styles:

.c-avatar {
  display: inline-block;
  margin-right: 2rem;
  width: calc(var(--size, 1) * 30px);
  height: calc(var(--size, 1) * 30px);
  object-fit: cover;
  border-radius: 50%;
  box-shadow: 0 3px 10px 0 rgba(#000, 0.2);
}

By using Calc() function, I can pass a size modifier, which will be multiplied by a base width value. The size modifier will be used in HTML.

<img src="user.jpg" alt="" class="c-avatar" style="--size: 1" />
<img src="user.jpg" alt="" class="c-avatar" style="--size: 2" />
<img src="user.jpg" alt="" class="c-avatar" style="--size: 3" />
<img src="user.jpg" alt="" class="c-avatar" style="--size: 4" />

See the Pen CSS Variables and Inline Styles - 3 by Ahmad Shadeed (@shadeed) on CodePen.

The End

And that’s a wrap. Do you have a comment or a suggestion? Please feel free to ping me on @shadeed9.