Login
You're viewing the front-end.social public feed.
  • Chris Coyierchriscoyier
    Aug 23, 2024, 3:33 PM

    Someone told me you can't apply different backgrounds to the content/padding/border/margin areas of a box, and I was like... I think you... can actually.

    (e.g. multiple backgrounds with different background-clip values)

    I ended up making this weird box model machine thingy. codepen.io/chriscoyier/pen/gON

    Image attached toot
    💬 3🔄 13⭐ 41

Replies

  • 💬 1🔄 0⭐ 0
  • Aug 23, 2024, 3:57 PM

    @polotek @chriscoyier - `background-clip` defines which box boundary (border, padding or content) a background *image* is painted within; it doesn't affect background *colors*.
    - `background-image` is used to paint solid colors using linear gradients, which are technically background images.
    - each one has three comma-separated values, which can be interpreted as 3 separate paint instructions, each with its own `background-image` and `background-clip` value.

    💬 1🔄 0⭐ 0
  • Aug 23, 2024, 4:06 PM

    @polotek @chriscoyier So it works as 3 instructions:

    1. Paint a blue-to-blue gradient image, and clip it at the box's content boundary.
    2. Paint a green-to-green gradient image, and clip it at the box's padding boundary.
    3. Paint a yellow-to-yellow gradient image, and clip it at the box's border boundary.

    Note that each layer is painted *behind* the previous layer. The spec has a fairly readable section on layering multiple background images:
    drafts.csswg.org/css-backgroun

    💬 1🔄 1⭐ 0
  • 💬 0🔄 0⭐ 0
  • Aug 23, 2024, 3:49 PM

    @chriscoyier My students will just be happy to have an interactive box model to understand what padding, border, and margin are in a delightfully visual way 💖

    💬 0🔄 0⭐ 0
  • Aug 23, 2024, 4:01 PM

    @chriscoyier This is really great, those `background-clip` values are very underrated.

    You could also fake a color on the margin area with outline also, though border-radius muddies it up a little:
    `outline: var(--margin) solid lightsalmon;`

    A div with different colored background colors to distinguish the box model.
    💬 1🔄 0⭐ 0
  • Chris Coyierchriscoyier
    Aug 23, 2024, 4:30 PM

    @walpolea ooo yeah that outline idea is cleaner than the weird grid thing I did

    💬 1🔄 0⭐ 1
  • 💬 1🔄 3⭐ 2
  • 💬 0🔄 0⭐ 2