代码之家  ›  专栏  ›  技术社区  ›  GrofMonteCristo

如何创建背景为矩形的六边形?

  •  0
  • GrofMonteCristo  · 技术社区  · 8 年前

    我创建的示例就像图片上的一样(全屏运行截取的代码),但我问这是否是更好的方法。若我缩放网页,我可以看到形状的边界,所以我可以在一个对象中创建六边形,并在背景中添加矩形。

    谢谢你的建议! 实例 enter image description here

    .arrow-left {
        margin-top: 40px;
        width: 0;
        height: 0;
        border-top: 160px solid transparent;
        border-bottom: 160px solid transparent;
        border-right:80px solid white;
        vertical-align: top;
    }
    
    .backwhite{
        margin-top: 40px;
        width: 50%;
        height:320px;
        background-color: #FFFFFF;
        color:#000000;
        vertical-align: top;
    
    }
    
    .arrow-left_yellow {
    
        width: 0;
        height: 0;
        border-top: 160px solid transparent;
        border-bottom: 160px solid transparent;
        border-right:80px solid #FFAA00;
        float:right;
    
    }
    .arrow-right_yellow {
        width: 0;
        height: 0;
        border-top: 200px solid transparent;
        border-bottom: 200px solid transparent;
        border-left: 100px solid #FFAA00;
    
    }
    .cube {
        width: 250px;
        height: 400px;
        background-color: #FFAA00;
    }
    
    .backwhite, .arrow-left,.arrow-left_yellow ,.arrow-right_yellow,.cube{
        display: inline-block;
    }
    
    .backwhite-text{
        float:left;
        width: 80%;
    }
    
    .backwhite-text >h1{
        color:#000000;
        text-align: center;
    }
    .backwhite-text>p{
       color:#000000;
    }
    
    
    
    .trapezoid {
        vertical-align: top;
        margin: 40px 0px;
        position: relative;
        display: inline-block;
        width: 20px;
        height: 320px;
        background-color: #FFAA00;
        color: white;
        font-size: 2rem;
    }
    .trapezoid::before {
        content: "";
        position: absolute;
        top: -40px;
        border-top: 40px solid transparent;
        border-left: 0 px solid transparent;
        border-right: 20px solid #FFAA00;
        border-bottom: 111px solid #FFAA00;
    }
    .trapezoid::after {
        content: "";
        position: absolute;
        bottom: -40px;
        border-bottom: 40px solid transparent;
        border-left: 0px solid transparent;
        border-right: 20px solid #FFAA00;
        border-top: 111px solid #FFAA00;
    }
    
    
    #single-project{
        padding-top: 310px;
    }
    <div id="single-project">
     <div class="arrow-left"></div><div class="backwhite"> <div class="backwhite-text"><h1>H1 H1 H1 H1</h1><p>TextText</p></div><div class="arrow-left_yellow"></div></div><div class="trapezoid"></div><div class="cube"></div><div class="arrow-right_yellow"></div>
    
    
    </div>
    1 回复  |  直到 7 年前
        1
  •  0
  •   Zsolt Simon    8 年前

    对您可以创建六边形对象并在背景中添加矩形。使用z-index属性。检查这篇文章: https://css-tricks.com/almanac/properties/z/z-index/