Explain Codes LogoExplain Codes Logo

Outline radius?

web-development
responsive-design
best-practices
css
Nikita BarsukovbyNikita Barsukov·Sep 17, 2024
TLDR

Using the box-shadow property in tandem with border-radius lets you simulate a rounded outline for an element, as no outline-radius property exists currently. The snippet below shows it in action:

.Element { border-radius: 15px; box-shadow: 0 0 0 2px #000; /* Using box-shadow as an imitation of outline */ }

This ensures your element not only has a rounded outline but also maintains the box model's essence, preserving layout harmony.

Leveling up the effect

Spice up with transitions

For smoother changes in box-shadow (like hovering or focusing), inject the transition property. It's like sliding on ice, but without the cold!

.element { transition: box-shadow 0.3s ease; } .element:hover { box-shadow: 0 0 0 3px #000; /* Boom! Your shadow just grew! */ }

Interacting with inputs

Leverage input:focus on interactive input fields to highlight the shadow effect when focused, just like turning on high beams in fog:

input:focus { box-shadow: 0 0 0 3px #000; /* Your input field is now 'in focus' pun intended ;) */ }

The power of pseudo-elements

Creating an extra layer for the rounded outline using the :after pseudo-element lets you twist more knobs for complex styling:

.element { position: relative; } .element:after { content: ''; position: absolute; top: -2px; right: -2px; bottom: -2px; left: -2px; /* We're technically outside but identically positioned */ border: 2px solid #000; border-radius: 17px; /* Essentially border-radius of .element plus border width */ pointer-events: none; /* Don't interfere, you're just for show! */ }

Transparency and high-contrast

Improve accessibility in high contrast modes by tweaking your box-shadow's transparency, like some action in a spy movie:

.element { box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.7); /* Transparent enough to see, but not ignore! */ }

Unique flair with gradients and offsets

Impress with uniqueness by adding gradient or offset to the box-shadow, like dressing up for a fancy event:

.element { background-image: linear-gradient(to bottom, #fff, #eee); box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.4), -1px -1px 5px rgba(0, 0, 0, 0.4); /* Double shadow, double style! */ }

Dive deeper

Cross-browser compatibility

Ensure a consistent visual experience across browsers by employing the necessary vendor prefixes:

.element { -moz-box-shadow: 0 0 0 2px #000; /* Firefox NPCs */ -webkit-box-shadow: 0 0 0 2px #000; /* Safari and antique-Chrome NPCs */ box-shadow: 0 0 0 2px #000; /* Everyone else */ }

Overcoming complexities

Avoid the headaches of unnecessarily complex solutions like nested DIVs or using JS libraries like jQuery just for styling purposes.

The future looks bright

Stay alert for changes in WebKit and upcoming features like outline-style: auto that could bring more dynamic native control over rounded outlines.

Evaluating and perfecting

Make it a habit to test your designs across a plethora of browsers and devices. This ensures that your rounded corner effect remains consistent and maintains its appeal across all platforms.