CSS/styling
Borders & Effects
Borders define an element's visible edge, while visual effects like shadows and object-fit shape how content and media appear inside their containers.
4 min readcss#css#borders#effects
Introduction
Borders define an element's visible edge, while visual effects like shadows and object-fit shape how content and media appear inside their containers.
Subtopics
bordershorthand and longhand (border-width,border-style,border-color)border-radius(rounded corners)box-shadowtext-shadowobject-fit/object-position(media sizing)filter(blur, brightness, etc.)opacity
Syntax
css
.bordered {
border: 2px solid #333;
border-radius: 8px;
}
.custom-sides {
border-top: 1px solid red;
border-bottom: 3px dashed blue;
}
.rounded-circle {
border-radius: 50%;
}
.shadowed {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.text-shadowed {
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}
.fitted-image {
width: 300px;
height: 200px;
object-fit: cover;
object-position: center;
}
.blurred {
filter: blur(4px) brightness(0.9);
}
.faded {
opacity: 0.6;
}
border— shorthand forwidth style color(e.g.,2px solid black); astylevalue (solid,dashed,dotted) is required for the border to render.border-radius— rounds corners;50%on an equally sized square element creates a circle.box-shadow: x y blur spread color— adds a drop shadow outside (orinsetfor inside) the box.text-shadow: x y blur color— adds a shadow behind text.object-fit— controls how replaced content (img/video) fills its box:cover(crop to fill),contain(fit without cropping),fill(stretch).object-position— adjusts the focal point of the fitted content.filter— applies graphical effects likeblur(),brightness(),grayscale(),contrast().opacity— controls overall transparency (0 = invisible, 1 = fully opaque); affects the whole element including children.
Important Properties
| Property | Purpose | Syntax | Example |
|---|---|---|---|
border | Edge line shorthand | border: w style color; | border: 1px solid #000; |
border-radius | Rounded corners | border-radius: value; | border-radius: 12px; |
box-shadow | Drop shadow on box | box-shadow: x y blur color; | box-shadow: 0 2px 8px #0003; |
text-shadow | Shadow on text | text-shadow: x y blur color; | text-shadow: 1px 1px #000; |
object-fit | Media scaling behavior | object-fit: value; | object-fit: cover; |
filter | Graphical effects | filter: fn(); | filter: grayscale(100%); |
opacity | Transparency | opacity: value; | opacity: 0.5; |
Common Use Cases
- Beginner: a rounded card with
border-radius: 8pxand a subtlebox-shadow. - Practical UI: a profile avatar image using
border-radius: 50%combined withobject-fit: coverso non-square photos still fill a circle correctly. - Real-world: an image gallery grid where every
<img>usesobject-fit: coverto maintain uniform card heights regardless of original image aspect ratio, with a hoverfilter: brightness(1.1)transition.
Common Errors
- ❌
border: 2px #000;(missing style) → ✅border: 2px solid #000;— a border style keyword is required or the border won't render. - ❌
border-radius: 50%;on a non-square element expecting a perfect circle → ✅ ensure equalwidthandheightfirst, then applyborder-radius: 50%;. - ❌
<img>distorted/stretched inside a fixed-size box → ✅ addobject-fit: cover;(orcontain) so the image scales properly without distortion. - ❌
box-shadow: black 2px 2px;with color placed first (order-sensitive in some legacy expectations) → ✅ modern syntax is flexible but the safest order isoffset-x offset-y blur spread color, e.g.box-shadow: 2px 2px 4px black;. - ❌ Using
opacity: 0.5;on a parent expecting only the background to fade, but child text fades too → ✅ usergba()/hsla()on the specificbackground-colorinstead ofopacityon the whole element if only the background should be transparent.
Common Mistakes
- Forgetting
border-style, causing an invisible border. - Assuming
opacityonly affects background, not entire element + children. - Not setting
object-fiton images in fixed-aspect containers, causing distortion. - Overusing heavy
filter/box-shadoweffects, hurting rendering performance.
Quick Reference
css
.card {
border: 1px solid #ddd;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}
img { object-fit: cover; }
.avatar { border-radius: 50%; }
LB
Author
Lakshya Bansal