【问题标题】:Creating a transparent CSS triangle over a background image在背景图像上创建一个透明的 CSS 三角形
【发布时间】:2015-04-07 04:58:47
【问题描述】:

我在背景图像上做了一个分隔线,其中有一条线,中间有一个指针,表示看它下面。这只是一条线,所以分隔线不是实心的。当我制作分隔线时,父级的边框穿过三角形,因为背景是透明的。

看看我要解释的内容:

我希望三角形将那条线隐藏在中间。这就是我创建分隔线的方式:

<div class="splash">
    <div class="splash-divider">
    </div>
</div>

.splash {
    background: url("https://unsplash.imgix.net/photo-1416339442236-8ceb164046f8?q=75&fm=jpg&s=8eb83df8a744544977722717b1ea4d09");
    height: 200px;

}

.splash-divider {
    position: relative;
    margin: 20px auto 0 auto;
    width: 50%;
    height: 30px;
    border-bottom: 1px solid #ffffff;
    box-shadow: 0px 2px 0px rgba(0, 0, 0, 0.15);
}

.splash-divider:after {
    content: "";
    position: absolute;
    top: 15px;
    left: 50%;
    width: 30px;
    height: 30px;
    border-left: 1px solid #ffffff;
    border-bottom: 1px solid #ffffff;
    box-shadow: -2px 2px 0 rgba(0, 0, 0, 0.15);
    transform: rotate(-45deg) translate(-50%, -50%);
    -webkit-transform: rotate(-45deg) translate(-50%, -50%);
    -ms-transform: rotate(-45deg) translate(-50%, -50%);
    -moz-transform: rotate(-45deg) translate(-50%, -50%);
    -o-transform: rotate(-45deg) translate(-50%, -50%);
}

如您所见,父级包含背景图像。如果它只是一种颜色,那就太简单了。

这是fiddle

编辑

解决了!这是工作小提琴:http://jsfiddle.net/a9fkh0tp/1/

【问题讨论】:

  • 您将无法掩盖白边线(至少不能让它看起来不错),因为您使用的是背景图像并掩盖了线条以使其看起来像背景几乎是不可能的。你需要完全重组你正在做的事情,或者只是按照@sev 的建议去做。
  • @Harry 不,这不一样

标签: html css css-shapes


【解决方案1】:

HTML:

<div class="line-separator">
    <div class="side-line"> </div>
    <div class="triangle"> </div>
    <div class="side-line"> </div>
</div>

CSS:

.side-line {
    display: inline-block;
    border-top: 1px solid black;
    width: 20%;
}

.triangle {
    display: inline-block;
    height: 7px;
    width: 7px;
    transform: rotate(45deg);
    transform-origin: center center;
    border-top: 1px solid black;
    border-left: 1px solid black;
    margin: 0 -3px -3px;
}

现场演示: http://jsfiddle.net/85saaphw/

如果您想让三角形倒置,只需将 transform: rotate(45deg) arg 更改为 225deg。

【讨论】:

    【解决方案2】:

    有可能,看现场演示:http://jsfiddle.net/a9fkh0tp/1/

    .splash {
      background: url("https://unsplash.imgix.net/photo-1416339442236-8ceb164046f8?q=75&fm=jpg&s=8eb83df8a744544977722717b1ea4d09");
      height: 200px;
    }
    .splash-divider {
      position: relative;
      margin: 20px auto 0 auto;
      width: 50%;
      height: 30px;
      border-bottom: 1px solid transparent;
    }
    .splash-divider:after {
      content: "";
      position: absolute;
      top: 15px;
      left: 50%;
      width: 30px;
      height: 30px;
      border-left: 1px solid #ffffff;
      border-bottom: 1px solid #ffffff;
      box-shadow: -2px 2px 0 rgba(0, 0, 0, 0.15);
      transform: rotate(-45deg) translate(-50%, -50%);
    }
    .splash-divider span:before,
    .splash-divider span:after {
      position: absolute;
      top: 0;
      display: inline-block;
      content: "";
      width: 50%;
      height: 30px;
      border-bottom: 1px solid #fff;
      box-shadow: 0px 2px 0px rgba(0, 0, 0, 0.15);
    }
    .splash-divider span:before {
      left: -28px;
    }
    .splash-divider span:after {
      right: -16px;
    }
    <div class="splash">
      <div class="splash-divider"><span></span></div>
    </div>

    这个想法是将单行分成两部分(左+右)。为此,请将&lt;span&gt; 添加到&lt;div&gt; 中,这样&lt;div class="splash-divider"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt; 我们就可以玩更多了。

    【讨论】:

      【解决方案3】:

      您也可以使用带有伪元素的方法。

      .divider {
        padding:1em;
        transform:rotate(45deg);
        width:0;
        margin:auto;
        border:2px white solid;
        border-top:none;
        border-left:none;
        position:relative;
        box-shadow:1px 1px 1px white;
      }
      .divider:before, div:after {
       content:'';
        width:2000px;
        border-bottom:2px solid white;
        position:absolute;
      
      
      }
      .divider:before {
        transform-origin:top left;
        bottom:1.9em;
        left:2em;  
        transform:rotate(-45deg);  
        box-shadow:1px 1px 1px white;
      }
      .divider:after {
        transform-origin:top left;
        left:0.05em;
        top:2.1em;
        transform:rotate(135deg);
        box-shadow:1px -1px 1px white;
      }
      html {
      min-height:100%;
        background:gray;
      background:linear-gradient(to bottom left, gray, yellow,purple, gray, yellow,purple, gray, yellow,purple);
        }
      &lt;div class="divider"&gt;&lt;/div&gt;

      您可以在http://codepen.io/gc-nomade/pen/raYGyO 中使用它...添加半径、变换等:)

      【讨论】:

        猜你喜欢
        • 2013-06-03
        • 2013-05-08
        • 2012-07-07
        • 2017-09-17
        • 2013-03-04
        • 2023-04-06
        • 2016-03-04
        • 1970-01-01
        • 2012-04-10
        相关资源
        最近更新 更多