Blog
All posts
0:00

Mehdi Kazi
11/2/22
Created on November 2, 2022 at 11:34 pm
·
threads-blog

threads-blog
Why we use the CSS property box-shadow instead of border in our app
Watch narration
0:00
0:00
If you inspect the html and CSS of the .threads.com site, you might notice something peculiar. On a div with a border, you'd expect to see the CSS property
border
, but instead, you'll see box-shadow
. Why is that?
0:00
Figma vs. the browser
Our design system works on a 4px grid, which means our spacing—margins and padding—is a multiple of 4.
However, we noticed something kept breaking the grid. Designs would look great in Figma, but they'd be a few pixels off in the browser.
What we found was there's an inconsistency between Figma and browsers when it comes to spacing around borders:
0:00
What does that mean?
For example, suppose we want to create a box with the following specifications:
width: 40px
height: 40px
border: 1px solid black
margin-right: 8px # orange
0:00
How it looks in Figma
0:00
How it looks in the browser
0:00
What problems does this cause?
Designers work in Figma, and Engineers work in the browser. This discrepancy creates friction between engineering and design, and makes it difficult to maintain consistency across the product. This creates issues such as:
0:00
What options did we consider to reconcile the differences?
Engineering adapts to the design system
:threadcount-1
With this solution, we would create a system where engineers understand this discrepancy and know how to update the spacing based off the Figma file. For example, they'd write the CSS:
width: 39px
height: 39px
border: 1px solid black
margin-right: 6px
# If there were padding, we'd implement it as (figmaPadding - borderWidth)
# For example if Figma has padding of 8 and 1px border - we write `padding: 7px` to account for the border taking space
0:00
We could create helper components and functions that make this easier to do this such as creating a
div
wrapper that transforms the spacing input based on whether a border has been defined. However, this would cause a discrepancy between the code and inspector, which makes it difficult to debug UI related bugs.0:00
Design and Engineering both use box-shadow instead of border
:threadcount-2
Borders in Figma act just like box-shadows in browsers. In this solution, both systems would stop using borders completely and only use box-shadows to represent them. For example
border: 1px solid black
is equivalent to box-shadow: 0px 0px 0px 1px black
.0:00
Using box-shadow gives an effect of a border while taking up no space, which lets the remaining element attributes work as expected. Even though
border
and box-shadow
are basically the same thing in Figma, Design would only use box-shadow
to ensure consistency between the team and to ensure we have a common language between design and engineering.0:00
What approach did we take?
We decided to go with Option
. Design and Engineering use
:threadcount-2
box-shadow
instead of border in both Figma and the browser. With this approach, engineers can copy the css properties directly from Figma instead of needing to know how to convert a
border
to a box-shadow
.As a result, when we polish a section of the frontend, it's much easier to make our designs pixel perfect.
Design and Engineering speak a common language, and we all move faster together.

:sparkles
0:00
If you have any questions about using the box-shadow instead of border or other similar topics, feel free to drop me a line at mehdi@threads.com!
:cat-wave
Comments
©2022 Threads, Inc.