On this page10 sections
Lecture 033: Border Images & Advanced Backgrounds
| Category | 7: Advanced Styling |
| Lecture | 033 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Borders don't have to be plain lines:
border-imagecan draw a frame from a picture or a gradient. Backgrounds can stack several images, stay fixed while the page scrolls, and be clipped to exactly the area you choose, even to the text itself.
Learning Objectives
- Draw borders from images and gradients with
border-image - Layer several backgrounds
- Control
background-size,background-originandbackground-clip - Make parallax-style backgrounds with
background-attachment: fixed
1. border-image
border-image: source slice / width repeat;
Example: a gradient border:
<style>
.grad { border: 8px solid; border-image: linear-gradient(45deg, #f76707, #ae3ec9) 1; padding: 14px; margin-bottom: 14px; }
.stripes { border: 12px solid; border-image: repeating-linear-gradient(45deg, #ffd43b 0 10px, #212529 10px 20px) 12; padding: 14px; }
</style>
<div class="grad">A border made from a gradient.</div>
<div class="stripes">A striped "caution tape" border.</div>With a real image file:
.frame { border: 20px solid transparent; border-image: url("frame.png") 30 round; }The slice value (30) says how many pixels from each edge of the image are used for the corners; round / repeat / stretch control how the edges fill.
border-image ignores border-radius. For a rounded gradient border, use the background trick in section 4.
2. Multiple Backgrounds
List several, separated by commas. The first is drawn on top.
Example
<div style="height: 160px; padding: 14px; color: white;
background:
url('https://picsum.photos/id/1027/60/60') no-repeat right 12px bottom 12px,
linear-gradient(135deg, #364fc7, #12b886);">
A small photo in the corner, over a gradient.
</div>3. Background Size
Example
<style>
.b { height: 90px; margin-bottom: 8px; border: 1px solid #ced4da; background: url('https://picsum.photos/id/237/300/200') no-repeat center; }
</style>
<div class="b" style="background-size: auto;">auto: the image's real size</div>
<div class="b" style="background-size: 100px 60px;">100px 60px: an exact size</div>
<div class="b" style="background-size: contain;">contain: the whole image fits</div>
<div class="b" style="background-size: cover;">cover: fills the box, cropping if needed</div>4. Origin and Clip
background-origin: where the image's position starts (padding-box,border-box,content-box).background-clip: how far the background is painted.
Example
<style>
.c { border: 10px dashed rgb(0 0 0 / 0.3); padding: 16px; margin-bottom: 8px; background: #ffd43b; }
</style>
<div class="c" style="background-clip: border-box;">border-box: under the border too</div>
<div class="c" style="background-clip: padding-box;">padding-box: stops at the border</div>
<div class="c" style="background-clip: content-box;">content-box: only behind the content</div>Rounded gradient borders, using two backgrounds and two clips:
<div style="padding: 16px; border: 4px solid transparent; border-radius: 14px;
background: linear-gradient(white, white) padding-box, linear-gradient(90deg, #f76707, #ae3ec9) border-box;">
A gradient border with rounded corners.
</div>Gradient text with background-clip: text:
<h1 style="background: linear-gradient(90deg, #1971c2, #e64980); background-clip: text; -webkit-background-clip: text; color: transparent;">
Gradient text
</h1>5. Fixed Backgrounds (Parallax)
Example: scroll inside the preview:
<style>
.parallax { height: 180px; background: url('https://picsum.photos/id/1018/800/600') center / cover fixed; }
.text { padding: 40px 16px; }
</style>
<div class="parallax"></div>
<div class="text">The photo above stays still while the page scrolls, so the content seems to slide over it.</div>
<div class="parallax"></div>
<div class="text">Some phone browsers ignore background-attachment: fixed and scroll it normally.</div>Common Beginner Mistakes
1. border-image with border-radius. Corners stay square. Use the padding-box/border-box trick.
2. The wrong layer order. The first background in the list is on top.
3. background-clip: text without transparent text. The text colour covers the gradient.
Practice Questions
- Make an 8px border from a red-to-blue gradient.
- With two backgrounds listed, which one is drawn on top?
- What's the difference between
coverandcontain? - Which value of
background-clippaints only behind the content? - How do you make text show a gradient?
- What does
background-attachment: fixeddo?
Practice Answers
border: 8px solid; border-image: linear-gradient(red, blue) 1;- The first one.
coverfills the box and may crop;containshows the whole image and may leave gaps.content-box- A gradient background,
background-clip: text(plus-webkit-background-clip: text) andcolor: transparent. - The background stays fixed to the window while the page scrolls.
Key Takeaways
border-imagemakes borders from images or gradients (no rounded corners).- Several backgrounds can be layered; the first is on top.
background-size,-originand-clipfine-tune the painting area.background-clip: textmakes gradient text;fixedgives a parallax feel.