简体   繁体   English

带 CSS 的圆角梯形

[英]Rounded trapezoid with CSS

I'm having a slight problem with css.我在使用 css 时遇到了一个小问题。 I need a trapezoid div which upper left corner(the one with the angle above 90 degrees) is rounded.我需要一个梯形 div,它的左上角(角度大于 90 度的那个)是圆角的。 I already know that this:我已经知道这个:

HTML: HTML:

<div style="margin:30px">
  <div class="trapezoid">
  </div>
</div>

CSS: CSS:

.trapezoid{
  vertical-align: middle;
  border-bottom: 31px solid red;
  border-left: 25px solid transparent;
  height: 0;
  width: 150px;
}

produces a trapezoid.产生梯形。 I tried the border-top-left-radius property, however the effect is not sufficent enough.我尝试了border-top-left-radius属性,但效果还不够。

Here's a jsfiddle with the above code to, well, fiddle with: http://jsfiddle.net/n3TLP/5/这是一个带有上述代码的 jsfiddle,用于: http : //jsfiddle.net/n3TLP/5/

I there is more info needed just comment.我需要更多信息,请发表评论。
Thanks in advance :)提前致谢 :)

Not that you should ever do this, but you can also create a rounded corner trapezoid with a single element by applying CSS 3d transformations:并不是说你应该这样做,但你也可以通过应用 CSS 3d 转换来创建一个带有单个元素的圆角梯形:

.trapezoid {
  position: relative;
  width: 300px;
  height: 200px;
  overflow: hidden;
}

.trapezoid:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 200%;
  height: 100%;
  background: red;
  border-radius: 20px 0 0 0;
  -webkit-transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -ms-transform-origin: 100% 100%;
  -o-transform-origin: 100% 100%;
  transform-origin: 100% 100%;
  -webkit-transform: perspective(400px) rotateX(45deg);
  -moz-transform:  perspective(400px) rotateX(45deg);
  -ms-transform: perspective(400px) rotateX(45deg);
  -o-transform: perspective(400px) rotateX(45deg);
  transform: perspective(400px) rotateX(45deg);
}

http://jsfiddle.net/RzJTP/ http://jsfiddle.net/RzJTP/

Although I think you're better off using <canvas> /SVG to draw this shape, this is close to what you want:虽然我认为你最好使用<canvas> /SVG 来绘制这个形状,但这与你想要的很接近:

.trapezoid{
    vertical-align: middle;
    border-bottom: 120px solid red;
    border-left: 200px solid transparent;
    border-top-left-radius:30px;
    height: 0;
    width: 150px;}

/* ---------- */

.trapezoid {
    position:relative;
}
.trapezoid:after {
    content:' ';
    left:-14px;
    top:-10px;
    position:absolute;
    background:red;
    border-radius:40px 0 0 0;
    width:164px;
    height:40px;
    display:block;
}

Demo: http://jsfiddle.net/n3TLP/20/演示: http : //jsfiddle.net/n3TLP/20/

It's not perfect, and you'll have to play with the numbers to get your desired dimensions, it's very finicky.它并不完美,您必须使用数字来获得所需的尺寸,这非常挑剔。 You might be interested in something like Raphaël for drawing, CSS doesn't really have the capacity for complex shapes (at least not intentionally).您可能对Raphaël 之类的绘图感兴趣,CSS 并没有真正具有复杂形状的能力(至少不是故意的)。

Use Adobe Illustrator or any other software to draw a shape and than save it as SVG code, you can use SVG directly on the page but IE8 and lower will ignore it.使用 Adob​​e Illustrator 或其他任何软件绘制形状并将其保存为 SVG 代码,您可以直接在页面上使用 SVG,但 IE8 及更低版本将忽略它。 If you need to support older versions of IE you can use Raphael.js to draw your SVG element.如果您需要支持旧版本的 IE,您可以使用 Raphael.js 来绘制您的 SVG 元素。

Rendering SVG polygons in Raphael Javascript library 在 Raphael Javascript 库中渲染 SVG 多边形

Voila:瞧:

css: css:

.trapezoid{
    vertical-align: middle;
    background: red;
    padding-left: 200px;
    height: 120px;
    width: 150px;
    position: relative;
    border-top-left-radius: 40px;
    overflow: hidden;
    background-clip: content-box;
}

.trapezoid:after{
    content: '';
    margin-left: -100px;
    top: 0;
    height: 120px;
    background: red;
    transform: skew(-31deg,0deg);
    -o-transform: skew(-31deg,0deg);
    -ms-transform: skew(-31deg,0deg);
    -moz-transform: skew(-31deg,0deg);
    -webkit-transform: skew(-59deg,0deg);
    position: absolute;
    width: 1000px;
    border-top-left-radius: 40px;
}

Demo:演示:
http://jsfiddle.net/n3TLP/24/ http://jsfiddle.net/n3TLP/24/

Here's my attempt lol这是我的尝试,哈哈

.trapezoid{
    position:relative;
    border-bottom: 100px solid blue;
    border-right: 12px solid transparent;
    border-left: 180px solid transparent;
    width: 122px;
}

.trapezoid:before{
    content:' ';
    left:-184px;
    top:98px;
    position:absolute;
    background:blue;
    border-radius:80px 20px 80px 80px;
    width:318px;
    height:20px;
}


.trapezoid:after {
    content:' ';
    left:-11px;
    top:-7px;
    position:absolute;
    background:blue;
    border-radius:150px 50px 90px 0px;
    width:133px;
    height:30px;
}

<div style="margin:30px">
    <div class="trapezoid">
    </div>
</div>

http://jsfiddle.net/Bzj3h/ http://jsfiddle.net/Bzj3h/

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

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