The design has a section title, with a little circle on the left side of it. When we hover on the section title, the circle gets bigger.
. section-title : before { content : "" ; width : 20 px ; height : 20 px ; background : blue ; / * Other styles * / } . section-title : hover: before { transform : scale ( (1.2)); }
Easy and straightforward. Let’s extend that concept to more useful use cases.
Projects / Blog Section
On my website, I have a section that lists all of my projects. I wanted to add a thumbnail for each project, but it wasn’t a top priority thing for me. What’s more important to me is the link itself. I first saw this effect a while ago onEthan Marcotte Website.
The above design mockup shows the idea that I wanted to apply. Each colored link in the paragraph has a pseudo-element paired with it.
1) I added padding to the hero
I want toReserve space for the pseudo-elements, so adding padding is a solution for that.
2) Position the pseudo-elements absolutely
To position them absolutely, I need to define which parent is the relative one. It should be added to the hero section.
Notice in the below GIF how removingposition: relative
from the. hero
section affects the pseudo-elements.
3) Adding pseudo-elements
The final step is to add the pseudo-elements along with their hover effects. Here is how I did it:
.Link-1 { color : # ; } @ media ( min-width : (px)) { .Link-1 : After { content : "" ; position : absolute ; right : 0 ; top : 20 px ; width : 150 px ; height : 100 px ; background : currentColor ; opacity : 0. 85 ; transition : 0.3s ease-out ; } .Link-1 : hover { text-decoration : underline ; } .Link-1 : hover: after { transform : scale ( (1.2)); opacity : 1 ; } }
Notice that I’ve usedcurrentColor
for the pseudo-element background. If you don’t know about this keyword, it inherits from thecolor
value of its parent. So at any point, I want to change the colors of the links, it’s easy to change them only once.
See the Pen Pseudo-elements: Example 1 by Ahmad Shadeed (@ shadeed ) onCodePen .
If you are curious, go to the home page of my website and check the “My Projects” section. I have used the above technique.
Increasing the clickable area size
By adding a pseudo-element to a link, the clickable area around it will get bigger. This is very useful and will enhance the experience for the user. Let’s take an example:
Moreover, it can be used to extend the clickable area of a card component, which has a view more link. I wrote a detailedarticle about that topic.
Overlays
Let’s suppose that there is an element with a background image, and the design has a gradient overlay with blending mode set tocolor
. Pseudo-elements can help with that!
.Hero { position : relative ; height : 300 px ; background : url ("image.jpg") center/ cover ; } .Hero : after { content : "" ; position : absolute ; left : 0 ; top : 0 ; width : 100% ; height : 100% ; background-image : linear-gradient ( (deg), # 851717 0 % , # C (%)); mix-blend-mode : color ; }
See the Pen Pseudo-elements: Example 2 by Ahmad Shadeed (@ shadeed ) onCodePen .
Wrapped Shadows
I’m not sure if the naming is correct, but this is what I got. Back in the days, I used to create a shadow that is skewed at the edges. It has a little subtle effect. Guess what! It’s possible to do them with pseudo-elements.
Creating the element
I created a div element with regular styles as below.
.Elem { position : relative ; display : flex ; align-items : center ; max-width : 400 px ; background : # fff ; padding : 2rem 1rem ; font-size : 1.5rem ; margin : 2rem auto ; text-align : center ; box-sizing : border-box ; }
Adding pseudo-elements
Then, I added: before
and: after
pseudo-elements with a width of 50% for each of them (I added a different background for each one for explaining purposes).
.Elem : before , .elem : after { content : "" ; position : absolute ; top : 2px ; width : 50% ; height : 100% ; } .elem : before { left : 0 ; background : gray ; } .elem : after { right : 0 ; background : # 000 ; }
Next, I will addtransform: skew (x)
where X is 2 degrees. For one of them, X should be negative to achieve the desired effect.
.Elem : before { transform : skew ( - 2deg ); } .elem : after { transform : skew ( 2deg ); }
Next, I will add (z-index: -1) to each pseudo-element to move it behind its parent.
Once that is done, I did the following:
Addedfilter: blur
Reduced opacity
Added a gradient from transparent to black (To hide the pseudo-elements edges at the top center of its parent)
Final Code
.Elem { position : relative ; display : flex ; align-items : center ; max-width : 400 px ; background : # fff ; padding : 2rem 1rem ; font-size : 1.5rem ; margin : 2rem auto ; text-align : center ; box-sizing : border-box ; } .elem : before , .elem : after { content : "" ; position : absolute ; top : 3px ; width : 50% ; height : 100% ; z-index : - 1 ; background : linear-gradient ( to bottom , transparent , # 000 ); filter : blur ( 3px ); opacity : 0.3 ; } .elem : before { left : 0 ; transform : skewY ( - 2deg ); } .elem : after { right : 0 ; transform : skewY ( 2deg ); }
There is another option, which is to swap theskewY
values between the: before
and: after
pseudo-elements. That will result in a different effect.
See the Pen Pseudo-elements: Example 3 by Ahmad Shadeed (@ shadeed ) onCodePen .
Using: after
vs: before
In a recent Twitterdiscussion , I learned that it’s better to use: before
instead of: after
. Why? Because when using: after
, it might require us to addz-index
to other nested elements so the pseudo-element won’t overlap them. Let’s take a real-life example.
Here is a simple card that consists of a thumbnail and title. If you notice, there is a gradient overlay below the text to make the text clearer in case the thumbnail is too light.
GIPHY App Key not set. Please check settings