Border images and opacity
Definition and purpose
Border images paint a sliced image around the border area. Opacity controls the composite of an element and its descendants; alpha on an individual color affects only that paint.
Gradient border image
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { border: 6px solid navy; border-image: linear-gradient(red, blue) 1; padding: 10px; } </style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>A gradient paints around the edges while the center remains ordinary content.
Syntax and mental model
border: 6px solid navy; border-image: linear-gradient(red, blue) 1; provides a fallback stroke, then an image with slice value 1. Slice defines image regions, width sets painted thickness and repeat controls fitting. Border images are not clipped by border-radius in the same way as ordinary borders. opacity: .5 fades the whole group.
Paint alpha versus group opacity
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { background-color: rgba(0,0,255,.2); color: black; } #b { background-color: blue; color: white; opacity: .5; } </style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>A leaves text opaque; B fades the entire element group.
Common mistakes
Do not fade essential text just to soften a background; use background alpha. Opacity zero does not remove layout or necessarily disable interaction. Keep a conventional border fallback.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Outlines and shadows. Next lesson: Block and inline layout.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Border images and opacity — knowledge check
Five questions on border images and opacity.
Which property controls image slicing?
Why specify a conventional border first?
What does opacity .5 affect?
Does opacity zero remove layout space?
Which is better for a translucent background with opaque text?