×

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element.

The CSS pseudo-class is used to add some styling effect to the webpage. We can change or add a style when we hover over the button. 

If we want to apply this styling effect, we must use the following syntax:

Syntax-

a:hover::before {
    // CSS Property
}
a:hover::after {
    // CSS Property
}

Example 1

<!DOCTYPE html>
<html>
<head>
<title>
 :hover condition for a:before
 and a:after
</title>
<!-- Style to add hover condition -->
<style>
 a:hover::before {
 content: "Before -";
 }
 a:hover::after {
 content: "-after";
 }
</style>
</head>
<body>
<a href="#"> javaTpoint </a>
</body>
</html>

Output-

Before mouse pointer-

Hover condition for a:before and a:after in CSS

After mouse pointer-

Hover condition for a:before and a:after in CSS

Example 2

<!DOCTYPE html>
<html>
<head>
 <title>
 :hover condition for a:before
 and a:after
 </title>
 
 <style>
 a:hover::before {
 content: "Before -";
 background-color: red;
 }
 a:hover::after{
 content: "-after";
 background-color: red;
 }
 </style>
</head>
<body>
 <a href="#"> javaTpoint </a>
</body>
</html>

Output-

Before mouse pointer-

Hover condition for a:before and a:after in CSS

After mouse pointer-

Hover condition for a:before and a:after in CSS

Example 3

This effect is used before the hover effect.

HTML code-

<html>
<body>
 <div>
 <h1>CSS Button Hover Effects</h1>
 <p>with the :before pseudo-element</p>
 <div class="container">
 <p>Gradient</p>
 <button class="button-one gradient">Hover</button>
 <button class="button-two gradient">Hover</button>
 <button class="button-three gradient">Hover</button>
 <hr>
 <p>Clip-Path</p>
 <button class="button-four clip-path">Hover</button>
 <button class="button-five clip-path">Hover</button>
 <button class="button-six clip-path">Hover</button>
 </div>
 </div>
</body>
</html>

CSS code-

button {
font-size: 30px;
padding: 10px 30px;
margin: 10px;
border: 0;
cursor: pointer;
color: black;
text-transform: lowercase;
border-radius: 10px;
display: inline-block;
overflow: hidden;
transition: all 0.4s cubic-bezier(.86, .01, .15, .99);
}


button::before {
content: "";
position: absolute;
z-index: -1;
top: 0px;
left: 0px;
right: 0px;
bottom: 0;
}


button.gradient:hover {
color: white;
box-shadow: 0 5px 35px rgba(0, 0, 0, 0.6);
transform: translateY(-5px);
}




/************************************
GRADIENT BUTTONS 




Button One 


*/


.button-one {
background-color: #f9ff00;
transform: translateY(0);
}


