Search code examples
csssvgsassgradientnoise

Can i create a scss gradient from black to transparent with a noise texture?


I found this old thread on how to create noise gradients, but it doesn't properly fade to transparent which creates a hard border effect. I was wondering if it was possible to have only the gradient be noisy, not the transparency in the end.

Here is what I tried so far:

.black-bar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100px;
    background-size: cover;
    background-position: center;
    background-repeat: repeat;
    stop-color: black;
    background-image: linear-gradient(to bottom,
            rgba(0, 0, 0, 1) 0%,
            rgba(0, 0, 0, 1) 50%,
            rgba(0, 0, 0, 0) 100%),
            url('/noise.svg');
    z-index: -1;  
}
<svg viewBox='0 0 250 250' xmlns='http://www.w3.org/2000/svg'>
  <filter id='noiseFilter'>
    <feTurbulence 
      type='fractalNoise' 
      baseFrequency='5' 
      numOctaves='3' 
      stitchTiles='stitch'/>
  </filter>
  
  <rect width='100%' height='100%' filter='url(#noiseFilter)'/>
</svg>

And here is the border effect I'm talking about:

Hard border effect I don't want

Noisy gradient I'm looking for (Mockup created in Photoshop)


Solution

  • I found the solution. Inverting the noise while fading itself to transparent while applying it after the gradient seemed to create the transition I'm looking for. Here is the scss

    .black-bar {
        position: absolute;
        width: 100%;
        height: 300px;
        background: linear-gradient(180deg, 
            rgba(0,0,0,1) 0%,
            rgba(0,0,0,1) 20%,
            rgba(0, 0, 0, 0) 100%);
        z-index: 1;
    }
    
    .black-bar::after {
        content: '';
        position: absolute; 
        width: 100%;
        height: 100%;
        background: url('/noise.svg');
        filter: contrast(200%) brightness(100%) invert(100%) saturate(0%);
        mask-image: linear-gradient(180deg, 
            rgba(0,0,0,0) 0%,
            rgba(0,0,0,.3) 40%,
            rgba(0,0,0,0) 100%);
        z-index: -1;
    }
    

    And here is the final result