Menu

How to Add a Border in CSS Without Changing Element Size
📰

How to Add a Border in CSS Without Changing Element Size

DEV Community: css·kandz·20 days ago
#uiO4ajSv
#devtocss#devto#dev#shadow#borders
Reading 0:00
15s threshold

Using CSS border is the standard way to outline an element. But it has a side effect that breaks layouts: borders add to the element's total width and height.

If you have a 100px wide div and add a 5px border, it suddenly becomes 110px wide. In a tight grid or flexbox layout, this causes elements to overflow or wrap unexpectedly.

You can fix this globally by setting box-sizing: border-box , but sometimes you need a border that doesn't take up any space at all, or you want multiple borders on a single element.

The solution is to use box-shadow .

The Problem with Borders

Here is a standard box:

.box { width : 100px ; height : 100px ; border : 2px solid blue ; }

Enter fullscreen mode

Exit fullscreen mode

Total width: 104px.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.