.button-one::before {
background: linear-gradient(to right, #f9ff00, #00ff00);
-webkit-transform: scaleY(0);
transform: scaleX(0);
-webkit-transform-origin: 50% 100%;
transform-origin: 100% 0%;
transition: 0.4s cubic-bezier(.86, .01, .15, .99);
}


.button-one:hover::before {
transform: scaleX(1.1) scaleY(1.1);
}


.button-one:hover {
color: #333;
}


/* Button Two */


.button-two {
background: #efcfff;
transform: perspective(1px) translateZ(0);
transition: 0.4s cubic-bezier(.86, .01, .15, .99);
}


.button-two:before {
background: linear-gradient(to right, #efcfff, #af6fff, #efcfff);
transform: scaleX(0);
opacity: 0;
transform-origin: 50% 50%;
transition: 0.4s cubic-bezier(.86, .01, .15, .99);
}


.button-two:hover:before {
transform: scaleX(1) scaleY(1);
opacity: 1;
}


/*Button Three*/


.button-three {
-webkit-transform: perspective(1px) translateZ(0);
background: #08aa61;
transform: perspective(1px) translateZ(0);
position: relative;
transition-timing-function: cubic-bezier(.86, .01, .15, .99);
transition-duration: 0.4ss;
}


.button-three:before {
background: linear-gradient(to bottom, #88daa1, #08aa61);
transform: scaleY(0);
transform-origin: 0% 0%;
transition: 0.4s cubic-bezier(.86, .01, .15, .99);
}


.button-three:hover:before {
-webkit-transform: scaleY(1);
transform: scaleY(1);
}




/****************
CLIP-PATH EFFECTS


Button Four*/


.button-four {
background: #fff;
transform: perspective(1px) translateZ(0);
color: black;
transition: 0.4s;
}


.button-four::before {
background: rgb(255, 90, 20);
clip-path: inset(0 100% 0 0);
opacity: 1;
transition: 0.4s;
}


.button-four:hover::before {
clip-path: inset(0 0 0 0);
}


.button-four:hover {
color: white;
box-shadow: 0 0 0 10px rgba(250, 250, 250, 0.1)
}




/*Button Five*/


.button-five {
background: white;
color: black;
transform: perspective(1px) translateZ(0);
transition: 0.4s;
}


.button-five::before {
background: rgb(20, 120, 255);
clip-path: polygon(50% 0, 50% 0, 50% 50%, 50% 100%, 50% 100%, 50% 50%);
opacity: 1;
transition: 0.4s;
}


.button-five:hover::before {
clip-path: polygon(25% -70%, 75% -70%, 120% 50%, 75% 170%, 25% 170%, -20% 50%);
}


.button-five:hover {
color: white;
box-shadow: 0 0 0 10px rgba(250, 250, 250, 0.1)
}




/*Button Six*/


.button-six {
background: white;
color: black;
transition: 0.4s;
transform: perspective(1px) translateZ(0);
}


.button-six:hover {
color: white;
box-shadow: 0 0 0 10px rgba(250, 250, 250, 0.1)
}


.button-six::before {
clip-path: circle(0.5% at 50% 50%);
background: #9900cc;
transition: 0.4s;
opacity: 1;
}


.button-six:hover::before {
clip-path: circle(100% at 50% 50%);
}




/* just some extra styles */


* {
margin: 0;
color: white;
}


body {
background: #222232;
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
font-family: -apple-system, system-ui, sans-serif;
}


div.container {
padding: 20px;
border-radius: 20px;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1)
}


.container {
text-align: center;
}


h1 {
font-size: 50px;
}


p {
font-size: 25px;
font-style: italic;
border-radius: 20px;
margin-bottom: 20px;
}


hr {
border: 0;
background: rgba(255, 255, 255, 0.2);
margin: 10px;
height: 1px;
}

Output

Before mouse pointer-

Hover condition for a:before and a:after in CSS

After mouse pointer-

Hover condition for a:before and a:after in CSS

Example 4

HTML code-

<nav>
<ul>
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Work</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>

CSS code-

body{
margin: 0px;
padding: 0px;
background: #e74c3c;
font-family: 'Lato', sans-serif;
}


nav{
float: none;
clear: both;
width: 30%;
margin: 10% auto;
background: #eee;
}


nav ul {
list-style: none;
margin: 0px;
padding: 0px;
}


nav li{
float: none;
width: 100%;
}


nav li a{
display: block;
width: 100%;
padding: 20px;
border-left: 5px solid;
position: relative;
z-index: 2;
text-decoration: none;
color: #444;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}


nav li a:hover{ border-bottom: 0px; color: #fff;}
nav li:first-child a{ border-left: 10px solid #3498db; }
nav li:nth-child(2) a{ border-left: 10px solid #ffd071; }
nav li:nth-child(3) a{ border-left: 10px solid #f0776c; }
nav li:last-child a{ border-left: 10px solid #1abc9c; }


nav li a:after {
content: "";
height: 100%;
left: 0;
top: 0;
width: 0px;
position: absolute;
transition: all 0.3s ease 0s;
-webkit-transition: all 0.3s ease 0s;
z-index: -1;
}


nav li a:hover:after{ width: 100%; }
nav li:first-child a:after{ background: #3498db; }
nav li:nth-child(2) a:after{ background: #ffd071; }
nav li:nth-child(3) a:after{ background: #f0776c; }
nav li:last-child a:after{ background: #1abc9c; }


@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 100;
src: local('Lato Hairline'), local('Lato-Hairline'), url(http://themes.googleusercontent.com/static/fonts/lato/v6/boeCNmOCCh-EWFLSfVffDg.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 300;
src: local('Lato Light'), local('Lato-Light'), url(http://themes.googleusercontent.com/static/fonts/lato/v6/KT3KS9Aol4WfR6Vas8kNcg.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: local('Lato Regular'), local('Lato-Regular'), url(http://themes.googleusercontent.com/static/fonts/lato/v6/9k-RPmcnxYEPm8CNFsH2gg.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(http://themes.googleusercontent.com/static/fonts/lato/v6/wkfQbvfT_02e2IWO3yYueQ.woff) format('woff');
}

Output-

Before mouse pointer-

Hover condition for a:before and a:after in CSS

After mouse pointer-

Hover condition for a:before and a:after in CSS

Related Topics

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS Text-decoration

CSS Text-decoration: It is an essential property of CSS that is used to decorate the content of the text. It can add-on the lines above, under, and over the text....

3 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

Cascading Style Sheet

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

7 minutes read.