Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trapezoid in HTML with CSS3 Gradient applied to it

I want to make a trapezoid in HTML5. I know it can be done using border radius and a height of 0:

#trapezoid {
    border-bottom: 100px solid red;
    border-left: 50px solid transparent;
    border-right: 50px solid COLOR HERE;
    height: 0;
    width: 100px;
}

However, I want to apply a CSS3 gradient and the above method only allows solid colors.

The following style will make a parallelogram. But is there a way to skew only one of the sides, instead of both?

-webkit-transform: skew(20deg);
like image 953
Case Avatar asked Mar 19 '13 11:03

Case


People also ask

How do you make a trapezoid shape in CSS?

For that, we have to design a div element of HTML with a class property of CSS named trapezoid . After that, we use CSS border properties (left, top, and bottom), and we will use the default value of border-right for div to create a Trapezoid shape.

Can you do gradient in CSS?

CSS gradients let you display smooth transitions between two or more specified colors. CSS defines three types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial Gradients (defined by their center)

Is it possible to combine a background image and CSS3 gradients?

You can combine a background-image and CSS3 gradient on the same element by using several backgrounds. In our example below, we set the height and width of our <div> and add a background. Then, we set the background image fallback using the “url” value of the background-image property.

What are the three main types of gradients in CSS?

There are 3 different CSS gradients: linear, conic, and radial. Each gradient uses a CSS function to pass in multiple color arguments. The colors can be in the format of hex, hsla, rgba or named colors. In addition, you can pass in direction and angles to specify the shape and transition of the gradient.


1 Answers

The trick is to create an angled content mask, and then fill in the masked area with the desired styling, in this case a background gradient. The content will be clipped to the shape of the mask.

A mask is simply a container with overflow:hidden. If a CSS3 transform is applied to the container (for instance, a rotation or a skew operation), the mask will have a rotated or skewed shape, and the content will be clipped to this shape. A pair of nested masks, the outer one skewed and the inner one counter-skewed, produces a trapezoid mask with 2 angled sides. Skewing only the inner mask produces a trapezoid with 1 angled side.

          Both masks skewed           Inner mask skewed
          _________________           _________________
         /                 \          |                \
        /  clipped content  \         | clipped content \
       /_____________________\        |__________________\

JSFiddle demos:

  • Both masks skewed
  • Inner mask skewed

HTML

<div class="main">
    <div class="outer-mask">
        <div class="inner-mask">
            <div class="content">Styled content goes here</div>
        </div>
    </div>
</div>

CSS

.main {
    position: relative;
}
.outer-mask {
    position: absolute;
    left: 95px;
    top: 45px;
    width: 390px;
    height: 110px;
    overflow: hidden;
    -webkit-transform: skew(20deg, 0deg);
        -ms-transform: skew(20deg, 0deg);
         -o-transform: skew(20deg, 0deg);
            transform: skew(20deg, 0deg);
}
.inner-mask {
    position: absolute;
    left: -45px;
    top: 0px;
    width: 390px;
    height: 110px;
    overflow: hidden;
    -webkit-transform: skew(-40deg, 0deg);
        -ms-transform: skew(-40deg, 0deg);
         -o-transform: skew(-40deg, 0deg);
            transform: skew(-40deg, 0deg);
}
like image 51
Matt Coughlin Avatar answered Sep 21 '22 14:09

Matt Coughlin