search
HomeWeb Front-endHTML TutorialCSS3: clip-path detailed explanation_html/css_WEB-ITnose

One of my students, Heather Banks, wanted to achieve an effect he saw in Squarespace:

Based on her past experience, the HTML and CSS of this website were completely It was within her ability, so I helped her achieve this effect. Displaying the effect of a nav being cropped is a non-trivial task. My first reaction when I saw the image was to create an image that matches the partially cropped background, and then set it as an after element. The problem is, at least reactivity has to be addressed, and reactivity is not entirely controllable.

Understand the CSS properties: clip-path

clip-path is part of the working draft and is a tool for hiding parts of an element through masking and clipping. Although clip-path is not supported by mainstream browsers (including IE and Firefox), it is still a small tool for achieving stylish effects in webkit browsers.

Note that the -webkit- prefix is ​​required in modern browsers.

clip-path simply works by providing a series of X and Y values ​​to create a path. When using these values ​​to create a full path, the image will be cropped to the internal dimensions of the path.

Using clip-path, we can create different shapes such as circles, ellipses and polygons, creativity is the only limit.

A simple triangle clipping


View the code on codepen
Simply apply clip-path to the element to achieve the above effect:

.clipClass { -webkit-clip-path: polygon(0 100%, 50% 0, 100% 100%); }

Step-by-Step Analysis

Much like positioning attributes, we need to consider X and Y values. X:0 and Y:0 means starting from the upper left corner of the element and moving from the upper left corner. X:100% refers to the right side of the element, Y:100% refers to the bottom of the element.
For the path created above, the following points are actually created:

x: 0, y:100%x: 50%, y: 0x: 100%, y: 100%

This simple path starts from the lower left corner, moves 50% horizontally, reaches the top position, and then moves horizontally to 100 % position, return vertically downward to the bottom and reach the third coordinate point. The triangle comes out.

Shape

In the above example, we use polygon to create a shape and define a path through pairs of X and Y values ​​separated by commas (,). We can then create different graphs by taking different values.

Circle


View the code on codepen
In order to create a circle, you need to pass in three values ​​​​to circle: the coordinates of the center of the circle (X value and Y value) and the radius . When defining the radius of a circle, we can use the at keyword to define the center coordinates.

.clipClass { -webkit-clip-path: circle(50% at 50% 50%); }

Ellipse


View this code on codepen
Many times, you don't want a simple circle, but an ellipse.

In order to implement an ellipse, four values ​​need to be provided to ellipse: the x-axis radius of the ellipse, the y-axis radius, the x-coordinate and y-coordinate of the position of the ellipse, the latter two values ​​​​use the at keyword and the first two values ​​separately.

.clipClass { -webkit-clip-path: ellipse(30% 20% at 50% 50%); }

Illustration

(bug in old version of chrome)

View the code on codepen
Because the polygon edges are sharp, it might not be for you What you want, what you want to create is a rounded rectangle, so let's look at the value of Inset. Inset uses four values ​​(corresponding to the order of "Top, Right, Bottom, Left") to set the corner radius.

.clipClass { -webkit-clip-path: inset(25% 0 25% 0 round 0 25% 0 25%); }

The above values ​​correspond to:

inset(<top> <right> <bottom> <left> round <top-radius> <right-radius> <bottom-radius> <left-radius>)

The abbreviated form:

.clipClass { -webkit-clip-path: inset(25% 0 round 0 25%); }

Quick Reference

  • Circle: circle (radius at x-axis y-axis)
  • Ellipse: ellipse(x-rad y-rad at x-axis y-axis)
  • Polygon: polygon(x-axis y-axis , x-axis y-axis, … )
  • Inset: inset(top right bottom left round top-radius right-radius bottom-radius left-radius)
  • Create shape

    As you can see, prototype and rounded shapes are limited to a few values, making Polygons the best choice for creating complex shapes. Polygons can define multiple sets of points, allowing us to crop graphics in various ways.

    Comic Textbox


    View the code on codepen

    .clipClass {  -webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 75%, 75% 75%, 75% 100%, 50% 75%, 0% 75%);}

    Star


    View the code on codepen

    .clipClass {  -webkit-clip-path: polygon(50% 0%, 63% 38%, 100% 38%, 69% 59%, 82% 100%, 50% 75%, 18% 100%, 31% 59%, 0 38%, 37% 38%);}

    Animation

    Now that we have learned about various graphics and how to create them, how do we use these graphics to create the effects we want?
    Apply a hover to the shape and use transition properties to create a smooth effect. But you need to remember that the initial default state we create must use the same coordinate system as all hover states.
    View the code on codepen

    .animateClass {  -webkit-clip-path: polygon(20% 0%, 0% 0%, 0% 50%, 0% 80%, 0% 100%, 50% 100%, 80% 100%, 100% 100%, 100% 50%, 100% 0, 80% 0, 50% 0);}.animateClass:hover {  -webkit-clip-path: polygon(50% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);}
    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft