惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Building a Football Ticket With CSS and SVG
Ahmad Shadeed · 2019-11-18 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

While working on a design project, I needed to build a football ticket with cut-out edges on the right and left sides. Initially, I thought that it would be simple and won’t take much time. It turned out to be an exciting challenge that I’ve decided to document and share with you.

In this article, I will explore all the various ways to build this card.

Clip Path

I thought about using clip-path since it’s the most obvious solution for that kind of thing. In Sketch, I made a black shape with cut-out edges as in the figure. The goal now is to get that as an SVG and try to use it with Clip Path.

<div class="ticket"></div>

<svg xmlns="http://www.w3.org/2000/svg">
  <clipPath id="my-clip-path" clipPathUnits="objectBoundingBox">
    <path d="..."></path>
  </clipPath>
</svg>
.ticket {
  /* Other Styles */
  clip-path: url("#my-clip-path");
}

That was weird! It’s kind of like the image and the clipped paths are shifted to the far right. I made the below GIF to clarify what I mean:

The reason behind that bug is that the exported path values are absolute. That means that they only work if I made the size as per the Sketch document, which is not practical in real-life projects. To fix that, I need to clean the SVG with SVGOMG tool. With that done, the path values should be converted from absolute to relative using this tool.

Final SVG after cleaning it and converting the path values to relative

<svg width="352" height="173" xmlns="http://www.w3.org/2000/svg">
  <path
    d="M1,0 l0,0.429 c-0.015,0.007,-0.026,0.033,-0.026,0.065 s0.011,0.058,0.026,0.065 V1 H0 V0.559 c0.015,-0.007,0.026,-0.033,0.026,-0.065 S0.015,0.436,0,0.429 V0 h1"
    fill="#000"
    fill-rule="nonzero"
  />
</svg>

