CSS linear-gradient with semi-transparent borders

beercohol

I'm using linear-gradients with semi-transparent borders for creating modules and buttons etc. Using (for example) rgba(0,0,0,0.1) as the border colour is convenient, because I can set any background colour on my elements without having to worry about the border colour again.

However I've noticed a very odd effect - when combined with a linear-gradient background, browsers use the height of the element's padding box to calculate the height of the gradient, which means it repeats over the top and bottom margins, creating a very odd effect: Gradient/Borders example

Here is the CSS that generates the "Actual" box:

.box {
    box-sizing: border-box;
    height: 100px;
    width: 100px;
    border: 25px solid rgba(0,0,0,0.1);
    background-color: #eee;
    background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 100%); 
    margin-bottom: 20px;
}

Now I've already found a workaround that allows me to achieve the desired effect, by forcing the background-size to be 100% + the border size. This is what generated the "Desired" box (.box2):

.box2 {
    background-position: 0 center;
    background-size: auto calc(100% + 50px);
}

However that seems a bit hacky.

So my question is: Can anyone explain why this is - I can't find it documented anywhere, and does anyone have a neater solution?

Here's the JS Fiddle that I used to create the examples, and it also includes a box with an actual image background for comparison: http://jsfiddle.net/29rgksgx/4/

vals

You can choose which of the boxes is used as a reference for the background

Choose border-box and it will work as desired

You can choose between border content and padding box

.box {
    box-sizing: border-box;
    height: 100px;
    width: 100px;
    border: 25px solid rgba(0,0,0,0.1);
    background-color: #eee;
    margin-bottom: 20px;
    background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 100%);   
    background-origin: border-box;  /* the trick */
}
<div class="box"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create semi transparent linear gradient background and border using css?

Creating semi-transparent borders

CSS button transition linear-gradient background into transparent background

4 gradient borders in CSS

CSS Semi-Transparent box

How to add a fully transparent to semi-transparent gradient in Android

CSS Linear gradient is inaccurate?

CSS linear gradient with separations

transparent linear gradient looking strange in safari

Linear gradient with one color being completely transparent?

CSS transparent radial gradient issue

Apply linear transparent gradient to a portion of image with transparent background in python

Why does a CSS linear-gradient from color1 to color2 render exactly the same as a linear-gradient from color1 to transparent on a color2 background?

CSS angled linear gradient with "nested" gradient

CSS linear gradient progress animation

CSS linear gradient angle position

CSS - wrong linear-gradient

CSS - Crisp boundaries for linear gradient

Dynamically update Linear Gradient CSS

Animating Linear Gradient using CSS

Chome Linear-Gradient css

CSS Gradient over multiple element's borders

CSS transparent triangle over background with half-transparent borders

Gradient borders

Reproducing SVG gradient equivalent to CSS gradient with linear-gradient

Flutter Stack's child is transparent while using linear gradient

Making a css animation using a transparent gradient mask

blurred linear gradient background to solid color Css

Css background: offset for repeating-linear-gradient