Skip to content

RESOURCES / BLOG

How can developers create custom borders for images using CSS?

CSS featured image

You have a grid of product shots or profile avatars and you want them to pop with decorative frames, gradient strokes, or playful patterns. The community often asks for a reliable way to do this that scales across devices and is easy to maintain. 

Hi folks,
I need to style image cards with more than plain 1px borders. I am looking for approaches that let me add rounded, multi-layer, gradient, or even image-based frames that scale responsively. How can developers create custom borders for images using CSS? I am also curious about pros and cons of different techniques and how to keep everything sharp on high DPI screens. Any code examples and pitfalls to avoid would be great.

Thanks!

There are several solid approaches, each with different tradeoffs in flexibility, visual fidelity, and complexity. Below are the most common techniques plus examples you can adapt quickly.

img.framed {
  display: block;          /* remove inline gaps */
  border: 8px solid #222/* thickness + color */
  border-radius: 16px;     /* rounded corners */
}Code language: CSS (css)

This is the simplest solution and supports rounded corners, but not textures or patterns.

Wrap the image with a container that paints a gradient, then reveal it as the border.

<div class="gradient-border">
  <img src="photo.jpg" alt="Decorated">
</div>

.gradient-border {
  display: inline-block;
  padding: 6px; /* border thickness */
  border-radius: 16px;
  background: linear-gradient(135deg, #7f5af0, #2cb67d);
}
.gradient-border img {
  display: block;
  border-radius: 12px;
}Code language: HTML, XML (xml)
img.double-border {
  display: block;
  border-radius: 12px;
  box-shadow:
    0 0 0 6px #fff,   /* inner border */
    0 0 0 12px #111/* outer border */
}Code language: CSS (css)

Useful for layered frames without extra wrappers. Each spread simulates a border ring.

Use a PNG or SVG as a 9-slice border that wraps around the image. This is perfect for decorative corners.

img.frame {
  border: 24px solid transparent;      /* reserve space */
  border-image-source: url('corner-frame.svg');
  border-image-slice: 30 fill;         /* controls the 9-slice cut */
  border-image-width: 24px;
  border-image-repeat: round;          /* round to avoid stretching artifacts */
  border-radius: 12px;
  box-sizing: border-box;
}Code language: CSS (css)

Prefer SVG for crisp scaling across DPIs. If you are choosing between raster and vector, this guide helps: SVG vs PNG.

Paint complex frames using ::before or ::after so you don’t touch the image element itself.

<figure class="card">
  <img src="photo.jpg" alt="Card">
</figure>

.card {
  position: relative;
  display: inline-block;
  border-radius: 14px;
}
.card::before {
  content: "";
  position: absolute;
  top: -8px; right: -8px; bottom: -8px; left: -8px;
  border-radius: 18px;
  background:
    repeating-linear-gradient(45deg, #0ea5e9 0 10px, #22c55e 10px 20px);
  z-index: -1; /* sits behind the image */
}
.card img { display: block; border-radius: 12px; }Code language: HTML, XML (xml)

This approach is great for dynamic patterns and themes driven by CSS variables.

For badge-like or ornamental shapes, use CSS clip-path or mask-image. You can mask a gradient or pattern to show as the border area only. This is more advanced but powerful for brand motifs.

  • Prefer SVG for border assets to keep borders sharp at any size.
  • Use object-fit: cover and responsive widths to avoid distorted images inside frames.
  • Avoid heavy raster border PNGs. If you must use them, optimize aggressively.

After you pick a CSS technique, you can manage and transform your images in the delivery layer. For overlays and frames, Cloudinary can generate or apply borders at URL time, and you can still combine them with your CSS borders.

Solid border and rounded corners on delivery:

https://res.cloudinary.com/demo/image/upload/f_auto,q_auto,w_600,c_fill,g_auto,bo_12px_solid_rgb:2cb67d,r_16/sample.jpg

Add a frame overlay image that scales to the base:

https://res.cloudinary.com/demo/image/upload/w_600,c_fill,g_auto,f_auto,q_auto/l_my_frame.png,fl_relative,w_1.0/r_12/sample.jpg

This lets you standardize borders server-side and keep your CSS simpler while still using the same HTML element. Learn overlay concepts here: How to overlay a picture on a picture.

  • For border-image, ensure the slice matches the asset’s corner thickness or corners will stretch.
  • Set box-sizing: border-box when borders affect sizing within layout grids.
  • Test on high DPI devices. If using raster frames, export 2x or 3x variants or prefer SVG.
  • For simple rounded borders, use border and border-radius.
  • For gradients and multi-layer styles, use a wrapper with background-clip and box-shadow.
  • For decorative corners and textures, use border-image with SVG.
  • For complex art, use pseudo-elements or masks. Optionally offload framing to Cloudinary with URL transformations and overlays.

Ready to streamline image delivery and apply borders, overlays, and optimization on the fly? Sign up for a free Cloudinary account and start building faster.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free