On this page10 sections
  1. Learning Objectives
  2. 1. border-image
  3. 2. Multiple Backgrounds
  4. 3. Background Size
  5. 4. Origin and Clip
  6. 5. Fixed Backgrounds (Parallax)
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 033: Border Images & Advanced Backgrounds

Category7: Advanced Styling
Lecture033 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

Borders don't have to be plain lines: border-image can 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-origin and background-clip
  • Make parallax-style backgrounds with background-attachment: fixed

1. border-image

border-image: source slice / width repeat;

Example: a gradient border:

html
<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:

css
.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

html
<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

html
<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

html
<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:

html
<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:

html
<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:

html
<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

  1. Make an 8px border from a red-to-blue gradient.
  2. With two backgrounds listed, which one is drawn on top?
  3. What's the difference between cover and contain?
  4. Which value of background-clip paints only behind the content?
  5. How do you make text show a gradient?
  6. What does background-attachment: fixed do?

Practice Answers

  1. border: 8px solid; border-image: linear-gradient(red, blue) 1;
  2. The first one.
  3. cover fills the box and may crop; contain shows the whole image and may leave gaps.
  4. content-box
  5. A gradient background, background-clip: text (plus -webkit-background-clip: text) and color: transparent.
  6. The background stays fixed to the window while the page scrolls.

Key Takeaways

  • border-image makes borders from images or gradients (no rounded corners).
  • Several backgrounds can be layered; the first is on top.
  • background-size, -origin and -clip fine-tune the painting area.
  • background-clip: text makes gradient text; fixed gives a parallax feel.