CSS Tutorial 13: Modern UI & Shapes

Lesson 12 Review

In our last lesson, we used mathematical functions like clamp() to make our typography smoothly scale on any screen. Today, we step away from layout math and focus on advanced User Experience (UX) features. We will learn how to build magnetic scrollbars and how to cut rectangular boxes into custom shapes.

Step 1: CSS Scroll Snapping (The "TikTok" Effect)

Have you ever noticed how mobile apps like TikTok, Instagram Reels, or Tinder don't let you scroll halfway through a post? When you swipe, the screen automatically "magnets" or "snaps" to perfectly align the next post. We can do this with pure CSS.

It requires a Parent-Child relationship (just like Flexbox and Grid):

  • Parent Container: scroll-snap-type: y mandatory; (Tells the browser to snap on the vertical Y-axis, and forces it to snap no matter what).
  • Child Element: scroll-snap-align: start; (Tells the browser to align the very top edge of the child to the top of the container).

The HTML & CSS:

<!-- HTML -->
<div class="scroll-container">
  <section class="panel bg-blue">Panel 1</section>
  <section class="panel bg-red">Panel 2</section>
  <section class="panel bg-green">Panel 3</section>
</div>

/* CSS */
.scroll-container {
  height: 100vh; /* Viewport Height */
  overflow-y: scroll; /* Allows vertical scrolling */
  scroll-snap-type: y mandatory; /* Activates the magnet effect */
}

.panel {
  height: 100vh; /* Each panel takes up the whole screen */
  scroll-snap-align: start; /* Snaps the top edge to the top of the screen */
}

The Output (Try scrolling slowly inside the box below!):

Slide 1 (Scroll Down)
Slide 2 (It snaps!)
Slide 3

Step 2: CSS Clip-Path (Breaking the Box)

In Lesson 2, we learned that every HTML element is a rectangle (The Box Model). But what if you want a triangle, a circle, or a slanted diagonal header? You use clip-path.

Imagine clip-path as a pair of virtual scissors. It cuts away parts of your rectangular box to reveal a custom shape. The areas that are cut away become completely invisible and un-clickable.

  • clip-path: circle(50%); - Cuts the rectangle into a perfect circle.
  • clip-path: polygon(x y, x y, x y); - Cuts the box using exact X/Y coordinate points.

The HTML & CSS: Creating a Triangle

.triangle-box {
  width: 200px;
  height: 200px;
  background-color: #e74c3c;
  /* Point 1 (Top Center), Point 2 (Bottom Right), Point 3 (Bottom Left) */
  clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}

The Output:

Triangle
Developer Hack: No developer memorizes polygon coordinates. We use a free website called CSS Clip-Path Maker (bennettfeely.com). You drag and drop points visually, copy the code, and paste it into your CSS file!

Step 3: Mix-Blend-Mode (Photoshop in the Browser)

If you have ever used Photoshop, you know about layer blending (Multiply, Screen, Overlay). CSS allows you to blend an HTML element directly with whatever background is behind it using mix-blend-mode.

A highly popular design trick is the Difference Blending Effect. If you put white text over a split background (half black, half white), the text will magically invert its color depending on what it is touching.

The HTML & CSS: Difference Blending

.split-background {
  background: linear-gradient(to right, black 50%, white 50%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.blended-text {
  color: white;
  font-size: 3rem;
  font-weight: bold;
  mix-blend-mode: difference; /* The magic line! */
}

The Output:

CONTRAST TEXT

Final Quiz: Test Your Advanced CSS Knowledge

Click the buttons below to verify your logic.

1. To make a scrolling section automatically stop at exactly the top of the next panel, which two properties must work together?
2. If you use `clip-path` to cut a square HTML `<div>` into the shape of a circle, what happens to the corners that were cut off?
3. What does `mix-blend-mode: difference;` do when applied to white text that is hovering over a solid white background?

Test your design skills in the interactive Shape & Blending Playground below!

Post a Comment

0 Comments