How to triangle top and bottom border?

前端 未结 4 935
误落风尘
误落风尘 2021-01-20 10:06

As you can see in the image below, I am trying to warp or triangle my div from bottom and top, but I have no idea how to do it. I just tried a couple of times to do it, but

相关标签:
4条回答
  • 2021-01-20 10:38

    This can be done using CSS triangles on the ::before and ::after pseudo-elements! I've colored them brightly so you can tell what's happening, but it should be somewhat easy to get these to look they way you want.

    body {
      background: lightblue;
    }
    
    .block {
      background-image: linear-gradient(to right, #314b56, #283b44, #1f2c32, #161e21, #0a0f11);
      border: 1px solid #fff;
      width: 300px;
      height: 150px;
      margin: 30px;
      position: relative;
    }
    
    .block::before,
    .block::after{
      display: block;
      content: '';
      position: absolute;
      border: 150px solid transparent;
    }
    
    .block::before {
      border-top-width: 0;
      border-bottom-width: 25px;
      border-bottom-color: red;
      top: -25px;
    }
    
    .block::after {
      border-bottom-width: 0;
      border-top-width: 25px;
      border-top-color: green;
      bottom: -25px;
    }
    <div class="block"></div>

    0 讨论(0)
  • 2021-01-20 10:42

    An idea using transformation and perspective where you will have the border, border-radius also the gradient:

    body {
      background: lightblue;
    }
    
    .block {
      overflow: hidden;
      width: 300px;
      height: 200px;
      margin: 20px;
      position: relative;
      z-index:0;
    }
    
    .block::before,
    .block::after {
      content: "";
      position: absolute;
      z-index:-1;
      border: 1px solid #fff;
      top: 0;
      bottom: 0;
      width: 50%;
      background-image: linear-gradient(to right, #314b56, #283b44, #1f2c32, #161e21, #0a0f11);
      background-size: 200% 100%;
    }
    
    .block::before {
      left: 0;
      border-right: 0;
      border-radius: 15px 0 0 15px;
      transform-origin: right;
      transform: perspective(100px) rotateY(-5deg);
    }
    
    .block::after {
      right: 0;
      border-left: 0;
      border-radius: 0 15px 15px 0;
      transform-origin: left;
      transform: perspective(100px) rotateY(5deg);
      background-position: right;
    }
    <div class="block"></div>

    You can also add the shadow and easily change the gradient:

    body {
      background: lightblue;
    }
    
    .block {
      overflow: hidden;
      width: 300px;
      height: 200px;
      margin: 20px;
      position: relative;
      z-index:0;
      filter:drop-shadow(0 0 5px #000);
    }
    
    .block::before,
    .block::after {
      content: "";
      position: absolute;
      z-index:-1;
      border: 1px solid #fff;
      top: 0;
      bottom: 0;
      width: 50%;
      background-image: linear-gradient(35deg, blue, red);
      background-size: 200% 100%;
    }
    
    .block::before {
      left: 0;
      border-right: 0;
      border-radius: 15px 0 0 15px;
      transform-origin: right;
      transform: perspective(100px) rotateY(-5deg);
    }
    
    .block::after {
      right: 0;
      border-left: 0;
      border-radius: 0 15px 15px 0;
      transform-origin: left;
      transform: perspective(100px) rotateY(5deg);
      background-position: right;
    }
    <div class="block"></div>

    0 讨论(0)
  • 2021-01-20 10:50

    Adjust the measurements to fit your exact shape requirements. This gives something close to what you are looking for.

    body{
    background:lightblue;;
    }
    .block{ position: 
    relative; width:200px; 
    height: 150px;
    margin: 20px 0;
    background: red;
    border-radius: 50% / 10%;
    background-image: linear-gradient(to right, #314b56, #283b44, #1f2c32, #161e21, #0a0f11);:
     }
    
    
    }
    .block:before
    { content: '';
    position: absolute;
    top: 20%; 
    bottom: 20%; 
    right: -5%; 
    left: -5%; 
    background: inherit;
    border-radius: 5% / 50%;
    }
    
    <div class="block"></div>
    
    0 讨论(0)
  • 2021-01-20 11:03

    You can do it with clip-path. There is a really simple tool that could help you: https://bennettfeely.com/clippy/.

    I've made an example for you with your content:

    body {
      background: lightblue;
    }
    
    .block {
      background-image: linear-gradient(to right, #314b56, #283b44, #1f2c32, #161e21, #0a0f11);
      border: 1px solid #fff;
      width: 300px;
      height: 150px;
      margin: 30px;
      -webkit-clip-path: polygon(100% 80%, 50% 100%, 0 80%, 0 20%, 51% 0, 100% 20%);
      clip-path: polygon(100% 80%, 50% 100%, 0 80%, 0 20%, 51% 0, 100% 20%);
    }
    <div class="block"></div>

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