Sometimes when creating inset borders, we want to set them at different distances from the edge of the element. In CSS, there are a number of ways to do this, which can all be useful depending on the situation you find yourself in.
Let's look at the different ways to create inset borders with CSS.
Option 1: Using inset borders
The easiest way to make an inset border in CSS with the the border
property:
button {
border: 2px inset rgba(255,255,255,0.5);
}
Option 2: Using outlines
If that doesn't work, we can also use outlines. We can also set an offset for the outline, so that it starts within the element. This is easy to do - it only requires two lines of code
button {
outline: 1px solid rgba(255,255,255,0.5);
outline-offset: -6px;
}
Positives/Negatives
- 👍 Supports rounded corners!
- 👍 Easy to implement.
- 👎 Interferes with usability - which outline is ideally supposed to be used for.
Option 3: Pseudo Elements
In some cases, the easiest way to do the job - pseudo elements look a bit like this:
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;
}
How this works
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 4: Box-shadow inset borders
Instead of using psuedo elements, in some cases we can use box-shadow:
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.