Home > front end >  My ::after hover effect is covering my Anchor text
My ::after hover effect is covering my Anchor text

Time:05-15

Im trying to make a simple hover effect, but when i hover on the div, the ::AFTER background color covers the text but I want the text to come on top of everything , I tried adjusting z-index of the anchor but that didnt help.

*{
    margin: 0;
    padding: 0;
    text-decoration: none;
    list-style: none;
    box-sizing: border-box;

}

body{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.box{

width: 300px;
height: 150px;
background-color: blueviolet;
cursor: pointer;
}

a{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    height: 100%;width: 100%;
    color: black;

}

a::after{
    position: absolute;
    content: "";
    width: 100%;
    height: 100%;
    background-color: #000000ad;
    left: 0;
    top: 0;
    z-index: -1;
}

a:hover{
    color: white;
}

a:hover::after{
    z-index: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Hover</title>
</head>
<body>
    <div >
        <a href="">Home</a>
    </div>
</body>
</html>

CodePudding user response:

you can fix that by separating the text with spans like

<a href=""><span style="z-index: 100;">Home</span></a>

CodePudding user response:

I Think You just want to change background color of anchor while hovering to anchor So I think you no need to use to after pseudo class. You can just change background color just by hovering as I showed in code. Try this out. If I not understand you question please apologize me.

*{
    margin: 0;
    padding: 0;
    text-decoration: none;
    list-style: none;
    box-sizing: border-box;

}

body{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.box{

width: 300px;
height: 150px;
background-color: blueviolet;
cursor: pointer;
}

a{
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    height: 100%;width: 100%;
    color: black;

}



a:hover{
    color: white;
    background-color: #000000ad;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Hover</title>
</head>
<body>
    <div >
        <a href="">Home</a>
    </div>
</body>
</html>

CodePudding user response:

You can add the background-color as part of the a:hover

a:hover {
  color: white;
  background-color: #000000ad;
}

Working snippet:

* {
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  width: 300px;
  height: 150px;
  background-color: blueviolet;
  cursor: pointer;
}

a {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  height: 100%;
  width: 100%;
  color: black;
}

a::after {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  z-index: -1;
}

a:hover {
  color: white;
  background-color: #000000ad;
}

a:hover::after {
  z-index: 1;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <title>Hover</title>
</head>

<body>
  <div >
    <a href="">Home</a>
  </div>
</body>

</html>

  • Related