Nikola Pavicevic
Nikola Pavicevic

Reputation: 23500

Fill entire angled li tag on hover

I made ul (for site navigation) with the left and right borders at an angle.

I'm struggling with requirement that :hover selector fill entire li.

Is there a way to achieve that or do I need to change my approach?

section ul {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: left;
  list-style-type: none;
  margin: .5em 1em;
}
li {
  margin: 0;
  border-radius: 1px;
  position: relative;
  padding: 0.3em 0.8em;
  cursor: pointer;
}
li:nth-child(2n+1)::before {
  content: "";
  border-right: 2px solid black;
  transform: rotate(20deg);
  display: block;
  position: absolute;
  top: -2px;
  left: -5px;
  width: 2px;
  height: 120%;
}
li:nth-child(2n)::after {
  content: "";
  border-right: 2px solid black;
  transform: rotate(-20deg);
  display: block;
  position: absolute;
  top: -2px;
  left: -5px;
  width: 2px;
  height: 120%;
}
li:hover {
  background-color: black;
  color: white;
}
<section>
  <ul>
    <li><a>Link 1</a></li>
    <li><a>Link 2</a></li>
    <li><a>Link 3</a></li>
    <li><a>Link 4</a></li>
  </ul>
</section>

Upvotes: 2

Views: 86

Answers (3)

Mohsen TOA
Mohsen TOA

Reputation: 809

or you can try the way you wrote your codes.

used css shape:

section ul {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: left;
  list-style-type: none;
  margin: .5em 1em;
}
li {
  margin: 0;
  border-radius: 1px;
  position: relative;
  padding: 0.3em 0.8em;
  cursor: pointer;
  z-index:30;
}
li:nth-child(2n+1)::before {
  content: "";
  border-left: 2px solid red;
  display: block;
  position: absolute;
  top: -2px;
  left: -3px;
  width: 100%;
  height: 120%;
  transform:skew(-20deg);
  z-index:-1;
}
li:nth-child(2n+1)::after {
  content: "";
  position: absolute;
  top: -2px;
  left: -10px;
border-bottom: 41px solid rgba(255, 0, 0, 0.6);
border-left: 15px solid transparent;
border-right: 15px solid transparent;
height: 0;
width: 120%;
opacity:0;
z-index:-2;
  transition:all 0.5s ease-in-out;
}
li:nth-child(2n)::before {
  content: "";
  border-right: 2px solid black;
  transform: rotate(-20deg);
  display: block;
  position: absolute;
  top: -2px;
  left: -5px;
  width: 2px;
  height: 120%;
}
li:nth-child(2n)::after {
  content: "";
  position: absolute;
  top: -2px;
  left: -12px;
border-top: 40px solid rgba(0, 0, 0, 0.6);
border-left: 15px solid transparent;
border-right: 15px solid transparent;
height: 0;
width: 125%;
opacity:0;
z-index:-2;
  transition:all 0.5s ease-in-out;
}
li:hover{
color: white!important;
}
li:hover::after {
opacity:1;
}

Upvotes: 1

isherwood
isherwood

Reputation: 61114

I would just update the styles for your pseudo-elements on hover:

section ul {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: left;
  list-style-type: none;
  margin: .5em 1em;
}

li {
  margin: 0;
  border-radius: 1px;
  position: relative;
  padding: 0.3em 0.8em;
  cursor: pointer;
}

li:hover a {
  color: white;
}

li:nth-child(2n)::before {
  content: "";
  border-right: 2px solid black;
  transform: rotate(-20deg);
  display: block;
  position: absolute;
  top: -2px;
  left: -5px;
  width: 2px;
  height: 120%;
}

li:nth-child(2n+1)::before {
  content: "";
  border-right: 2px solid black;
  transform: rotate(20deg);
  display: block;
  position: absolute;
  top: -2px;
  left: -5px;
  width: 2px;
  height: 120%;
}

li:nth-child(2n):hover::before {
  background-color: black;
  top: -2px;
  left: -6px;
  width: calc(100% + 8px);
  clip-path: polygon(0 0, 100% 0, 85% 100%, 15% 100%);
  transform: none;
  z-index: -1;
}

li:nth-child(2n+1):hover::before {
  background-color: black;
  top: -2px;
  left: -6px;
  width: calc(100% + 8px);
  clip-path: polygon(15% 0, 85% 0, 100% 100%, 0 100%);
  transform: none;
  z-index: -1;
}
<section>
  <ul>
    <li><a>Link 1</a></li>
    <li><a>Link 2</a></li>
    <li><a>Link 3</a></li>
    <li><a>Link 4</a></li>
  </ul>
</section>

Upvotes: 2

Temani Afif
Temani Afif

Reputation: 274024

I think you need a different approach, here is an idea using pseudo element and skew:

section ul {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  list-style-type: none;
  margin: .5em 1em;
}

li {
  position: relative;
  padding: 0.3em 0.8em;
  cursor: pointer;
  z-index: 0;
}

li:before,
li:after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  bottom: 0;
  width: 70%;
}

li:before {
  left: -1px;
  border-left: 2px solid #000;
}
li:after {
  right: -1px;
  border-right: 2px solid #000;
}

li:after,
li:nth-child(odd):before {
  transform: skewX(-15deg)
}
li:before,
li:nth-child(odd):after {
  transform: skewX(15deg)
}

li:hover {
  color: #fff;
}

li:hover:before,
li:hover:after {
  background: #000;
}
<section>
  <ul>
    <li><a>Link 1</a></li>
    <li><a>Link 2</a></li>
    <li><a>Link 3</a></li>
    <li><a>Link 4</a></li>
  </ul>
</section>

Upvotes: 5

Related Questions