Front-End Development React Optimization 101: Tips and Tricks Integrating Cloudinary with Astro Building an Image Upload Feature with JavaScript Mastering Image Alignment: Centering Images with HTML & CSS Adding Video to Your React Native App with react-native-video HTML Image Slider: Do It Yourself and 1-Step Image Gallery Widget How to Effectively Manage Digital Assets in a PHP Image Gallery Introducing Angular Image Editor: Your New Editing Too Mastering Javascript Image Annotation Mastering JavaScript Image Popup Python Video Player: 3 Free Options and a Quick Tutorial Image Recognition Machine Learning: Use Cases and Common Algorithms HTML/CSS: How to Center Images Vertically and Horizontally How to Create an Image Map Understand CSS Background Position with 4 Simple Examples Java for Image Processing: 4 Libraries You Should Know Python Video Processing: 6 Useful Libraries and a Quick Tutorial Blur Image CSS: Two Ways to Blur Images for Gorgeous Effects Designing a Video Flipping App for Android Build an App for Embedding Video Watermarks on Android Devices Change Image on Hover with HTML and CSS How to Align Images with CSS Full Page Background Image with CSS: Tutorial and 5 Automation Tips Using CSS to Scale Page Elements and a Better Way to Scale Your Images CSS Background Image: Quick Tutorial and 3 Automation Tips Featured Image: Best Practices to Feature Images on Your Website Image Gallery Websites: Tips and Tricks for a Stunning Image Gallery 6 Ways to Stretch a Background Image with CSS Auto Cropping for Images and Video: Features & Best Practices FLAC vs. WAV: 4 Key Differences and How to Choose Converting Audio to Video: A Practical Guide FLAC vs. AIFF: 5 Key Differences and How to Choose FLAC vs. MQA: 5 Key Differences and How to Choose Converting WAV Files To OGG The Ultimate Guide On Converting OGG Files To WAV Sound Choices: FLAC vs. MP3 AAC vs MP3 – The Future of Audio Files All about AIFF and how it compares to WAV and MP3 Integrating Cloudinary with Netlify Integrating Cloudinary with Svelte and SvelteKit Integrating Cloudinary with Nuxt Integrating Cloudinary with Gatsby File Upload as a Service: How It Works and 5 Leading Solutions Native Mobile App Development Creative Uses for CSS Inner Border and 3 Ways to Set a Border Integrating Cloudinary with Next.js Front-End Development: The Complete Guide

Creative Uses for CSS Inner Border and 3 Ways to Set a Border

css inner border

What Is the CSS border Property?

The CSS border property is a shorthand property that allows you to set the appearance of an element’s border in a single declaration. It can be used to define the border width, border style, and border color for all four sides of an element. The border property is composed of three individual properties:

CSS Border Width

The border-width property: sSpecifies the thickness of the border. You can use length values (e.g., px, em, or rem) or predefined keywords (thin, medium, or thick).

CSS Border Style

The border-style property: dDefines the line style for the border. Some common values include solid, dashed, dotted, double, groove, ridge, inset, and outset.

The following values are allowed:

  • dotted
  • dashed
  • solid
  • double
  • groove
  • ridge
  • inset
  • outset
  • none
  • hidden

The border-style property can have from one to four values.

CSS Examples:

p.dotted { border-style: dotted; }
p.dashed { border-style: dashed; }
p.solid { border-style: solid; }
p.double { border-style: double; }
p.groove { border-style: groove; }
p.ridge { border-style: ridge; }
p.inset { border-style: inset; }
p.outset { border-style: outset; }
p.none { border-style: none; }
p.hidden { border-style: hidden; }

CSS Border Color

The border-color property: sets the color of the border. You can use color names, hexadecimal values, RGB, or RGBA values.

In addition to using these as a shorthand, each property can be set individually for granular control. For instance, border-style can be specified with one to four values, affecting each side of an element differently. If only one value is defined, it applies uniformly to all sides. If two values are used, the first value represents the top and bottom borders, and the second represents the right and left borders.

Similarly, border-width and border-color can also be defined with up to four values to control the appearance of each side of an element separately.

This is part of a series of articles about working with CSS images

In this article:

What Can You Do with CSS Borders?

The CSS border property has numerous use cases in web design and development. Some common applications include:

  • Containing elements: Using borders to visually separate and contain elements like images, text, and other content can help organize the layout and make it easier for users to understand the structure of a web page.
  • Highlighting sections: Applying borders to specific sections or elements on a page can draw attention to them, making them stand out from the surrounding content. This can be useful for highlighting important information, such as calls to action, notifications, or alerts.
  • Table formatting: Borders are often used to style tables, making it easier to read and distinguish between rows and columns. By applying borders to table cells, rows, or the entire table, you can create a clean, organized presentation of data.
  • Visual separation: Borders can be used to create visual separation between different sections or elements within a layout. This can help group related content together or distinguish between unrelated elements, improving the overall readability and organization of a web page.
  • Decorative purposes: Borders can be used for purely decorative purposes, adding visual interest and style to a design. Creative use of border styles, widths, and colors can contribute to a unique and appealing visual appearance.
  • Image frames: Applying a border to images can give them a framed appearance, making them stand out from the background and adding a touch of professionalism to a design.
  • Buttons and form elements: Borders are commonly used to style buttons and form elements, making them more visually appealing and easier to interact with. Different border styles and colors can be used to indicate the state of a button or form element, such as hover, active, or disabled states.
  • Card-based layouts: In modern web design, card-based layouts are prevalent. Applying borders to these cards can help separate content and make it more visually appealing and organized.

Additionally, for text elements specifically, you can use the text-stroke property to add an outline. This CSS property allows for the specification of the outline’s width and color, providing a way to highlight or stylize text content distinctively.

Related content: Read our guide to CSS image overlay

3 Ways to Set an Inner Border in CSS

Here are several ways to apply a CSS inner border.

1. Using the Box-Sizing Property

When you add a border to an element within a container, it increases the container’s size. Adding an inner border provides a workaround for this problem, creating a space between the element or outline property and the border. An inner border does not increase the container’s size. Inner borders can be applied to images, table contents, headers, and text. The shape of an inner border may vary, including square, rectangular, or circular.

The box-sizing property can be used in CSS to establish an inner border. You set this property to border-box to include the padding and border within the container’s dimension. For instance, you could style a division element (div) by setting its width and height to 250px, creating a solid blue border with a width of 10px, and setting the background property to yellow.

The div should thus have the dimensions 250×250 px. The added 10px border doesn’t change the container’s overall dimension because it is included inside the container. For example:

    div {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        width: 250px;
        height: 250px;
        border: 10px solid blue;
        background: yellow;
    }

See the following sample code:

    <html>
    	<head>
    		<title>CSS Border</title>

    		<style>
    		div {
    			box-sizing: border-box;
    			-moz-box-sizing: border-box;
    			-webkit-box-sizing: border-box;
    			width: 250px;
    			height: 250px;
    			border: 10px solid blue;
    			background: yellow;
    		}

    		</style>
    	</head>

    	<body>
    		<h1>CSS Border Property</h1>

    		<div>
    			DIV Box Sizing Property
    		</div>

    	</body>
    </html>

CSS Border Property DIV Box Sizing Property

2. Using the Box-Shadow Property

Another approach to creating an inner border is with the box-shadow property in CSS. It lets you specify an inset shadow with the appearance of a border instead of a shadow. To achieve this effect, set the vertical and horizontal offset values as the first values for the box-shadow.

The remaining values (color, blur, and spread) are optional. Specify a small spread radius to create a narrow shadow and use the inset option to switch the outer shadow to an inner shadow. This shadow will then be inside the container and look like an inner border.

“Front

For instance, you can set the div’s width and height properties to 250px and the background to yellow. Using the box-shadow property, the spread radius should be set to 10px, with the other three options as 0px. Apply the color blue. The code should look like this:

    div {
        width:250px;
        height:250px;
        background-color:yellow;
        box-shadow: 5px 10px #888888;
    }

    We can use the following HTML code:

    <html>
    	<head>
    		<title>CSS Border</title>

    		<style>
    		div {
    			width:250px;
    			height:250px;
    			background-color:yellow;
    			box-shadow: 5px 10px #888888;
    		}

    		</style>
    	</head>

    	<body>
    		<h1>CSS Border Property</h1>

    		<div>
    			DIV Box Shadow Property
    		</div>

    	</body>
    </html>

