The trick is to create an angled mask of the content , and then fill the hidden area with the desired style, in this case the background gradient, the Content will be attached to the shape of the mask.
A mask is just a container with overflow:hidden . If the CSS3 transform is applied to the container (for example, a rotation or skew operation), the mask will have a rotated or skewed shape, and the content will be cropped to this shape. A pair of nested masks, external, skewed and internal, with a skew, creates a trapezoidal mask with two angular sides. Tilting only the inner mask creates a trapezoid from 1 corner side.
Both masks skewed Inner mask skewed _________________ _________________ / \ | \ / clipped content \ | clipped content \ /_____________________\ |__________________\
JSFiddle demos:
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); }
Matt Coughlin
source share