简体   繁体   English

梯形形状,顶部和右侧成角度

[英]Trapezoid shape with angled top and right side

It is a curved div basically: 基本上是一个弯曲的div:

弯曲的div

So it is possible to do with just CSS and no images? 那么有可能只使用CSS而没有图像吗?

Well... I was the biggest skeptic of this shape, but it seems it is possible O_o 好吧...我是这种形状的最大怀疑者,但似乎O_o

Demo 演示

HTML HTML

<div class="shape one"></div>
<div class="shape two"></div>
<div class="shape three"></div>

CSS CSS

.shape
{
    background:red;
    float:left;
}

.one
{
    border-width:0px;
    border-bottom:10px solid red;
    border-left:200px solid #fff;
    width:0px;
}

.two
{
    width:200px;
    height:40px;
    clear:left;
}

.three
{
    border-width:0px;
    border-top:50px solid red;
    border-right:10px solid #fff;
    width:0px;
    margin-top:-10px;
}

The border method looks grainy in the browsers I tested. 在我测试的浏览器中, border方法看起来很粗糙。 Here's a method using the ::before and ::after pseudo-elements and CSS transform: skew() that looks smoother. 这是使用::before::after伪元素以及CSS transform: skew()看起来更平滑。 You can adjust the angles as needed. 您可以根据需要调整角度。 This uses only one <div> . 这仅使用一个<div>

Demo: 演示: 的jsfiddle

Output: 输出:

产量

HTML: HTML:

<div class="quadrilateral"></div>

CSS: CSS:

.quadrilateral {
    background-color: red;
    height: 50px;
    margin: 50px;
    position: relative;
    width: 300px;
}
.quadrilateral::before {
    background-color: red;
    content: '';
    display: inline-block;
    height: 61px;
    position: absolute;
    right: -3px;
    top: -11px;
    transform:             skewX( -5deg ); 
        -ms-transform:     skewX( -5deg ); 
        -webkit-transform: skewX( -5deg ); 
        -o-transform:      skewX( -5deg ); 
        -moz-transform:    skewX( -5deg ); 
    width: 10px;
}
.quadrilateral::after {
    background-color: red;
    content: '';
    display: inline-block;
    height: 15px;
    position: absolute;
    top: -6px;
    transform:             skewY( -2deg ); 
        -ms-transform:     skewY( -2deg ); 
        -webkit-transform: skewY( -2deg ); 
        -o-transform:      skewY( -2deg ); 
        -moz-transform:    skewY( -2deg ); 
    width: 300px;
}

Instead of CSS, consider using SVG. 考虑使用SVG代替CSS。 It's supported in all major browsers and that shape would be very very small in SVG format. 所有主要的浏览器都支持它,并且该形状在SVG格式中将非常小。 It also would be in your DOM tree so you could bind events on it. 它也将在您的DOM树中,因此您可以在其上绑定事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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