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

Deployment and configuration of Apache service under Linux

Table of contents 1 The role of Apache 2 Apache I...

How to purchase and install Alibaba Cloud servers

1. Purchase a server In the example, the server p...

How to use custom tags in html

Custom tags can be used freely in XML files and HT...

Detailed explanation of CSS style sheets and format layout

Style Sheets CSS (Cascading Style Sheets) is used...

Detailed explanation of invisible indexes in MySQL 8.0

Word MySQL 8.0 has been released for four years s...

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

How to connect to MySQL using C++

C++ connects to MySQL for your reference. The spe...

Use of Linux relative and absolute paths

01. Overview Absolute paths and relative paths ar...

Web front-end development CSS related team collaboration

The front-end development department is growing, ...

Detailed installation and configuration of hadoop2.7.2 under ubuntu15.10

There are many Hadoop installation tutorials on L...

Writing Snake Game with Native JS

This article shares the specific code of writing ...

How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7

1. Download the MySQL installation package (there...

Vue3.0 handwritten carousel effect

This article shares the specific code of Vue3.0 h...