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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  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
DZone Core CORE ·
Apr. 22, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.6K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!