I want to position a css triangle as a background

后端 未结 3 576
萌比男神i
萌比男神i 2021-01-07 05:51

I want to build the following layout:

Preferable i want only use css for that. But even with an background-image i wouldn\'t know how to build it. I searche

相关标签:
3条回答
  • 2021-01-07 06:34

    This should get you close, and illustrates a CSS only approach:

    * {
      margin: 0;
      padding: 0
    }
    
    body {
      background: #ccc;
      min-height: 500px;
    }
    
    div {
      width: 0;
      height: 0;
      margin: 0px auto;
      border: 200px solid transparent;
      border-top-color: grey;
    }
    
    a {
      display: block;
      background: blue;
      color: white;
      padding: 5px 10px;
      width: 200px;
      margin: 0px auto;
      position: relative;
      top: -200px;
      text-align: center;
      text-decoration: none;
    }
    <div></div>
    <a href="#">link</a>

    0 讨论(0)
  • 2021-01-07 06:49

    Try this one, but still need some work on the responsive part.

    .box{
      position: relative;
      width: 100%;
      max-width: 600px;
      background: #ccc;
      min-height: 300px;
    }
    .box:before {
      width: 0; 
      height: 0;
      content: "";
      position: absolute;
      z-index: 0;
      top: 0;
      left: 0;
      border-left: 300px solid transparent;
      border-right: 300px solid transparent;
      border-top: 180px solid #555;
    }
    
    .box .content{
      z-index: 10;
      position: relative;
      color: #fff;
      text-align: center;
      padding-top: 40px;
    }
    
    h1, h2{
      margin: 0;
      padding: 0;
    }
    h2{
      margin-bottom: 80px;
    }
    
    .btn{
      background: #f00;
      color: #fff;
      display: inline-block;
      padding: 5px 10px;
      text-align: center;
      text-decoration: none;
      min-width: 200px;
      font-size: 20px;
    }
    <div class="box">
      <div class="content">
      <h1>Headline</h1>
      <h2>Headline</h2>
        
        <a href="#" class="btn">CTA</a>
      </div><!--// end .content -->
    </div><!--// end .box -->

    0 讨论(0)
  • 2021-01-07 06:53

    You can set 2 light gradients on top of the darker background.

    They overlap each other and leave only the remaining triangle darker

    div {
        width: 400px;
        height: 200px;
        border: solid 1px green;
        background: linear-gradient(to top left, lightgreen 50%, transparent 50%),
        linear-gradient(to top right, lightgreen 50%, transparent 50%), green;
    }
    <div></div>

    0 讨论(0)
提交回复
热议问题