DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid

Trending

  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  1. DZone
  2. Coding
  3. Languages
  4. CSS Basics: Creating Inset Borders With CSS

CSS Basics: Creating Inset Borders With CSS

Let's look at how to create inset borders in CSS.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Apr. 22, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
9.8K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes 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.
CSS

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook