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; }
Benefits of Using Box Shadow
Using box-shadow offers several advantages:
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!