CSS Basics: Creating Inset Borders With CSS
Let's look at how to create inset borders in CSS.
Join the DZone community and get the full member experience.
Join For FreeSometimes when creating inset borders, we want to set them at different distances from the edge of the element. There are several ways to do this in CSS, which can all be applicable 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 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 to start 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
- Positive: Supports rounded corners!
- Positive: Easy to implement.
- Negative: 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, 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. Its 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 to have 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 excellent outline.
Positives/Negatives
- Negative: Uses up one of your two precious pseudo-elements ☹️
- Positive: It is a standalone element, so we can style it as we see fit!
- Positive: Supports rounded corners!
Option 4: Box-Shadow Inset Borders
Instead of using pseudo 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%);
}
We put one inset box-shadow in the above example, 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
- Negative: It only really works with solid colors/non-animated surfaces.
- Positive: Doesn't use pseudo-elements
- Positive: Supports rounded corners!
- Positive: It doesn't affect other CSS - you can still add more box-shadow layers if needed.
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments