skip to Main Content

While the pseudo class is sliding out we can see that its left corners overlap the initial border of element. I wonder how I can fix this?

.item {
      width: 400px;
      height 100px;
      border: 2px solid black;
      border-radius: 15px;
      position: relative;
    }

    .item::before {
      content: "";
      left: 0;
      top: 0;
      border-radius: 15px;
      position: absolute;
      background: red;
      width: 100%;
      height: 100%;
      z-index: -1;
      transform: scaleX(0);
      transform-origin: left;
      transition: all 3s;
    }

    .item:hover::before {
      transform: scaleX(1);
    }
<div style="display: flex; justify-content: center;">
  <div class="item">
    <p>
          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Accusantium dolorum aliquam dolor nisi quaerat cum?
    </p>
  </div>
</div>

UPD: Also I have noticed that when the transition completes there are small gaps between pseudo class and the border of element. Any ideas on this?

2

Answers


  1. To fix the issue where the left corners of the pseudo class overlap the initial border of the element.

      .item {
        width: 400px;
        height: 100px;
        border: 2px solid black;
        border-radius: 15px;
        position: relative;
        overflow: hidden;
      }
    
      .item::before {
        content: "";
        left: 0;
        top: 0;
        border-radius: 15px;
        position: absolute;
        background: red;
        width: 100%;
        height: 100%;
        z-index: -1;
        transform: scaleX(0);
        transform-origin: left;
        transition: all 3s;
        clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 100%);
      }
    
      .item:hover::before {
        transform: scaleX(1);
      }
    <div style="display: flex; justify-content: center;">
      <div class="item">
        <p>
          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Accusantium dolorum aliquam dolor nisi quaerat cum?
        </p>
      </div>
    </div>
    Login or Signup to reply.
  2. Just use border-box instead

      .item {
        width: 400px;
        height: 100px;
        border: 2px solid black;
        border-radius: 15px;
        position: relative;
        overflow: hidden;
      }
    
      .item::before {
        content: "";
        left: 0;
        top: 0;
        border-radius: 15px;
        position: absolute;
        background: red;
        width: 100%;
        height: 100%;
        z-index: -1;
        transform: scaleX(0);
        transform-origin: left;
        transition: all 3s;
        clip-path:border-box;
      }
    
      .item:hover::before {
        transform: scaleX(1);
      }
    <div style="display: flex; justify-content: center;">
      <div class="item">
        <p>
          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Accusantium dolorum aliquam dolor nisi quaerat cum?
        </p>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search