简体   繁体   中英

How to make a gradient div in the shape of a trapezoid?

  • I'm trying to make this image I found on Dribbble using Codepen.
  • I've made the bike, but I can't get the light.
  • I've tried several times using overflow:hidden and it isn't working.
  • Do you have an idea of how to do this?

What I'm trying to do is :

在此输入图像描述

web here

Use triangles instead of a trapezium. You can cover the tip of said triangle with the light.

Tweak the numbers, but here's the general idea: http://jsfiddle.net/cZQmH/ http://jsfiddle.net/cZQmH/1/ (added browser compatibility)

<div class="light">
    <div class="top triangle"></div>
    <div class="bottom triangle"></div>
</div>

Where each "triangle" is actually just covering up the corner

.light { /* Just a big box. Where the magic happens*/
  padding:50px 0px;
  position: absolute;
  background: -webkit-linear-gradient(left, rgba(255,255,255,1),rgba(255,255,255,1), rgba(255,255,0,0)); /* should add other compatibility things */
  height: 75px;
  width:200px;
}

.triangle {
  width: 50px;
  position: absolute;
  left: 0;
}

.top { /*Covers top corner*/
  top:0;
  border-top: 100px solid #ff0; 
  border-right: 100px solid transparent;
}


.bottom { /* Covers bottom corner */
  bottom:0;
  border-bottom: 100px solid #ff0; 
  border-right: 100px solid transparent;    
}

The final CSS is a bit verbose, but if you're using LESS or SASS it should come out pretty cleanly.

What it comes down to is not using the border itself, but using a gradient negative image. You could experiment with the border-image it's just as well supported as Gradients but I came up with this solution first. It does look like it's possible however

Also: could you post a link to the completed bike? I'd be curious to see how you handle all of those curves.

html

<div class="light">
<div id="trapezoid"></div>
<div id="trapezoid-two"></div>
</div>

css

   .light{
    background-color: yellow;
    width: 400px;
    height: 200px;
    }
    #trapezoid {
            top: 50px;
            left: 150px;
            position: absolute;
            border-top: 40px solid transparent;
            border-bottom: 40px solid transparent;
            height: 16px;
            border-right-width: 160px;
            border-right-style: dashed;
            border-right-color: white;
    }
    #trapezoid:before {
    content: '';
    position: absolute;
    width: 40px;
    height: 10px;
    -webkit-box-shadow: 3px 0 130px 80px white;
    left: 160px;
    border-radius: 50px;
    background-color: white;
    }
    #trapezoid-two{
            top: 60px;
            left: 200px;
            position: absolute;
            border-top: 35px solid transparent;
            border-bottom: 35px solid transparent;
            height: 6px;
            border-right-width: 160px;
            border-right-style: solid;
            border-right-color: white;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM