Key takeaways
- The easy answer: box-shadow: inset 0 0 0 4px blue; draws an inside border without changing the element’s size.
- For images, or anything whose content reaches the edges: use outline with a negative outline-offset instead, because an inset shadow is painted below the content and the content hides it.
- All of these methods follow border-radius in current browsers, despite some outdated tutorials that still say the outline method cannot do rounded corners.
- If the border has to survive outside your CSS, in an email or a PDF, bake it into the image file at delivery instead.
How Do You Put a Border Inside an Element in CSS?
To draw a border inside an element, use an inset box shadow. Set both offsets and the blur to zero, give the spread radius the thickness you want, and add the inset keyword:
.card {
box-shadow: inset 0 0 0 4px blue;
}
The four lengths are horizontal offset, vertical offset, blur radius, and spread radius. MDN documents that a positive spread value causes the shadow to expand and grow bigger, and that inset changes the shadow from an outer box shadow to an inner one. A 4px spread with no offset and no blur paints a solid 4px line hugging the inside edge of the element.
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: specifies 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: defines 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; }
Two things about this list are worth knowing before the inner border methods below:
First, a border with no border-style is not drawn at all, whatever width and color you give it, which is the most common reason a border fails to appear.
Second, the inset and outset values here are bevel effects that shade the four sides differently to suggest depth. They do not move the border inside the element, and they are unrelated to the inset keyword used with box-shadow.
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-webkit-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
Pro Tip
Consider Cloudinary’s Digital Asset Management
Looking for a centralized location to manage all your media assets? Cloudinary’s Media Library makes it easy to upload, store, manage, and deliver images and videos efficiently.
What Is an Inner Border in CSS?
An inner border, also called an inside border, is a visible line drawn within the space an element already occupies instead of added around the outside of it. The distinction matters because of the way the default CSS box model measures elements. Under box-sizing: content-box, which MDN documents as the initial and default value, width and height describe the content area only, and any border is added on top of it. MDN’s own example is exact: .box { width: 350px; border: 10px solid black; } renders a box that is 370px wide.
Those extra 20px are the whole problem. Add a border to a card on hover and the card grows by twice the border width in each direction, every neighbor shifts, and the layout jumps under the cursor. An inner border avoids that by painting the line inside the footprint the element already has, so nothing around it moves.
Two different needs hide behind the same search:
- A line drawn inset from the element’s edge, with background or content still visible outside it. This is a true inner border, and box-shadow: inset, outline with a negative offset, and pseudo-elements all produce it.
- A normal border that does not make the box any bigger. This is a sizing question rather than a drawing question, and box-sizing: border-box is the answer. The line still sits on the outer edge, it just stops counting as extra width.
The methods below cover both needs.
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
Adding a standard border to an element within a container increases the overall dimensions of that container. To avoid this, an inner border can be used as an effective alternative. It creates visual separation between the element and its outline or surrounding content without altering the container’s size. Inner borders can be applied to various elements, such as images, table cells, headers, and text. They can also take on different shapes, including square, rectangular, or circular, depending on the design requirements.
In CSS, the box-sizing property is a useful tool for creating inner borders. By setting it to “border-box”, the element’s padding and border are included within its defined width and height, preventing the overall size from increasing. For example, you can style a “div” by assigning it a width and height of 250px, applying a 10px solid blue border, and setting the background color to yellow, all while maintaining the element’s intended dimensions.
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>
Two notes on this example.
The -moz-box-sizing and -webkit-box-sizing lines are no longer needed: MDN lists box-sizing as Baseline widely available, with support across browsers since July 2015, so the unprefixed property on its own is enough. And it is worth being precise about what this method does, because it answers the sizing half of the question rather than the drawing half. box-sizing: border-box does not move the line inside the element. The border still sits on the outer edge; the element is simply measured so that width and height include the padding and border instead of excluding them. That is why the div above renders 250px by 250px rather than 270px by 270px.
Most projects set this once, globally, rather than per element:
Most projects set this once, globally, rather than per element:
*,
*::before,
*::after {
box-sizing: border-box;
}
If the only thing you were trying to avoid was the layout jump on hover, there is a simpler fix than any method on this page: reserve the space with a transparent border from the start, then change only its color.
.card {
border: 4px solid transparent;
}
.card:hover {
border-color: blue;
}
The size never changes, because the border is always present and only becomes visible.
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.
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: 0px 0px 0px 10px blue inset;
}
<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 Box Shadow Property
</div>
</body>
</html>
Two caveats before you commit to this method, both documented by MDN. Inset shadows are clipped to the element’s padding box and appear above the background but below the content. If the element holds an image, or a child whose background reaches the edges, that content paints over the line and hides it. Padding on the parent solves it. And because the shadow sits on the padding box rather than the border box, an element that also carries a real border shows the inset shadow inside that border rather than underneath it.
The payoff is that shadows take no part in layout, which makes this the right method for hover and focus states. Nothing moves when the line appears:
.card {
transition: box-shadow 150ms ease;
}
.card:hover {
box-shadow: inset 0 0 0 4px blue;
}
You can also stack comma separated shadows to build layered rings inside one element:
div {
box-shadow:
inset 0 0 0 4px white,
inset 0 0 0 10px blue;
}
A shadow has no style keyword, so an inset box shadow is always a solid line. For a dashed or double inner border, use the outline method below.
3. Using the Outline and Outline-Offset Properties
CSS offers properties that can effectively create the appearance of an inner border. The outline property draws a line outside an element’s border and allows you to define the style, width, and color of that line. To control the space between the outline and the actual border, the outline-offset property is used. Together, these properties help achieve a layered border effect without affecting the element’s layout or dimensions.
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>
Unlike an inset shadow, an outline is not clipped to the padding box, so it does not disappear behind an image or a child that reaches the element’s edges. That makes it the better inside border for framed images and thumbnails.
Make the offset more negative than the outline width and the line floats inside the element with a visible gap, which is the classic matted frame look. This is also the only method here that can draw a line that isn’t solid. outline-style accepts dotted, dashed, solid, double, groove, ridge, inset and outset, the same line style keywords as border-style apart from hidden.
The cost is that you have spent the element’s only outline. On anything interactive that line is the keyboard focus indicator, so read the focus ring section below before putting this on a button, link, or input.
Which Inner Border Method Should You Use?
All three methods above put a line inside the element without changing its footprint, and a fourth option, an overlay drawn on a pseudo-element, reaches the cases none of them do. They differ in where the line can sit, what paints over it, and what you give up to get it.
| Method | Where the line is drawn | Changes the element’s size | Follows border-radius | Main caveat |
|---|---|---|---|---|
| box-shadow: inset | Inside, on the padding box | No | Yes | Clipped to the padding box and painted below the content, so edge-to-edge content hides it. Always a solid line. |
| outline with a negative outline-offset | Inside, offset from the border edge | No | Yes, in current browsers | Uses up the element’s focus ring, so avoid it on interactive elements. No per-side control. |
| box-sizing: border-box | On the outer edge, as normal | No, the border is counted inside width and height | Yes | Does not inset the line. It only stops the border from growing the box. |
| ::after overlay | Anywhere you position it | No | Yes, and set independently of the parent | Needs position: relative on the parent, and pointer-events: none if the element is clickable. |
Do Inner Borders Work with border-radius?
An outline’s style, color and width are controlled through the outline property, its distance from the border through outline-offset, and its corner angles through the border-radius property. Shadows behave the same way: if a border-radius is specified on the element with a box shadow, the box shadow takes on the same rounded corners.
div {
width: 250px;
height: 250px;
border-radius: 24px;
background: yellow;
outline: 10px solid blue;
outline-offset: -10px;
}
.pill {
border-radius: 999px;
box-shadow: inset 0 0 0 6px blue;
}
How Do You Set an Inner Border on One Side Only?
For reliable per side control, draw the border on an ::after pseudo-element positioned over the parent. That keeps the line independent of the element’s own box, background and outline, which also makes it the method to reach for when the outline has to stay free for a focus ring.
div {
position: relative;
width: 250px;
height: 250px;
background: yellow;
}
div::after {
content: "";
position: absolute;
inset: 12px;
border-left: 3px solid blue;
border-bottom: 3px solid blue;
pointer-events: none;
}
Three requirements make this behave.
- The parent needs
position: relativeso the overlay is positioned against it. - The inset shorthand sets top, right, bottom and left in one declaration, and it is what controls how far in the line sits; set it to 0 to put the line flush against the edge.
pointer-events: noneisn’t optional for anything clickable, because without it the overlay swallows clicks meant for the element underneath. The pseudo-element is absolutely positioned out of flow, so the parent’s size does not change.
Does an Inner Border Break the Keyboard Focus Ring?
It can, and this is the caveat none of the tutorials ranking on this query mention. outline is what browsers draw to show keyboard focus, and an element has only one. Using it for decoration on a button, link or input means the decorative ring and the focus indicator are the same line, so a keyboard user gets no visible change when they tab onto the control.
MDN’s warning is direct: assigning outline a value of 0 or none removes the browser’s default focus style, and if an element can be interacted with it must have a visible focus indicator. So on interactive elements, draw the decoration with box-shadow or a pseudo-element and leave the outline free for focus:
button {
/* decoration, not the focus ring */
box-shadow: inset 0 0 0 2px blue;
}
button:focus-visible {
/* the focus ring, still available */
outline: 3px solid navy;
outline-offset: 2px;
If you do need the outline for decoration on a control, you have to replace the focus indicator with something of your own, and it has to satisfy WCAG 2.2 success criterion 2.4.7, Focus Visible, a Level AA criterion which requires that any keyboard operable user interface has a mode of operation where the keyboard focus indicator is visible.
Can You Add the Border to the Image File Instead of the CSS?
Everything above is painted by the browser. If the border has to travel with the asset, into an email, a PDF, a marketplace listing, or anywhere your CSS does not run, it has to be part of the image file instead.
Cloudinary’s bo (border) transformation does that at delivery time. The transformation reference shows the syntax as bo_<width>_<style>_<color>. Only solid is supported for the style, and hex colors take an rgb: prefix:
https://res.cloudinary.com/demo/image/upload/bo_5px_solid_black/sample.jpghttps://res.cloudinary.com/demo/image/upload/w_400,h_400,c_fill,bo_15px_solid_white,r_max/sample.jpg It composes with the rest of the URL, and the documentation notes that the border follows any rounded corners applied with the r parameter.
Instead of using CSS, Cloudinary attaches the border to the media itself. bo_ works as an outer border, added around the image content. Requesting w_300,h_200,c_fill/sample.jpg from the demo cloud returns a 300 by 200 image, and adding bo_10px_solid_black to the same URL returns 320 by 220.
Create your free Cloudinary account and add borders straight from the URL.
Frequently Asked Questions
How do I put a border inside a div?
Use box-shadow: inset 0 0 0 4px blue; on the div. The shadow is painted inside the element and takes no part in layout, so the div keeps its size. If the div contains an image or a child that reaches its edges, add padding to the parent, or use outline: 4px solid blue; outline-offset: -4px; instead, since an outline is not clipped to the padding box.
What is the difference between an inner border and an outer border in CSS?
An outer border, which is what the border property gives you under the default content-box sizing, is added around the element and increases its rendered size. An inner border is painted within the space the element already occupies, so the rendered size does not change. The visual difference is a few pixels. The layout difference is that an outer border added on hover shifts everything around it.
Can I add an inner border to an image?
Yes, with outline and a negative outline-offset on the img element. Prefer it over an inset shadow here, because MDN notes that inset shadows are painted below the content, and the image itself is the content, so an inset shadow on an img is hidden by the image. A wrapper element with padding and an inset shadow also works if you want the line to sit outside the image pixels.
Why is my CSS border not showing at all?
Most often because border-style is missing. A border with a width and a color but no style is not drawn. Check also that the element has a height, since an empty block has nothing to draw a border around, and that an inset box-shadow is not sitting behind an image that reaches the edges, which MDN documents will paint over it.
