Search code examples
csshovertransitionslidebox-shadow

Box-shadow hover transition blinking


I'm trying to make box-shadow work like a slide animation over an img, by making the black background coming over from left to right. But I can't do it without this weird blinking problem. I've already looked for solutions around Stack Overflow.

I also tried it with a section instead of an img, but the result was the same.

Here's the JSFiddle demo

HTML

<section>
</section>

CSS

section {
  border:black 1px solid;
  width:500px;
  height:200px;
  transition:1s ease-out
}

section:hover{
  box-shadow:inset 500px 0 0 #222;
  float:left;
  transition:1s ease-out;
}

Solution

  • This appears to be a result of the way box-shadow is painted. Try another approach. Here's a flicker-free solution that thickens the left border instead of adding a box-shadow:

    div {
      position: relative;
      display: inline-block;
    }
    section {
      border: black 1px solid;
      width: 500px;
      height: 200px;
      transition: 1s ease-out;
      box-sizing: border-box;
    }
    
    div:hover section {
      border-left-width: 500px;
      float: left;
      transition: 1s ease-out;
    }
    
    section~* {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
      color: #888;
    }
    <div class="prog">
      <section></section>
      <p>Here's some text content.</p>
    </div>

    https://jsfiddle.net/k5kznmvL/