CSS

CSS Inset Borders at Varying Depths

Sponsor

Borders are one of the fundamental parts of the CSS box model. Despite this, we still lack some basic built-in border functionality. One of these "lacking" features is an easy way to do inset borders at differing distances from the edge of the element.

Even though it is not built in, there are a few ways we can accomplish this effect. Let's look at some of the ways to achieve that effect

Option 1: Using outlines

Sometimes, we can also use outlines. This is easy to do - it only requires two lines of code:

css Copy
button { outline: 1px solid rgba(255,255,255,0.5); outline-offset: -6px; }

Positives/Negatives

  • Easy to implement.
  • Interferes with usability - which outline is ideally supposed to be used for.
  • Can't be used with rounded corners!

Option 2: Pseudo Elements

In some cases, the easiest way to do the job - pseudo elements look a bit like this:

css Copy
button { position: relative; } button:before { content: ''; position: absolute; top: 6px; left: 6px; border: 1px solid rgba(255,255,255,0.5); border-radius: 100px; width: calc(100% - 13px); height: calc(100% - 13px); z-index: 999; }

Let's break down what we're doing here

  • First, a pseudo element is a "fake" element made by CSS which is created within any HTML tag. There are two - :before, and :after - so any HTML element can have two pseudo elements.
  • Above, we created one within every button element. It's position is absolute, so it sits on top of the button, instead of within it. It moves according to where the button is, as the button has its position set to relative. We then position it 6px from the top and left of the button.
  • Next, we give it a border, and a border-radius so it has curved edges. We set the width and height of the element to the width or height of the element minus the distance we moved it (taking into account we are moving it "inset" by 12px as we moved it to the left by 6px), and adding the border width, so (6px + 6px + 1px)
  • Finally, we set the z-index-- the vertical order of elements, so the pseudo element sits atop the button, giving us our nice outline.

Positives/Negatives

  • Uses up one of your two precious pseudo elements ☹️
  • Is a standalone element, so we can style it as we see fit!
  • Supports rounded corners!

Option 3: Box-shadow inset borders

Instead of using psuedo elements, in some cases we can use box-shadow:

css Copy
button { box-shadow: inset 0 0 0 5px #e94d71, inset 0 0 0 6px rgb(255 255 255 / 50%); }

In the above example, we put one inset box shadow which is inset 1px more, which is our border color. On top of that, we place another box shadow which is the color of the original button. This gives the illusion of an inset border.

Positives/Negatives

  • Only really works with solid colors/non animated surfaces.
  • Doesn't use pseudo elements
  • Supports rounded corners!
  • Doesn't affect other CSS - you can still add more box shadow layers if you need to.
Last Updated Monday, 25 October 2021

Subscribe to Newsletter

Subscribe to stay up to date with our latest web development and software engineering posts via email. You can opt out at any time.

Not a valid email