Today we want to share with you a cool little CSS trick you can use to create a border overlay where the border is only visible on the edges. This can make some great looking effects, especially when you use it as a overlay for images.

The Problem

Okay so heres the problem: we want to create a border overlay where the border is not a straight line all the way through but it only visible on the corners. Like a camera view finder. Here is an example of what we will do:

Sure, we could simply draw the lines with Photoshop but then we could not simply use the image elsewhere and it wouldn’t be responsive. Therefore we need a more flexible solution. Also we don’t want to repeat the same steps over and over again in PS, right? So lets write some awesome CSS.

The Solution

The solution is to use pseudo elements in combination with the border property. Basically this trick works on every HTML element, which is at least inside one other element. If you have multiple elements on the same level, you might need to change the CSS slightly to achieve what you want. In our case, our HTML looks like this:

We want to put the edged border over the image but to make that work, we needed to put the image inside a p (any other container will do the trick as well) because the image itself has nothing inside it. The h2 would work as it is though.

The first thing we wanted to do, was to center the title over the image and also make the image fill the available space. We can easily achieve this using the following CSS:

Now to the tricky part, the corner borders. For this, we use the following CSS:

And thats already everything you need to do. We hope that cool little trick will help you create a cool looking overlay on your next projects. If you use it, we would love to hear from you in the comments. Cheers ✌️

Credits go to the person who came up with this solution in this Stack Overflow Post:

  • SO: https://stackoverflow.com/questions/14387690/css-show-only-corner-border
  • The corresponding fiddle: http://jsfiddle.net/V8jmR/