I used url(#my-clip-path) to call the inline SVG. It works in Chrome, Safari, but not Firefox.

See the Pen Football Ticket - Take 1 by Ahmad Shadeed (@shadeed) on CodePen.

And here we go with the journey of finding a solution that works across all these browsers.

At first, I thought that Firefox is not showing it as expected due to security reasons. However, it turned out to be weird. When I remove box-shadow from the element, it works!

See the Pen Football Ticket - Take 2 by Ahmad Shadeed (@shadeed) on CodePen.

Instead of using CSS Box Shadow for the filter, I will use a CSS filter for that purpose. I added the following, and it didn’t work:

.ticket {
  filter: drop-shadow(0 3px 10px #000);
}

After some research, I got that this should be applied to the parent element instead, so I went and added a parent with the shadow filter applied to it.

It’s possible to add it to the body! But this is not good for sure, even for testing purposes. I wrapped the ticket in an element and added the following:

.ticket-wrapper {
  filter: drop-shadow(0 3px 10px #000);
}

Here is the result. Notice how the shadow is following the clipped shape nicely.

See the Pen Football Ticket - Take 3 by Ahmad Shadeed (@shadeed) on CodePen.

Stretching Cut-out Circles on Resize

I wanted to test what would happen if the ticket size becomes bigger. I noticed something weird.

The half-circles at the left and right are stretched weirdly, which is not the desired result. I want the clipPath to be dynamic and fluid as its content.

I tried a lot to draw custom paths and did research about that. I learned how to manually draw an SVG path by using the arc path command.

<svg>
  <clipPath id="my-clip-path" clipPathUnits="userSpaceOnUse">
    <path
      d="M 0 0 H 352 V 75 a 1 1 0 0 0 0 25 V 173 H 0 V 95 a 1 1 0 0 0 0 -25 V 0"
    ></path>
  </clipPath>
</svg>

The above only works when the ticket size is fixed since I entered the width 352 and height 173 as fixed values in the path. When the size is fluid, the clipPath doesn’t expand with the ticket width. To fix that, the path values should be converted from absolute to relative again using this tool.

Before

<svg>
  <clipPath id="my-clip-path" clipPathUnits="userSpaceOnUse">
    <path
      d="M 0 0 H 352 V 75 a 1 1 0 0 0 0 25 V 173 H 0 V 95 a 1 1 0 0 0 0 -25 V 0"
    ></path>
  </clipPath>
</svg>

After

<svg>
  <clipPath id="my-clip-path" clipPathUnits="objectBoundingBox">
    <path
      d="M0,0 H1 V0.434 a0.003,0.006,0,0,0,0,0.145 V1 H0 V0.549 a0.003,0.006,0,0,0,0,-0.145 V0"
    ></path>
  </clipPath>
</svg>

Since the issue is related to resizing, I thought about having two paths. Each path will be used based on the ticket size.

Here is how the SVG looks after converting the above path values to relative. I saw the need for another path so I added a third one.

<svg>
  <clipPath id="my-clip-path" clipPathUnits="objectBoundingBox">
    <path
      class="path-big"
      d="M0,0 H1 V0.434 a0.001,0.006,0,0,0,0,0.145 V1 H0 V0.549 a0.001,0.006,0,0,0,0,-0.145 V0"
    ></path>
    <path
      class="path-middle"
      d="M0,0 H1 V0.434 a0.002,0.006,0,0,0,0,0.145 V1 H0 V0.549 a0.002,0.006,0,0,0,0,-0.145 V0"
    ></path>
    <path
      class="path-small"
      d="M0,0 H1 V0.434 a0.003,0.006,0,0,0,0,0.145 V1 H0 V0.549 a0.003,0.006,0,0,0,0,-0.145 V0"
    ></path>
  </clipPath>
</svg>
/*The middle and big paths are hidden by default*/
.path-big,
.path-middle {
  display: none;
}

/*Show the middle path and hide the small*/
@media (min-width: 550px) {
  .path-middle {
    display: block;
  }

  .path-small {
    display: none;
  }
}

/*Show the big path and hide the small and middle*/
@media (min-width: 750px) {
  .path-big {
    display: block;
  }

  .path-middle {
    display: none;
  }
}

It works, but there is another issue. I’m showing and hiding the paths based on the screen size, not the ticket or its container. That means, if the ticket were placed in a grid of three columns, the big path will be shown! This is not the desired result.

See the Pen Football Ticket - Take 4 by Ahmad Shadeed (@shadeed) on CodePen.

ResizeObserver and SVG

Since the issue is in making the SVG path stretch as per the ticket width, I thought about using the ResizeObserver API. The idea is to get the ticket width and substitute that value in the d attribute of the SVG path.

<svg>
  <clipPath id="my-clip-path" clipPathUnits="userSpaceOnUse">
    <path
      class="ticketPath"
      d="M 0 0 H 352 V 75 a 1 1 0 0 0 0 25 V 173 H 0 V 95 a 1 1 0 0 0 0 -25 V 0"
    ></path>
  </clipPath>
</svg>

<div class="ticket-wrapper">
  <div class="ticket"></div>
</div>
let ticketPath = document.querySelector(".ticketPath")

const ro = new ResizeObserver((entries) => {
  for (let entry of entries) {
    let path = `M 0 0 H ${entry.contentRect.width} V 75 a 1 1 0 0 0 0 25 V 173 H 0 V 95 a 1 1 0 0 0 0 -25 V 0`
    ticketPath.setAttribute("d", path)
  }
})

ro.observe(document.querySelector(".ticket"))

Now when the ticket is resized, the path will be modified based on its width. ResizeObserver is powerful!

See the Pen Football Ticket - Resize Observer by Ahmad Shadeed (@shadeed) on CodePen.

CSS Masks and Radial Gradients

I thought about combining CSS Masks and Radial Gradients. Let’s explore that. I want to create a gradient with a transparent circle, and the rest will be solid.

.elem {
  background: radial-gradient(
    circle at left center,
    transparent 10px,
    black 11px,
    black 55%,
    transparent 0
  );
}

The result will be as the following:

For our case, I need to have two transparent cut-out circles, so I will duplicate the gradient and make them two.

.elem {
  background: radial-gradient(
      circle at left center,
      transparent 10px,
      black 11px,
      black 55%,
      transparent 0
    ), radial-gradient(circle at right center, transparent 10px, black
        11px, black 55%, transparent 0);
}

Below is a GIF that explains how the above works.

Now that the gradient is explained let’s use that as a mask image.

.ticket {
  /* Other styles */
  mask-image: radial-gradient(
      circle at left center,
      transparent 10px,
      black 11px,
      black 55%,
      transparent 0
    ), radial-gradient(circle at right center, transparent 10px, black
        11px, black 55%, transparent 0);
}

It works! By far, this solution is good for me. I can control the size of the cut-out circles, and they don’t stretch on resizing.

See the Pen Football Ticket - Take 8 by Ahmad Shadeed (@shadeed) on CodePen.

DOM/Pseudo Elements

The idea is to add two pseudo-elements; each one will be on either left or right sides. I added the following CSS:

.ticket {
  position: relative;
}

.ticket:after,
.ticket:before {
  content: "";
  position: absolute;
  top: calc(50% - 10px);
  width: 20px;
  height: 20px;
  background: #f8f8f8;
  border-radius: 50%;
}

.ticket:before {
  left: -10px;
}

.ticket:after {
  right: -10px;
}

.ticket:before,
.ticket:after {
  background: #000;
}

Now, I will hide the outer half of each circle by adding overflow: hidden to the ticket element.

.ticket {
  /* Other styles */
  overflow: hidden;
}

The one downside for this technique is that it’s doesn’t work when the background color change.

See the Pen Football Ticket - Take 7 by Ahmad Shadeed (@shadeed) on CodePen.

Final Words

My favorite solutions are CSS Masks and ResizeObserver. That doesn’t mean that the rest are bad. I enjoyed learning and exploring all of the above techniques.

Thank you

  • Adebiyi for proof-reading the article
  • Amelia for confirming that it’s not possible to have fixed cut-out circles in an SVG path

The End

And that’s a wrap. Do you have a comment or a suggestion? Please feel free to ping me on @shadeed9.