Table of Contents
Understand the CSS properties: clip-path
A simple triangle clipping
Step-by-Step Analysis
Shape
Circle
Ellipse
Illustration
Quick Reference
Create shape
Comic Textbox
Star
Animation
Home Web Front-end HTML Tutorial CSS3: clip-path detailed explanation_html/css_WEB-ITnose

CSS3: clip-path detailed explanation_html/css_WEB-ITnose

Jun 24, 2016 am 11:41 AM

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%); }
Copy after login

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%
Copy after login

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%); }
Copy after login

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%); }
Copy after login

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%); }
Copy after login

The above values ​​correspond to:

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

The abbreviated form:

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

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%);}
    Copy after login

    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%);}
    Copy after login

    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%);}
    Copy after login
    Statement of this Website
    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

    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

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

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

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

    See all articles