How to Fade the Edge of a Div With Just CSS

CSS - Fading All Edges of div to Transparent over Defined Distance

I've used box shadows in the past to achieve this sort of effect.

  box-shadow: 0px 0px 25px 25px rgba(55,54,51, 1);

By adding a box shadow with a translation of 0px in any direction, a spread distance of 25px, and a blur radius of 25px, it makes it the 50px blur you like. By changing the margin to 50px and using top, right, etc. for positioning you can get it exactly where you want.

HTML:

<div class="formBackground">
<form id="gform" method="POST" action="***">
<input type="text" id="name" name="name" placeholder="Name" style="width: 100%; float: left;">
<input type="text" id="email" name="email" placeholder="Email" style="width: 100%; float: left;">
<input type="textarea" id="message" name="message" placeholder="Write your message here..." style="width: 100%; float: left;">
</form>
</div>

CSS:

.formBackground {
height: auto;
background-color : rgba(55,54,51, 1);
overflow: hidden;
margin: 50px;
box-shadow: 0px 0px 25px 25px rgba(55,54,51, 1);
}

HTML/CSS: How to fade the left and right edges of a div?

The default of linear gradients run from top to bottom. So, you need to use to left or to right with transparent. To make it more clear you have to reduce transparency. The transition is made between two-color or more which creates a band of colors that progress in a straight line. See here.

.container {
height: 234px;
width: 234px;
overflow: scroll;

mask-image: linear-gradient(transparent,
black 20%,
black 80%,
transparent 100%);
-webkit-mask-image: linear-gradient( to right,transparent,
black 20%,
black 80%,
transparent 100%);

}

.container::-webkit-scrollbar {
display: none;
}


<div class="container">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit
dolorem libero, dolor, fuga illo nobis rem ipsam ipsa
perferendis dolore autem fugiat! Dicta eius repellendus totam
qui maiores odio a! Lorem ipsum dolor sit amet consectetur
adipisicing elit. Impedit dolorem libero, dolor, fuga illo nobis
rem ipsam ipsa perferendis dolore autem fugiat! Dicta eius
repellendus totam qui maiores odio a! Lorem ipsum dolor sit amet
consectetur adipisicing elit. Impedit dolorem libero, dolor,
fuga illo nobis rem ipsam ipsa perferendis dolore autem fugiat!
Dicta eius repellendus totam qui maiores odio a! Lorem ipsum
dolor sit amet consectetur adipisicing elit. Impedit dolorem
libero, dolor, fuga illo nobis rem ipsam ipsa perferendis dolore
autem fugiat! Dicta eius repellendus totam qui maiores odio a!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit
dolorem libero, dolor, fuga illo nobis rem ipsam ipsa
perferendis dolore autem fugiat! Dicta eius repellendus totam
qui maiores odio a! Lorem ipsum dolor sit amet consectetur
adipisicing elit. Impedit dolorem libero, dolor, fuga illo nobis
rem ipsam ipsa perferendis dolore autem fugiat! Dicta eius
repellendus totam qui maiores odio a!
</p>
</div>

is there css to fade edges of a div?

There are several ways of doing such faded style with CSS3. But it's better to apply a combination of both Gradient Border and Inset Box Shadow. You may get a much better idea on this Codepen Example

CSS Div Fade Scroll Styling

You can use CSS mask-image for browsers that support it (which is pretty common nowadays). Depending on your needs, JS might be needed (for example, if you wan to conditionally toggle the fading depending if the user has scrolled to the very top/bottom of the element).

To create a "bottom fadeout" effect, you can use this following CSS rule:

mask-image: linear-gradient(to bottom, black calc(100% - 48px), transparent 100%);

It basically says:

  • Create a linear gradient from the top going towards the bottom
  • When we are 48px away from the bottom, start changing color from black (see through when masking) to transparent (opaque when masking)

However, this only creates the bottom gradient. To mask both the top and the bottom, you will need to combine two gradients into one:

mask-image: linear-gradient(to bottom, transparent 0%, black 48px, black calc(100% - 48px), transparent 100%);

Now you ask: how do we hide the mask? Easy: if the color stops are spaced 0px apart, the mask will be black throughout, which will make it completely see through.

The JS part is simply toggling these color stops using classes. We use CSS custom properties that can be overridden by adding/removing a class. JS is used to sniff out the scroll position and toggle these classes. See proof-of-concept below:

function setClasses(el) {
const isScrollable = el.scrollHeight > el.clientHeight;

// GUARD: If element is not scrollable, remove all classes
if (!isScrollable) {
el.classList.remove('is-bottom-overflowing', 'is-top-overflowing');
return;
}

// Otherwise, the element is overflowing!
// Now we just need to find out which direction it is overflowing to (can be both)
const isScrolledToBottom = el.scrollHeight <= el.clientHeight + el.scrollTop;
const isScroledlToTop = el.scrollTop === 0;
el.classList.toggle('is-bottom-overflowing', !isScrolledToBottom);
el.classList.toggle('is-top-overflowing', !isScroledlToTop);
}

document.querySelector('#content').addEventListener('scroll', (e) => {
const el = e.currentTarget;
setClasses(el);
});

