代码之家  ›  专栏  ›  技术社区  ›  Lukas Bach

转换节点上的css边框

  •  5
  • Lukas Bach  · 技术社区  · 8 年前

    我有一个html节点,在这个节点上我设置了一个非常胖的边框和一个通过css缩放和旋转的转换。由于某种原因,在转换之后,在边界的外部,节点本身颜色中非常细的附加边界出现在外部,就好像节点的背景延伸到边界下面,并且边界不够大,无法覆盖背景颜色一样。

    .transform {
      transform: scale(1, .7) rotate(45deg);
    }
    .container {
      width: 100px;
      height: 100px;
      background-color: chocolate;
      border: 20px solid white;
    }
    <div class="container">proper white border</div>
    <div class="container transform">slim orange border around actual white border</div>

    请注意,在顶部框中,由于边框的颜色设置为白色,因此无法注意到边框的设置,但在底部框中,白色边框由框颜色中的另一个细边框包围。

    有什么可以防止这种情况发生吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Temani Afif    8 年前

    你可以调整 background-clip 属性来避免这种情况。默认情况下,该值设置为 border-box :

    边框

    背景延伸到边界的外缘 (但在边界的下面是z顺序)。

    .transform {
      transform: scale(1, .7) rotate(45deg);
    }
    .container {
      width: 100px;
      height: 100px;
      background-color: chocolate;
      border: 20px solid white;
      background-clip:content-box; /*OR padding-box*/
    }
    <div class="container">proper white border</div>
    <div class="container transform">slim orange border around actual white border</div>

    padding-box content-box 背景不会延伸到边界。

        2
  •  0
  •   Keyur Patel    8 年前
    Use background-clip: padding-box;
    

    .transform {
      transform: scale(1, .7) rotate(45deg);
    }
    .container {
      width: 100px;
      height: 100px;
      background-color: chocolate;
      border: 20px solid white;
      background-clip: padding-box;
    }
    <div class="container">proper white border</div>
    <div class="container transform">slim orange border around actual white border</div>