【问题标题】:CSS diagonal div backgroundCSS对角div背景
【发布时间】:2017-05-03 10:25:58
【问题描述】:

对于我正在开发的网站,我需要在div 中包含一些对角线形状的边框。这些是我需要重新创建的主要示例。

double diagonal top border, triangle shaped

现在一直在网上搜索如何实现这一点,我的第一个想法也是使用::before。但是,如果没有绝对定位,我无法让它工作,这会弄乱整个页面。

这是我尝试实现的代码:

.slider-container{
  background-color: $blue;
  width: 100%;
  overflow: hidden;
  position: relative;
  .col-md-3{
    img{
      padding: 40px;
      width: 100%;
      max-width: 400px;
      margin: auto;
    }
  }
  &::before {
    background: red;
    bottom: 100%;
    content: '';
    display: block;
    height: 100%;
    position: absolute;
    right: 0;
    transform-origin: 100% 100%;
    transform: rotate(-15deg);
    width: 150%;
  }
}
<section id="slider">
    <div class="container-fluid">
        <div class="row slider-container">
            <div class="col-md-3">
                <p>imgae 1</p>
            </div>
            <div class="col-md-3">
                <p>imgae 2</p>
            </div>
            <div class="col-md-3">
                <p>imgae 3</p>
            </div>
            <div class="col-md-3">
                <p>imgae 4</p>
            </div>
        </div>
    </div>
</section>

注意:它在这里不起作用,但这是我得到的结果result

【问题讨论】:

  • 你能发布你尝试过的html和css吗?
  • 尝试实现变换倾斜属性,例如transform: skew(0deg,5deg);
  • 我会用我现在拥有的代码更新问题

标签: html css diagonal


【解决方案1】:

只需 css 并根据您的 div 大小进行一些调整,您就可以创建如下内容:

.myclass {
width: 100px;
height: 100px;
background: linear-gradient(45deg, black 0%, black 26%, transparent 26%), linear-gradient(-45deg, black 0%, black 27%, transparent 27%)
}

.myclass2 {
width: 100px;
height: 100px;
background: linear-gradient(-45deg, blue 0%, blue 27%, transparent 27%), linear-gradient(45deg, blue 0%, blue 26%, red 26%)
}
With transparency:
<div class="myclass">My content here</div>
<br/>
Not as easy with transparent:
<div class="myclass2">My content here</div>

编辑:刚刚在 chrome 中对此进行了测试,您可能需要针对旧版/其他浏览器的特殊线性渐变。

【讨论】:

  • 没关系,摆弄高度和其他一些属性,这就是我要找的,谢谢!
  • 我建议使用“到左下角”和“到右下角”作为线性渐变而不是角度。这样它会将对角线(?)调整为容器的高度。
【解决方案2】:

实现这一点的最简单方法可能是使用背景图像,但在较小的设备上效果可能会不一致。因此,您可能需要考虑使用硬停止渐变。

.grad {
  background: lightblue; /* For browsers that don't support gradients */
  background: -webkit-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
  background: -o-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
  background: -moz-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
  background: linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
  width: 100%;
  padding: 20px;
}
<div class="grad">
  <h1>Hard-stop gradient</h1>
  <p>Using this type of gradient, you can create an angled background without using a background image.</p>
</div>

使用它,您可以创建一个从 0% 到 15% 的渐变,两端都是白色,然后是一个从 15% 到 100% 的渐变,它是全黑的。这完全消除了褪色效果,为您提供有角度的背景。这可能也是最有效的方式,因为它只需要一行 CSS。

【讨论】:

    【解决方案3】:

    您可以使用clip-path

    body {
      margin: 0;
      padding: 0;
    
      color: #ffffff;
    }
    
    .wrapper {
      min-height: 100vh;
      min-width: 100vw;
      max-width: 100vw;
      width: 100vw;
      background-color: red;
    }
    
    .bg {
      min-height: 100vh;
      min-width: 100vw;
      background-color: blue;
      clip-path: polygon(80% 0, 100% 0, 100% 100%, 50% 100%);
    }
    <div class="wrapper">
      <div class="bg"></div>
    </div>

    【讨论】:

      【解决方案4】:

      这样的?

      div {
        background: yellow;
        height: 150px;
        overflow: hidden;
        position: relative;
        width: 300px;
      }
      
      div::before {
        background: red;
        bottom: 100%;
        content: '';
        display: block;
        height: 100%;
        position: absolute;
        right: 0;
        transform-origin: 100% 100%;
        transform: rotate(-15deg);
        width: 150%;
      }
      &lt;div&gt;&lt;/div&gt;

      【讨论】:

      • 确实是这样的,但是当这个 div 跟随另一个时,它不会像您的示例那样在顶部被切断,因为它在视口之外
      【解决方案5】:

      对我来说,linear-gradient 并不顺利... 我建议clip-pathsvg

      svg {
        display: block;
        width: 100%;
        height: 55px;      
      }
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 10" preserveAspectRatio="none">
        <polygon points="100 0 100 10 0 10" fill="white" />
      </svg>
      

      【讨论】:

        【解决方案6】:
        .arrow-right {
          width: 0; 
          height: 0; 
          border-top: 60px solid green;
          border-bottom: 60px solid transparent;
        
          border-left: 60px solid green;
        }
        

        【讨论】:

        • 我在一个类似的问题上看到过这个答案,但它似乎只关注对角线的 div,而不是两边都有类似三角形的 div。我似乎也无法完全弄清楚
        • 给了我一个奇怪的形状,在我的 div 的左侧有向下的边框?
        猜你喜欢
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        相关资源
        最近更新 更多