setClasses(document.querySelector('#content'));
* {
box-sizing: border-box;
}

body {
padding: 0;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: #2a9d8f;
font-family: Arial, sans-serif;
}

.box {
width: 50vw;
height: 90vh;
background-color: #fff;
border-radius: 16px;
padding: 32px;
display: flex;
flex-direction: column;
}

.box h1 {
margin: 0;
}

#content {
overflow-y: auto;
-webkit-mask-image: linear-gradient(to bottom, transparent 0, black var(--top-mask-size, 0), black calc(100% - var(--bottom-mask-size, 0)), transparent 100%);
mask-image: linear-gradient(to bottom, transparent 0, black var(--top-mask-size, 0), black calc(100% - var(--bottom-mask-size, 0)), transparent 100%);
}

#content.is-top-overflowing {
--top-mask-size: 48px;
}

#content.is-bottom-overflowing {
--bottom-mask-size: 48px;
}
<article class="box">
<h1>Lorem ipsum</h1>
<section id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque vel metus at tellus consectetur rhoncus. Aenean euismod eget mauris cursus tincidunt. Cras lectus dolor, suscipit nec porttitor a, tincidunt vel arcu. Etiam facilisis faucibus lectus
vitae pharetra. Fusce euismod lacus sit amet consequat mattis. Aliquam suscipit metus a nulla suscipit varius. In tempor suscipit pretium.</p>
<p>Vivamus aliquam eros eu orci finibus, id efficitur lorem placerat. Sed congue ipsum quis accumsan feugiat. Nunc imperdiet faucibus tellus, nec tincidunt ipsum dictum non. Quisque dui lacus, bibendum vitae lectus vel, vulputate tempus quam. Praesent
ullamcorper ultricies felis, at fermentum massa gravida quis. Pellentesque mollis, urna sed vehicula elementum, dolor lorem congue ipsum, eget ullamcorper ex arcu nec mi. Suspendisse potenti. Morbi pharetra eu nisi quis laoreet. Donec ut quam id
justo pulvinar volutpat eget ut magna.</p>
<p>Etiam aliquam eleifend dignissim. Donec sagittis tincidunt quam, eget venenatis mi sollicitudin et. Vestibulum id lectus mi. Aenean enim sem, viverra at velit ut, posuere dapibus tellus. Ut accumsan mi eu lectus sollicitudin ornare. Morbi eu semper
lacus. Aenean id erat at nulla ornare consequat a at leo. In risus risus, blandit sit amet tortor sed, accumsan porttitor velit. Quisque interdum id ipsum quis convallis. Suspendisse vel pretium augue. Sed tincidunt, felis ut porta consectetur,
mauris urna hendrerit diam, nec aliquet augue ante quis metus.</p>
</section>
</article>

Box with fading opacity near edge

You need to use CSS3 transparency and Gradient feature both at the same time.

some thing like below css:

.gradient{
background-image: -webkit-gradient(
linear, center center, left center, from(rgba(255, 255, 255, 1.0)),
to(rgba(255, 255, 255, 0))
);

see this fiddle (it includes support for all the browsers. Am not sure of IE, cause I don't have it :))

How to fade edges of background image of element to blend in with the main background image?

You can use the mask-* CSS properties to achieve the effect.

The following example produce a 10px long fading edge.

.body {
display: grid;
width: 200px;
height: 200px;
/* replace with the image you like here */
background-image: repeating-linear-gradient(-45deg,
yellow,
yellow 20px,
black 20px,
black 40px);
}

.content {
margin: 25px;
/* replace with the image you like here */
background-image: linear-gradient(to top, blue 0%, blue 100%);

/* for webkit-based browsers */
-webkit-mask-image:
linear-gradient(to top, black 0%, black 100%),
linear-gradient(to top, transparent 0%, black 100%),
linear-gradient(to right, transparent 0%, black 100%),
linear-gradient(to bottom, transparent 0%, black 100%),
linear-gradient(to left, transparent 0%, black 100%);
-webkit-mask-position:
center,
top,
right,
bottom,
left;
-webkit-mask-size:
100% 100%,
100% 10px,
10px 100%,
100% 10px,
10px 100%;
-webkit-mask-repeat:
no-repeat,
no-repeat,
no-repeat,
no-repeat,
no-repeat;
-webkit-mask-composite:
source-out,
source-over,
source-over,
source-over;

/* for browsers which have implemented the official spec */
mask-image:
linear-gradient(to top, black 0%, black 100%),
linear-gradient(to top, transparent 0%, black 100%),
linear-gradient(to right, transparent 0%, black 100%),
linear-gradient(to bottom, transparent 0%, black 100%),
linear-gradient(to left, transparent 0%, black 100%);
mask-position:
center,
top,
right,
bottom,
left;
mask-size:
100% 100%,
100% 10px,
10px 100%,
100% 10px,
10px 100%;
mask-repeat:
no-repeat,
no-repeat,
no-repeat,
no-repeat,
no-repeat;
mask-composite:
subtract,
add,
add,
add;
}
<div class="body">
<div class="content"></div>
</div>


Related Topics



Leave a reply



Submit