【问题标题】:Best way to make this (trapezoid) shape in css3在css3中制作这个(梯形)形状的最佳方法
【发布时间】:2016-07-12 19:54:45
【问题描述】:

我有这个带有伪元素的形状:

https://jsfiddle.net/6gf1m3j5/

body {
    margin: 0;
    background:#ccc;
}

#octagon-left {
    background: red none repeat scroll 0 0;
    height: 60px;
    width: 100%;
}
#octagon-left::before {
    border-left: 100px solid white;
    border-top: 60px solid red;
    content: "";
    height: 0;
    margin: 0;
    position: absolute;
    top: 0;
    width: 40px;
}

#octagon-left::after {
    border-right: 100px solid white;
    border-top: 60px solid red;
    content: "";
    height: 0;
    margin: 0;
    position: absolute;
    right: 0;
    top: 0;
    width: 40px;
}

这里的问题是左右角的白色。

如果我这样做,左边框:100px 纯透明;以红色为主。

我想要这样:

我该怎么做?

非常感谢您的支持,谢谢。

【问题讨论】:

    标签: css shape pseudo-element


    【解决方案1】:

    #octagon
    {
      background-color: red;
      height: 60px;
      margin: 0 auto;
      position: relative;
      width: calc(100% - 120px);
    }
    #octagon:before,
    #octagon:after
    {
      background-color: red;
      content: "";
      height: 100%;
      left: 0;
      position: absolute;
      top: 0;
      width: 100%;
    }
    #octagon:before
    {
      transform: translateX(30px) skewX(-45deg);
    }
    #octagon:after
    {
      transform: translateX(-30px) skewX(45deg);
    }
    <div id="octagon"></div>

    【讨论】:

    • 感谢您的支持。
    • 我这里有个小问题,如果我将 100%(full width) 添加到:#octagon { width: 100%;这行不通,我需要像我的例子一样,但要透明。 jsfiddle.net/6gf1m3j5
    • @Thenothing 你还有其他规格吗?高度是固定的还是什么?
    • 嗨 Mohit Bhardwaj,首先,感谢您的参与,大约 60px 的高度就可以了。
    • @Thenothing 谢谢。请检查更新的代码是否适合您。在这里,我使用了 css calc 函数并根据 div 的高度定义了 width 和 translateX 值。因此,如果高度为 60px,则需要使用 width: calc(100% - 120px); 和 translateX 值作为高度的一半,即 30px。
    【解决方案2】:

    像这样更改你的代码并告诉我

    body {
        margin: 0;
        background:#ccc;
    }
    
    #octagon-left {
        background: red none repeat scroll 0 0;
        height: 60px;
        width: 100%;
    }
    #octagon-left::before {
        border-left: 100px solid #ccc;/*Change from white to #ccc*/
        border-top: 60px solid red;
        content: "";
        height: 0;
        margin: 0;
        position: absolute;
        top: 0;
        width: 40px;
    }
    
    #octagon-left::after {
        border-right: 100px solid #ccc;/*Change from white to #ccc*/
        border-top: 60px solid red;
        content: "";
        height: 0;
        margin: 0;
        position: absolute;
        right: 0;
        top: 0;
        width: 40px;
    }
    <div id="octagon-left">
    
    </div>

    【讨论】:

    • 我必须在图像背景之后设置这个形状,这不起作用。
    • 看来我没有理解您的要求。您想要与您的图像相同的东西吗?
    • 您好,谢谢您的留言,颜色#ccc只是一个例子,我必须设置一个像背景一样的图像。
    • 哦,好吧,应该换一种方式。你已经有了答案?
    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    相关资源
    最近更新 更多