S1njack
S1njack

Reputation: 35

How to use :hover on two of the same elements

I'm experimenting with CSS animations and I've made a navbar where the tags have an outline and a wave effect is made by animating another tag with a clip-path polygon. I am currently trying to make it so, on hover, the inner element's height increase to completely fill the outline. But I don't know how to use :hover with two elements of the same tag. I tried .nthChild but it didn't seem to work. It could be my transition though. Also, run snippet in fullscreen as I haven't made it responsive. Thanks :)

* {
  margin: 0;
  padding: 0;
  pointer-events: all;
}

nav {
  align-items: center;
  background-color: black;
  display: flex;
  justify-content: space-around;
  min-height: 8vh;
}

.logo{
  position: relative;
}

.logo h2 {
  font-family: 'Poppins', sans-serif;
  color: #fff;
  font-size: 32px;
  letter-spacing: 5px;
  position: absolute;
  text-transform: uppercase;
  transform: translate(-50%, -50%);
}
.logo h2:nth-child(1)
{
  color: transparent;
  -webkit-text-stroke: 1px #03a9f4;
}
.logo h2:nth-child(2)
{
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}

.nav-links{
  display: flex;
  justify-content: space-around;
  position: relative;
  width: 50%
}

.nav-links li{
  list-style: none;
}

.nav-links a {
  color: rgb(238, 238, 238);
  font-family: 'Poppins', sans-serif;
  font-size: 30px;
  font-weight: bold;
  letter-spacing: 3px;
  position: absolute;
  text-decoration: none;
  text-transform: uppercase;
  transform: translate(-50%,-50%);
  transition: height 4s;
}

.nav-links a:nth-child(1) {
  color: transparent;
  -webkit-background-clip: text;
  -webkit-text-stroke: 1px #03a9f4;
  overflow: hidden;
}

.nav-links a:nth-child(2) {
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
  overflow: hidden;

}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}
<!DOCTYPE html>
<html lang="en">

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

<body>

  <nav class="nav">
    <div class="logo">
      <h2>Nav</h2>
      <h2>Nav</h2>
    </div>
    <ul class="nav-links">
      <li>
         <a id='outline' href="./index.html"><h3>Home</h3></a>
          <a id='wave' href="./index.html"><h3>Home</h3></a>
       </li>
       
      <li>
        <a id='outline' href="./contact.html"><h3>Contact</h3></a>
        <a id='wave'href="./contact.html"><h3>Contact</h3></a>
      </li>

      <li>
        <a id='outline' href="./canvas.html"><h3>Visualizer</h3></a>
        <a id='wave'href="./canvas.html"><h3>Visualizer</h3></a>
      </li>
    </ul>
  </nav>

  <script src="app.js" charset="utf-8"></script>

</body>

<main></main>

</html>

Upvotes: 3

Views: 84

Answers (1)

Rounin
Rounin

Reputation: 29511

In this case you will need two selectors:

  • .nav-links a:nth-child(1):hover + a
  • .nav-links a:nth-child(2):hover

You can combine them with a comma, like this:

.nav-links a:nth-child(1):hover + a,
.nav-links a:nth-child(2):hover {
  animation: none;
}

Working Example:

* {
  margin: 0;
  padding: 0;
  pointer-events: all;
}

nav {
  align-items: center;
  background-color: black;
  display: flex;
  justify-content: space-around;
  min-height: 8vh;
}

.logo{
  position: relative;
}

.logo h2 {
  font-family: 'Poppins', sans-serif;
  color: #fff;
  font-size: 32px;
  letter-spacing: 5px;
  position: absolute;
  text-transform: uppercase;
  transform: translate(-50%, -50%);
}
.logo h2:nth-child(1)
{
  color: transparent;
  -webkit-text-stroke: 1px #03a9f4;
}
.logo h2:nth-child(2)
{
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
}

.nav-links{
  display: flex;
  justify-content: space-around;
  position: relative;
  width: 50%
}

.nav-links li{
  list-style: none;
}

.nav-links a {
  color: rgb(238, 238, 238);
  font-family: 'Poppins', sans-serif;
  font-size: 30px;
  font-weight: bold;
  letter-spacing: 3px;
  position: absolute;
  text-decoration: none;
  text-transform: uppercase;
  transform: translate(-50%,-50%);
  transition: height 4s;
}

.nav-links a:nth-child(1) {
  color: transparent;
  -webkit-background-clip: text;
  -webkit-text-stroke: 1px #03a9f4;
  overflow: hidden;
}

.nav-links a:nth-child(2) {
  animation: animate 3s ease-in-out infinite;
  color: #03a9f4;
  overflow: hidden;

}

@keyframes animate
{
    0%,100%
  {
    clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
  }
  50%
  {
    clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
  }
}

.nav-links a:nth-child(1):hover + a,
.nav-links a:nth-child(2):hover {
  animation: none;
}
<!DOCTYPE html>
<html lang="en">

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

<body>

  <nav class="nav">
    <div class="logo">
      <h2>Nav</h2>
      <h2>Nav</h2>
    </div>
    <ul class="nav-links">
      <li>
         <a id='outline' href="./index.html"><h3>Home</h3></a>
          <a id='wave' href="./index.html"><h3>Home</h3></a>
       </li>
       
      <li>
        <a id='outline' href="./contact.html"><h3>Contact</h3></a>
        <a id='wave'href="./contact.html"><h3>Contact</h3></a>
      </li>

      <li>
        <a id='outline' href="./canvas.html"><h3>Visualizer</h3></a>
        <a id='wave'href="./canvas.html"><h3>Visualizer</h3></a>
      </li>
    </ul>
  </nav>

  <script src="app.js" charset="utf-8"></script>

</body>

<main></main>

</html>

Upvotes: 2

Related Questions