Placing border inside of div and not on its edge
Insert your border right inside the <div>
using box-sizing: border-box;
. This CSS property includes padding and border within the width and height, instead of adding to it.
Apply this style to your <div>
, to get the internal border style:
The 100px
block takes the border also into account, maintaining the specified dimensions.
Browser back-compatibility
To take care of those who are still clinging onto their old browsers:
These browser-specific properties ensure compatibility with older browser versions including IE8+.
Inner-glow borders with box-shadow
Sure, you can use the force, but here's how you use box-shadow
for inner borders and that soft glowy effect:
Just a heads up, while box-shadow
has a wide scoreboard, make sure to check for specific properties on resources like Can I use.
Crafty outlines for non-disruptive borders
Use outline
to sneak in borders without affecting the box size or layout:
Use negative values for outline-offset
to relocate your outline inside the <div>
.
Case-by-case scenarios
Case: Multiple inner borders
Watch the magic happen. Create multiple borders by comma-separating shadow values:
Case: Hover effects
A little interaction never hurts. Hover around:
Case: Color & spread manipulation
You can adjust the spread and add that nuance with RGBA colors:
Advanced cases
3D effect
With inset box-shadows, simulate a 3D effect. Try this:
Decorative double borders
Elegance is key. Use box-shadow
for double borders:
Animated border transitions
Bored of static borders? Let's animate:
Was this article helpful?