CSS Border Property Div Box Shadow Property

3. Using the Outline and Outline-Offset Properties

These CSS properties can be used to set an inner border. An outline in CSS is a line outside the border of an element. The outline property specifies an element’s border type, size, and color, while the outline-offset property specifies the space or distance between the outline and the border.

For instance, you could specify the div’s width and height as 250px and set the background to yellow. To create an inner border, divide the border’s width by two (in this case, 5px) and set the outline property as blue. The outline-offset property should have a negative value (-5px) to switch the outer border to an inner border.

This approach works by creating an outer border using the outline property and then inverting it with the outline-offset property. For example:

    div {
        width: 250px;
        height: 250px;
        background: yellow;
        outline: 5px solid blue;
        outline-offset: -5px;
    }

We can use the following code:

    <html>
    	<head>
    		<title>CSS Border</title>

    		<style>
    		div {
    			width:250px;
    			height:250px;
    			background-color:yellow;
    			box-shadow:0px 0px 0px 10px blue inset;
    		}

    		</style>
    	</head>

    	<body>
    		<h1>CSS Border Property</h1>

    		<div>
    			DIV Border Outline Property
    		</div>

    	</body>
    </html>


CSS Border Property DIV Box Outline Property

Related content: Read our guide to css stretch background image.

QUICK TIPS
Colby Fayock
Cloudinary Logo Colby Fayock

In my experience, here are tips that can help you better utilize CSS for inner borders:

  1. Leverage box-sizing for consistent layouts
    When using the box-sizing: border-box; property, you can include the padding and border within the element’s total width and height. This approach helps avoid layout shifts and ensures that the element’s size remains consistent even when adding borders or padding, making it ideal for responsive designs.
  2. Use box-shadow for creative inner borders
    The box-shadow property isn’t just for shadows; it can create the illusion of an inner border. Experiment with different shadow colors, blur radii, and offsets to achieve subtle or bold effects, such as 3D or embossed appearances, without altering the actual dimensions of the element.
  3. Combine outline-offset for layered effects
    By using outline and outline-offset, you can create layered borders that add depth and distinction to your design. Negative values for outline-offset can make an outline appear as an inner border, and combining multiple outlines with varying offsets can achieve intricate visual effects that go beyond simple borders.
  4. Animate inner borders for dynamic UI elements
    CSS transitions and animations can be applied to properties like box-shadow and outline to create dynamic inner borders. For example, animating the outline-offset property can create a “focus” effect, making buttons or inputs feel more interactive when hovered or focused.
  5. Mix inner and outer borders for emphasis
    Use both inner borders (via box-shadow or outline-offset) and traditional outer borders to emphasize important UI elements like buttons or cards. This technique can make elements stand out more effectively, especially in UI designs where visual hierarchy is crucial.
  6. Experiment with border-radius for soft inner borders
    When applying inner borders, remember that border-radius can be used to soften the edges of these borders. Combining box-shadow or outline with border-radius can create smooth, rounded inner borders that contribute to a polished, modern look.
  7. Implement layered border effects with pseudo-elements
    Utilize CSS pseudo-elements (::before and ::after) to add extra layers of inner borders. These can be styled and positioned independently, allowing you to create complex border effects without altering the actual content structure. This technique is particularly useful for creating decorative borders or adding subtle design flourishes.
  8. Ensure accessibility with sufficient contrast
    When using inner borders, especially for interactive elements like buttons, ensure there’s enough contrast between the border and the element’s background. This is crucial for accessibility, as it helps users with visual impairments distinguish between different parts of your UI.
  9. Optimize for performance
    While adding inner borders using CSS properties like box-shadow or outline, be mindful of the potential performance impact on rendering, particularly in complex or large-scale designs. Test across different devices and browsers to ensure smooth performance, especially on mobile devices.
  10. Create custom shapes with SVG for unique borders
    If you need more control or creative freedom, consider using SVG graphics for custom inner borders. SVG allows you to define complex shapes and patterns that can be easily integrated into your layout with CSS, providing a unique and scalable solution for decorative inner borders.
Last updated: Aug 24, 2024