Home > Web Front-end > CSS Tutorial > Why Does a Transparent Border Distort a Gradient Background, and How Can I Fix It?

Why Does a Transparent Border Distort a Gradient Background, and How Can I Fix It?

Mary-Kate Olsen
Release: 2024-11-23 21:12:10
Original
1073 people have browsed it

Why Does a Transparent Border Distort a Gradient Background, and How Can I Fix It?

Transparent Border Distortion on Gradient Background

Applying a transparent border to an element with a linear-gradient background can result in a peculiar visual effect where the edges of the element appear distorted or "flat." This occurs because the gradient extends beyond the padding-box into the border-box, causing the border to interfere with the colors.

Cause of the Effect

The issue stems from the different rendering order of the gradient and border. The gradient is drawn within the padding-box, while the border is rendered outside the padding-box. As a result, the borders overlap onto the gradient, creating the unwanted effect.

Solution: Using Box Shadow Instead

To rectify this, consider using box-shadow:inset instead of a border. Box-shadow is rendered within the padding-box, similar to the gradient, eliminating the overlap issue.

Code Sample

.colors {
    width: 100px;
    height: 50px;
    background: linear-gradient(
        to right,
        #78C5D6,
        #459BA8,
        #79C267,
        #C5D647,
        #F5D63D,
        #F08B33,
        #E868A2,
        #BE61A5
    );
    box-shadow: inset 0 0 0 10px rgba(0, 0, 0, 0.2);
    padding: 10px;
}
Copy after login

Benefits of Using Box Shadow

Using box-shadow offers several advantages:

  • It produces the same visual effect as a border without affecting the background rendering.
  • Box-shadow doesn't take up physical space, so you can adjust the padding accordingly.

Fiddle Demonstration

Refer to the following fiddle to see the effect in action: http://jsfiddle.net/ilpo/fzndodgx/5/

The above is the detailed content of Why Does a Transparent Border Distort a Gradient Background, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template