How to make CSS border count inside element dimensions instead of adding extra space?

Hi everyone!

I’m working on a navigation menu and running into an issue with CSS borders. I created a menu item that should be exactly 40px tall. The height comes from padding rather than a fixed height value.

The problem is when I add a 1px border around it, the total height becomes 42px instead of 40px. The border adds extra space on top and bottom.

Is there a CSS property or method to make the border sit inside the element’s dimensions? I want the border to be part of the 40px height, not additional to it. I can’t just reduce the padding because I need it for other layout requirements.

Basically I’m looking for something similar to how design tools handle borders - where they can be set to appear inside the frame rather than outside it.

Use box-sizing: border-box on your element. This makes the browser include padding and borders inside your specified dimensions instead of adding them on top. Add it to your nav menu items and that 1px border will fit inside the 40px height rather than making it 42px. Had the exact same problem building responsive layouts last year - this fixed it instantly. The default content-box adds borders and padding to your content size, but border-box keeps everything within the dimensions you set.

To ensure your nav menu maintains the desired height of 40px with a border, apply box-sizing: border-box to your element. This property allows the height to encompass the padding and border without extending beyond your specified dimensions. I faced similar challenges with another project, and using this setting was a game-changer for preventing layout overflow. For broader consistency across your project, consider including it globally with * { box-sizing: border-box; }.

The box-sizing approach works great for simple stuff, but real projects get messy fast. You’ll hit different border behaviors across components and breakpoints.

I ran into this building complex nav systems. Managing border dimensions manually becomes a total nightmare with dozens of components having different states (hover, active, disabled) plus responsive needs.

So I automated the whole CSS generation process. Set up workflows that auto-calculate the right box-sizing properties, handle edge cases, and generate responsive variants from design tokens.

This saved me tons of manual CSS tweaking and killed those annoying “why is this 2px off” moments. The automation covers everything - basic border-box calculations to complex nested components.

Latenode makes CSS automation super straightforward. Build workflows that process your design requirements and spit out perfect styles without touching anything manually.