<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[A Programmer's Rambling]]></title><description><![CDATA[I like to talk about programming stuff.]]></description><link>https://blog.ramseynjire.me</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 13:58:29 GMT</lastBuildDate><atom:link href="https://blog.ramseynjire.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Creating Shadows with Pseudo-Elements]]></title><description><![CDATA[Using Pseudo-Elements for Box Shadows
What if I wanted a box-shadow with a linear gradient? I can add a color to the box-shadow property, but I can't define a linear gradient on it. A would work, but not B:
A:
{
  /* offset-x | offset-y | blur-radius...]]></description><link>https://blog.ramseynjire.me/creating-shadows-with-pseudo-elements</link><guid isPermaLink="true">https://blog.ramseynjire.me/creating-shadows-with-pseudo-elements</guid><category><![CDATA[CSS]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[CSS3]]></category><category><![CDATA[HTML]]></category><category><![CDATA[Design]]></category><dc:creator><![CDATA[Ramsey Njire]]></dc:creator><pubDate>Thu, 03 Mar 2022 00:12:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645129711948/FGy4nALvS.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-using-pseudo-elements-for-box-shadows">Using Pseudo-Elements for Box Shadows</h1>
<p>What if I wanted a <code>box-shadow</code> with a linear gradient? I can add a color to the <code>box-shadow</code> property, but I can't define a linear gradient on it. A would work, but not B:</p>
<p><strong>A</strong>:</p>
<pre><code>{
  <span class="hljs-comment">/* offset-x | offset-y | blur-radius | spread-radius | color */</span>
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">1px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.2</span>);
}
</code></pre><p><strong>B</strong>:</p>
<pre><code>{
  <span class="hljs-comment">/* offset-x | offset-y | blur-radius | spread-radius | color */</span>
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">1px</span> <span class="hljs-built_in">linear-gradient</span>(-<span class="hljs-number">45deg</span>, #<span class="hljs-number">687</span>bdd <span class="hljs-number">0%</span>, #<span class="hljs-number">221</span>e22 <span class="hljs-number">50%</span>, #e90e45 <span class="hljs-number">100%</span>);
}
</code></pre><p>That's because the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient%28%29"><code>linear-gradient</code></a> property is actually a special type of <em>image</em>, not a color. Since a shadow cannot have an image, we have a problem.</p>
<p>There's also the additional issue of the <em>shape</em> of the shadow. Box shadows are boxes by default. We could use <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/drop-shadow%28%29"><code>drop-shadow</code></a> if we wanted, but then we wouldn't be able to define a linear gradient on that. Moreover, drop-shadows conform to the shape of the object they are shadowing. You don't get to define a custom shape on that shadow.</p>
<p>To solve this problem, we can use CSS pseudo-elements. In particular, I'm talking about the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/::before"><code>::before</code></a> and <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/::after"><code>::after</code></a> pseudo-elements.</p>
<p>CSS pseudo-elements allow you to create extra content for your page without actually having to add elements to the HTML. There are <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements">A LOT</a> of them, but <code>::before</code> and <code>::after</code> are the most used by far. In this case, we'll use these to make a custom shadow for our box with some cool effects.</p>
<p>Before we start, below are some important rules we need to remember about working with <code>::before</code> and <code>::after</code>:</p>
<ul>
<li><p>They MUST have a <code>content</code> property defined on them. You may make this an empty string, which is the convention, but you cannot omit it from the pseudo-element's CSS rule or it won't show up on your page.</p>
</li>
<li><p>A pseudo-element is actually a <em>child</em> of the element it is defined on. This explains the monikers <code>::before</code> and <code>::after</code>. One appears <em>before</em> the rest of the containing element's children; the other appears <em>after</em> them.</p>
</li>
<li><p>Most of the rules that apply to normal elements apply to pseudo-elements as well. You can position and style them, and they will typically inherit styles from their parents like good little children.</p>
</li>
</ul>
<p>And now that we've set the ground rules, let's get to work!</p>
<h2 id="heading-a-gradient-shadow">A Gradient Shadow</h2>
<p>First, we define a simple card. Inside it, I'll add some text telling us what we're doing because why not?!</p>
<pre><code><span class="hljs-operator">&lt;</span>div class<span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span>h2 class<span class="hljs-operator">=</span><span class="hljs-string">"gradient-shadow"</span><span class="hljs-operator">&gt;</span>Gradient Shadow<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h2<span class="hljs-operator">&gt;</span>
<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
</code></pre><p>That's all we need. We can now style it. We'll start by defining a height and width for the card. We'll also turn the card into a <code>flex</code> container and center everything so it looks nice. We'll <code>position: relative</code> it so that we can <code>position: absolute</code> its children and play around with them more easily. Finally, we'll give it a white background for simplicity.</p>
<pre><code><span class="hljs-selector-class">.card</span> {
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">background</span>: white;
}
</code></pre><p>And now comes the fun part: adding the shadow. For this, we'll use a <code>::before</code> pseudo-element, though we can just as easily use an <code>::after</code> element. Since we'll <code>position: absolute</code> it, we are not constrained by where it logically appears in its parent element.</p>
<p>Here's a short list of what we'll do with the <code>::before</code> element:</p>
<ul>
<li>We'll give it an empty string value for the <code>content</code> property.</li>
<li>We'll <code>position: absolute</code> it. This allows us to define its offsets, and also whether it appears behind or in front of everything else.</li>
<li>Its <code>top</code> offset will be <code>40px</code> and its <code>left</code> offset will be 0. Since it's a shadow, we want it slightly below the parent container (by 'slightly', I mean <code>40px</code> below the parent).</li>
<li>We want the pseudo-element to be just as large as its parent. That way, it will be visible. We will therefore set the <code>width</code> and <code>height</code> properties to <code>100%</code>.</li>
</ul>
<blockquote>
<p>Note: elements are automatically assigned <code>display: block</code> when you <code>position: absolute</code> them, so we can set a width and height on our <code>::before</code> pseudo-element without explicitly assigning a display property to it. Otherwise, <code>::before</code> and <code>::after</code> pseudo-elements have <code>display: inline</code> by default.</p>
</blockquote>
<ul>
<li><p>We'll give our <code>::before</code> pseudo-element a<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient%28%29"><code>linear-gradient</code></a> for its background. For this, I just used a gradient generator and picked something that looked good.</p>
</li>
<li><p>To make our <code>::before</code> pseudo-element more shadow-like, we'll give it a blur. For this, we set its <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/filter"><code>filter</code></a> property to, well, <code>blur</code>. You can specify any blur size you want. I chose <code>30px</code> because it looked more natural to me.</p>
</li>
<li>The pseudo-element is still sitting in front of its parent. We'll set its <code>z-index</code> to <code>-1</code> so it goes behind it.</li>
<li>Finally, I also decided to <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale%28%29"><code>transform: scale()</code></a> the pseudo-element down slightly. This will mean we see less of it bulging out of the sides of its parent. Since it's been shifted downwards, we'll see most of the 'shadow' on the bottom of the container.</li>
</ul>
<p>The final code should look something like below:</p>
<pre><code><span class="hljs-selector-class">.card</span><span class="hljs-selector-pseudo">::before</span> {
  <span class="hljs-attribute">content</span>: <span class="hljs-string">""</span>;
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">43deg</span>, #<span class="hljs-number">4158</span>D0 <span class="hljs-number">0%</span>, #C850C0 <span class="hljs-number">46%</span>, #FFCC70 <span class="hljs-number">100%</span>);
  <span class="hljs-attribute">filter</span>: <span class="hljs-built_in">blur</span>(<span class="hljs-number">30px</span>);
  <span class="hljs-attribute">z-index</span>: -<span class="hljs-number">1</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">scale</span>(<span class="hljs-number">0.9</span>);
}
</code></pre><p>Below is a pen of the final result. Note I added some styling for the body so everything looks good.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/RamRam93/pen/KKyxKRK">https://codepen.io/RamRam93/pen/KKyxKRK</a></div>
<p>And we have a nice rainbow shadow!</p>
<p>But why stop there? Let's try and make something slightly fancier.</p>
<h2 id="heading-making-a-skewed-shadow">Making A Skewed Shadow</h2>
<p>Remember the ground rules we set? We said that you can do just about anything with a pseudo-element that you can do with a regular DOM element. That is an immense amount of power! Linear gradients are just the tip of the iceberg. You can shape them however you like and even go so far as to animate them. To give you a taste of this power, we'll create a skewed shadow with 2 different colors on opposite corners.</p>
<p>We'll start by writing the HTML. It will be the same div as before, except I won't be adding any text to it in this case. Nothing too complicated:</p>
<pre><code><span class="hljs-operator">&lt;</span>div class<span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
<span class="hljs-operator">&lt;</span>div<span class="hljs-operator">&gt;</span>
</code></pre><p>Let's style the card:</p>
<ul>
<li>As usual, we'll <code>position: relative</code> our card so we have control over the positioning of its children.</li>
<li>We'll set the <code>width</code> and <code>height</code> properties for our card. I chose the dimensions <code>220px</code> and <code>320px</code> respectively because I thought it looked good.</li>
<li>Finally, I gave the card a background color. This was so that it would look nice next to its colored shadow later on. This part is at your discretion, really.</li>
</ul>
<p>The final code should be as below:</p>
<pre><code><span class="hljs-selector-class">.card</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">220px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">320px</span>;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#030a2e</span>;
}
</code></pre><p>Now we can style the shadow.</p>
<p>For this, the concept is to have a shadow that juts out of 2 opposite corners of the card, with a distinct color at each corner. So, for example, if the shadow is to poke out of the top left and bottom right corners, it would have one color in the top left corner and another in the bottom right. To help it stand out, it would look slightly skewed at the corners.</p>
<ul>
<li>We can achieve the effect of 2 colors by defining a linear gradient on the shadow. It would start at one color in one corner and transition into another one by the time it reached the other corner. </li>
<li>We'll then place the shadow behind the card using its <code>z-index</code> property so all we can see are the colors popping out of the corners. </li>
<li>To make the corners actually pop out, we'll use the <code>transform: skewX() skewY()</code> property.</li>
<li>The other stuff, like positioning and setting the <code>content</code> property will be like in the previous example.</li>
</ul>
<p>There's something a little more complicated to talk about here (but only a little):</p>
<p>In the previous example, we explicitly set the width and height of our pseudo-element. Before that, we had given it a vertical (using <code>top</code>) and horizontal (using <code>left</code>) offset. We could do this because we had set its <code>position</code> property to <code>absolute</code>.</p>
<p>Elements with <code>position: absolute</code> have an interesting property that lets us set their width and height in other ways: if we set all 4 offsets (<code>top</code>, <code>right</code>, <code>bottom</code> and <code>left</code>), we can <em>stretch</em> the element. For example, if we set all 4 properties above to have a value of <code>4px</code>, the pseudo-element will have<code>8px</code> shorter <code>width</code>  and <code>height</code> properties (<code>4px</code> less on top, 4px less on the right, etc.) than its parent. It will be as if we had set the <code>width</code> and <code>height</code> properties explicitly.</p>
<p>On the other hand, if we set all the offsets to something like <code>-4px</code>, the pseudo-element would have <code>8px</code> longer <code>width</code> and <code>height</code> properties. So common is this practice of uniformly setting the offsets to size a <code>position: absolute</code> element that there is a CSS property to do it all at once: <code>inset</code>.</p>
<p>In this case, I want my <code>::before</code> element to be <code>8px</code> wider and taller than its container. That way, it will be easy to skew it at the corners. <code>8px</code> wider and taller is the same as <code>left: -4px</code>, <code>right: -4px</code>, <code>top: -4px</code>, and <code>bottom: -4px</code>. We can achieve that in one fell swoop with <code>inset: -4px</code>, allowing us to write less code.</p>
<blockquote>
<p>Note: the <code>inset</code> property takes a single value, so it should only be used where all 4 offsets would have the same value.</p>
</blockquote>
<p>The final code for the before element will look something like what we have below:</p>
<pre><code><span class="hljs-selector-class">.card</span><span class="hljs-selector-pseudo">::before</span> {
    <span class="hljs-attribute">content</span>: <span class="hljs-string">''</span>;
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">inset</span>: -<span class="hljs-number">4px</span>;
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">skewX</span>(<span class="hljs-number">3deg</span>) <span class="hljs-built_in">skewY</span>(<span class="hljs-number">6deg</span>);
    <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(-<span class="hljs-number">45deg</span>, #<span class="hljs-number">687</span>bdd <span class="hljs-number">0%</span>, #<span class="hljs-number">221</span>e22 <span class="hljs-number">50%</span>, #e90e45 <span class="hljs-number">100%</span>);
    <span class="hljs-attribute">z-index</span>: -<span class="hljs-number">1</span>;
}
</code></pre><p>I also added some rules for the body, just so everything is well centered, and gave it the same background color as the card, because I thought it made the effect look even nicer. The shadow should appear as below:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/RamRam93/pen/abVPyQJ">https://codepen.io/RamRam93/pen/abVPyQJ</a></div>
<h2 id="heading-afterglow">Afterglow</h2>
<p>The above card looks nice, but we could make it look even more interesting without adding a single element to the HTML. In this case, we'll leverage the <code>::after</code> pseudo-element.</p>
<p>The idea is simple: We'll give the <code>::after</code> pseudo-element all the properties we gave the <code>::before</code> pseudo-element, and then some. We'll add a blur effect on the <code>::after</code> element using the <code>filter</code> property so it simulates a <em>glow</em>:</p>
<pre><code><span class="hljs-selector-class">.card</span><span class="hljs-selector-pseudo">::after</span> {
    <span class="hljs-attribute">content</span>: <span class="hljs-string">''</span>;
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">inset</span>: -<span class="hljs-number">4px</span>;
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">skewX</span>(<span class="hljs-number">3deg</span>) <span class="hljs-built_in">skewY</span>(<span class="hljs-number">6deg</span>);
    <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(-<span class="hljs-number">45deg</span>, #<span class="hljs-number">687</span>bdd <span class="hljs-number">0%</span>, #<span class="hljs-number">221</span>e22 <span class="hljs-number">50%</span>, #e90e45 <span class="hljs-number">100%</span>);
    <span class="hljs-attribute">filter</span>: <span class="hljs-built_in">blur</span>(<span class="hljs-number">50px</span>);
    <span class="hljs-attribute">z-index</span>: -<span class="hljs-number">1</span>;
}
</code></pre><p>And now I present to you the end result!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/RamRam93/pen/MWOqYWj">https://codepen.io/RamRam93/pen/MWOqYWj</a></div>
<p>As you can see, we've been able to achieve a lot with just a single HTML element, while doing most of the work in CSS, all thanks to the power of pseudo-elements.</p>
<h2 id="heading-caveats">Caveats</h2>
<p>There are some things to consider, and gotchas to avoid, while we use pseudo-elements in our daily work.</p>
<h3 id="heading-accessibility">Accessibility</h3>
<p>Pseudo-elements don't appear in the DOM. That's probably why they're called 'pseudo-elements'; they're not actual HTML elements. You should therefore avoid using them to represent important information on your page as that might lead to accessibility issues. Not all screen readers can detect pseudo-elements.</p>
<h3 id="heading-z-index">Z-index</h3>
<p>In this exercise, we played around with the <code>z-index</code> of elements on the page to achieve the shadow effect. Note that the <code>z-index</code> property can only be applied to positioned elements. Also, whenever you define <code>z-index</code> on a suitable element, you create a new <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context">stacking context</a> for that element and its children. If you ever find <code>z-index</code> not working the way you expect it to, try to figure out whether <code>z-index</code> was defined on an element earlier in the CSS, and whether this is determining where your current element is in the stacking context. You can read the article I've linked on stacking contexts to understand how this works.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And with that, we're done! We managed to create colorful shadows using pseudo-elements. We've only touched the tip of the iceberg here. As you can imagine, a lot more is possible with pseudo-elements. There are a lot of talented people out there who come up with new ways to utilize this CSS feature every day, sometimes making artwork that would give Picasso or Van Gogh a run for their money. Hopefully, this introduction will inspire you to create your own art too.</p>
<h4 id="heading-attributions">Attributions</h4>
<p>I was inspired to write this by <a target="_blank" href="https://www.youtube.com/watch?v=jMbWDcc5oNc">this video</a>, where I learned this technique. It's a great channel with lots of gems. Be sure to check it out!</p>
<p>Any interesting thoughts? Let's talk in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[Image Fade Effect Using Pseudo-Elements]]></title><description><![CDATA[Having images on your page is nice, but what if you want to create a fading effect (like the banner image for this article)? As it turns out, you can easily do this in CSS using ::before and ::after pseudo-elements. That’s what we’ll explore in this ...]]></description><link>https://blog.ramseynjire.me/image-fade-effect-using-pseudo-elements-689a96d6b1ad</link><guid isPermaLink="true">https://blog.ramseynjire.me/image-fade-effect-using-pseudo-elements-689a96d6b1ad</guid><dc:creator><![CDATA[Ramsey Njire]]></dc:creator><pubDate>Tue, 25 Jan 2022 22:17:14 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644862204235/yhGF3vOQT.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Having images on your page is nice, but what if you want to create a fading effect (like the banner image for this article)? As it turns out, you can easily do this in CSS using ::before and ::after pseudo-elements. That’s what we’ll explore in this article.</p>
<p>I built a small demo on CodePen. We’ll go through it in detail below. Feel free to keep referring back here to better understand how the effect works:</p>
<iframe src="https://codepen.io/RamRam93/embed/preview/eYeYyqO?default-tabs=css%2Cresult&amp;height=600&amp;host=https%3A%2F%2Fcodepen.io&amp;slug-hash=eYeYyqO" width="700" height="525"></iframe>

<h3 id="heading-setting-up-the-page">Setting Up the Page</h3>
<p>Assuming you’ve applied a basic <a target="_blank" href="https://stackoverflow.com/questions/11578819/css-reset-what-exactly-does-it-do#:~:text=A%20CSS%20Reset%20%28or%20%E2%80%9CReset,unstyled%20websites%20appear%20more%20legible.">CSS reset</a> and have your HTML in place, we will start by styling the body to occupy the whole page and center its contents:</p>
<p>body {  </p>
<p>  min-height: 100vh;<br />  display: flex;<br />  flex-direction: column;<br />  justify-content: center;<br />  align-items: center;<br />}</p>
<p>Next, we create two divs to demonstrate the fade. We’ll give each of these a width and height. We’ll also set the positioning of each to relative. That way, we have control over how to position their children. ::before and ::after pseudo-elements can be treated and styled as children of their parents. They can also only be positioned relative to the last positioned ancestor. Since we want the pseudo-elements positioned relative to the two divs we create, we’ll set the position of the divs to relative. You can learn more about positioning <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/position">here</a> and more about pseudo-elements <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements">here</a>.</p>
<p>HTML:</p>
<div class="first-div"></div><br /><div class="second-div"></div>

<p>CSS:</p>
<p>.first-div, .second-div{<br />  position: relative;<br />  background: #ccc;<br />  height: 200px;<br />  width: 200px;<br />}</p>
<p>.second-div {<br />  margin-top: 10px;<br />}</p>
<p>Now that we have the divs in place, we’ll style the pseudo-elements.</p>
<p>For the first div, I will add the fading effect to the bottom. We position the ::after pseudo-element on the bottom, give it a width and height, and set a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient%28%29">linear gradient</a> on it. Don’t forget to set the content of the pseudo-element or it won’t work. Also, the linear gradient should end at completely transparent for the fading effect to blend into the rest of the div.</p>
<p>.first-div::after {<br />  content: "";<br />  position: absolute;<br />  left: 0;<br />  bottom: 0;<br />  background: linear-gradient(to top, #999, transparent);<br />  width: 100%;<br />  height: 50px;<br />}</p>
<p>We’ll do the same for the second div, except this time we’ll use a ::before element (it really doesn’t matter whether you use a ::before or ::after element, I just decided to use both to demonstrate that it can work with both). We’ll position it at the top and give it a linear gradient going down instead of up.</p>
<p>.second-div::before {<br />  content: "";<br />  position: absolute;<br />  left: 0;<br />  top: 0;<br />  background: linear-gradient(to bottom, #999, transparent);<br />  width: 100%;<br />  height: 50px;<br />}</p>
<p>The two divs should look something like below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644862200293/6dKoNvKoD.png" alt /></p>
<p>Let’s try and do the same for an image. First, we set up some HTML to organize everything:</p>
<div class="container"><br />  <div class="no-fade"><br />    <img src="[https://res.cloudinary.com/insimp/image/upload/v1643138771/argonath\_ldg1ij.jpg](https://res.cloudinary.com/insimp/image/upload/v1643138771/argonath_ldg1ij.jpg)" /><br />  </div><br />  <div class="bottom-fade"><br />    <img src="[https://res.cloudinary.com/insimp/image/upload/v1643138771/argonath\_ldg1ij.jpg](https://res.cloudinary.com/insimp/image/upload/v1643138771/argonath_ldg1ij.jpg)" /><br />  </div><br /></div>

<p>You can, of course, set the <code>src</code> attribute to any source you like.</p>
<p>Next, we’ll style the container div so it arranges the children properly:</p>
<p>.container {<br />  display: flex;<br />  width: 60%;<br />  justify-content: space-between;<br />  margin-top: 50px;<br />}</p>
<p>The margin property is so that it has some room to breathe from the divs above that we created earlier.</p>
<p>Next, we’ll style the children so they also look good (and similar to our earlier divs:</p>
<p>.no-fade, .bottom-fade {<br />  position: relative;<br />  background: #ccc;<br />  height: 200px;<br />  width: 200px;<br />}</p>
<p>And then we’ll style the images so they fit properly in the divs. We will explicitly set the height and width of the image and then use <code>object-fit: cover</code> so the image maintains its aspect ratio. You can learn more about object-fit <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit">here</a>.</p>
<p>img {<br />  width: 100%;<br />  height: 100%;<br />  object-fit: cover;<br />}</p>
<p>Now we can finally style the pseudo-element on the right div to create the effect:</p>
<p>.bottom-fade::after {<br />  content: "";<br />  position: absolute;<br />  left: 0;<br />  bottom: 0;<br />  background: linear-gradient(to top, #111, transparent);<br />  width: 100%;<br />  height: 100px;<br />}</p>
<p>The end result shows the original image and its faded version side by side:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644862201990/8mBkU5h5W.png" alt /></p>
<p>I did the same with the images fading from the top. I’ll share the complete CodePen again so you can play around with it.</p>
<iframe src="https://codepen.io/RamRam93/embed/preview/eYeYyqO?default-tabs=css%2Cresult&amp;height=600&amp;host=https%3A%2F%2Fcodepen.io&amp;slug-hash=eYeYyqO" width="700" height="525"></iframe>

<p>And that’s it for now. What do you think about this trick? Drop a comment below!</p>
]]></content:encoded></item></channel></rss>