Implementation of CSS linear gradient concave rectangle transition effect

Implementation of CSS linear gradient concave rectangle transition effect

This article discusses the difficulties and ideas of the linear gradient concave rectangle transition effect. It mainly introduces the implementation of concave rectangle, adding linear gradient to concave rectangle, and the transition of linear gradient background color. If you have a better implementation solution, please feel free to discuss with me in the comment section.

Above

Implementing a concave rectangle

The inspiration for implementing this style comes from an article on the Internet that uses CSS to achieve concave corners. It describes how to achieve the concave corner effect of Chrome tabs, which is roughly as follows:

Use radial-gradient gradient to achieve this. Reduce the blur distance of the gradient to 0 to see a clear circular boundary. Following this idea, we can achieve the style of a concave rectangle. By adjusting position of the gradient to control the position of the center point, and adjusting the size of the radial circle to control the curvature of the concave surface, as shown below:

You can debug it in the radial gradient demo on MDN:

background: radial-gradient(300px 300px at 112px 0, #eee 75%, #333 75%);

Small details

  1. If the blur distance of the radial gradient is 0, you will see obvious jagged edges on the circular border. Increasing the blur distance by 1px can eliminate the jagged edges. (Is this the legendary anti-aliasing?)
  2. Android 4.4.4 is not compatible with radial gradients.

Concave rectangle with linear gradient

In the previous section, we have implemented a concave rectangle. So how do we set a linear gradient for the concave rectangle? The background is already in use. You cannot set both radial and linear gradients.

You can use mask-image to achieve the effect of cutting out the image. Here is an example from MDN:

The MDN example uses a five-pointed star svg to cut out a green five-pointed star on a green background. To achieve a gradient concave rectangle, you can use a concave rectangle shape to cut out a linear gradient background.

Use mask-image to set the shape and background to set the linear gradient:

.xxx {
  background: linear-gradient(115deg, #ff66ff, #4db8ff);
  mask-image: radial-gradient(300px 300px at 112px 0, rgba(255, 255, 255, 0) 75%, #333 75%);
}

Effect:

Small details

Mask-image is more compatible than radial gradient on mobile devices, and is supported by Android 4.4.4.

Gradient background color transition

Background-image itself does not support transition animation, but it can be achieved through some fancy operations. Teacher Zhang Xinxu’s article explains it in great detail:

https://www.zhangxinxu.com/wordpress/2020/08/background-image-animation/

https://www.zhangxinxu.com/wordpress/2018/03/background-gradient-transtion/

Background-image does not support transition animation, but opacity does. Add another pseudo-element on the previous linear gradient concave rectangle, set the background color of the pseudo-element to another linear gradient color, and then control the opacity of the pseudo-element to achieve a linear gradient transition effect.

This concludes this article about the implementation of CSS linear gradient concave rectangle transition effect. For more relevant CSS linear gradient concave rectangle transition content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  mysql data insert, update and delete details

>>:  Example of fork and mutex lock process in Linux multithreading

Recommend

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

Vue implements partial refresh of the page (router-view page refresh)

Using provide+inject combination in Vue First you...

What is JavaScript anti-shake and throttling

Table of contents 1. Function debounce 1. What is...

JavaScript to achieve JD.com flash sale effect

This article shares the specific code of JavaScri...

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

Detailed explanation of docker-machine usage

Docker-machine is a Docker management tool offici...

JavaScript custom calendar effect

This article shares the specific code of JavaScri...

Vertical and horizontal splitting of MySQL tables

Vertical Split Vertical splitting refers to the s...

Vue multi-page configuration details

Table of contents 1. The difference between multi...

Implementation of React configuration sub-routing

1. The component First.js has subcomponents: impo...

html option disable select select disable option example

Copy code The code is as follows: <select> ...

Detailed tutorial on installing MySQL database in Linux environment

1. Install the database 1) yum -y install mysql-s...

Quickjs encapsulates JavaScript sandbox details

Table of contents 1. Scenario 2. Simplify the und...

Steps to purchase a cloud server and install the Pagoda Panel on Alibaba Cloud

Alibaba Cloud purchases servers Purchase a cloud ...

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...