← Back toCSS Layout

Frame corners

Written byPhuoc Nguyen
Category
Display
Created
06 Sep, 2023
The frame corner layout pattern is a popular and effective technique in web design. It uses corner elements to frame content on a web page, adding depth and structure. This technique is often used with images and shapes.
In this post, we'll teach you how to create frame corners with CSS. Are you ready to dive in and learn?

Markup

The first step in preparing the layout is to structure it properly. This usually involves two elements: an inner element for displaying the content and an outer element that will display four frames at its corners.
Here's an example of what the layout could look like:
<div class="box">
<div class="box__inner"></div>
</div>

Adding overlays

Creating a frame at the corners is a simple idea. We can use CSS to add overlay elements around the content area and position them using absolute positioning.
Here's how to get started: define some CSS variables that can be reused later.
:root {
--frame-border-size: 0.25rem;
--frame-height: 1rem;
--frame-width: 1rem;
}
The `--frame-border-size` variable controls the thickness of the frame, while `--frame-height` and `--frame-width` determine its dimensions. To create a single frame for the outer element, we can use a solid border and adjust the `padding` property to set the space between the border and the box content.
.box {
border: var(--frame-border-size) solid rgb(30 41 59);
padding: var(--frame-height) var(--frame-width);
}
In order to hide the top and bottom borders of the box, there's a nifty trick we can use called the pseudo-element. Here's how it works:
We'll use the `::before` pseudo-element to create a vertical overlay that sits outside the box. To achieve this, we position the element absolutely within the box. We set the `position` property to both elements to make sure it stays in place.
.box {
position: relative;
}
.box::before {
content: '';
position: absolute;
}
To create the overlay effect, we need to set the `left` and `right` properties to be the same as the frame's width, and the `top` and `bottom` properties to be the same the thickness of the frame border. By using negative numbers for these properties, we push the overlay to the outside of the box, causing the top and bottom borders to overlap.
.box::before {
left: var(--frame-width);
right: var(--frame-width);

top: calc(-1 * var(--frame-border-size));
bottom: calc(-1 * var(--frame-border-size));
}
Finally, to create the blank areas at the top and bottom, we make the top and bottom borders the same height as the frame. And voila! The top and bottom borders are hidden, and our box looks sleek and polished.
.box::before {
border-top: var(--frame-height) solid #fff;
border-bottom: var(--frame-height) solid #fff;
}
We can use the same approach and create the horizontal overlay element with the `::after` pesudo-element. Now, let's check out the final result of the steps we've followed together so far.

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn