当您使用移动版Safari浏览器访问http://m.google.com页面时,您会看到页面顶部有一个漂亮的工具栏。
我想画一些类似的梯形,但我不知道如何做。我应该使用CSS3 3D变换吗?如果您有好的方法,请告诉我。
我想画一些类似的梯形,但我不知道如何做。我应该使用CSS3 3D变换吗?如果您有好的方法,请告诉我。
由于这篇文章很旧了,我觉得需要一些新的更新答案,应用一些新技术。
.trapezoid {
width: 200px;
height: 200px;
background: red;
transform: perspective(10px) rotateX(1deg);
margin: 50px;
}
<div class="trapezoid"></div>
<svg viewBox="0 0 20 20" width="20%">
<path d="M3,0 L17,0 L20,20 L0,20z" fill="red" />
</svg>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(30, 0);
ctx.lineTo(170, 0);
ctx.lineTo(200, 200);
ctx.lineTo(0, 200);
ctx.fillStyle = "#FF0000";
ctx.fill();
<canvas id="myCanvas" width="200" height="200"></canvas>
animation
内部,所以我不得不使用CSS transform
,这也包含在这个答案中。 - Jacksonkrbox-shadow
! - Timar Ivo Batistransform: perspective(10px) rotateX(-1deg);
- John您可以使用以下CSS:
#trapezoid {
border-bottom: 100px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
height: 0;
width: 100px;
}
<div id="trapezoid"></div>
制作所有这些形状真的很酷,可以看看更多漂亮的形状:
http://css-tricks.com/examples/ShapesOfCSS/
编辑: 这个CSS应用于一个DIV元素。
要绘制任何形状,可以使用CSS的clip-path属性,如下所示。
您可以使用免费在线编辑器生成此代码(例如:https://bennettfeely.com/clippy/)
.trapezoid {
clip-path: polygon(0 0, 100% 0, 84% 41%, 16% 41%);
}
如果您希望它更具适应性,可以定义一个Sass混合器,如下所示:
@mixin trapezoid ($top-width, $bottom-width, $height) {
$width: max($top-width, $bottom-width);
$half-width-diff: abs($top-width - $bottom-width) / 2;
$top-left-x: 0;
$top-right-x: 0;
$bottom-left-x: 0;
$bottom-right-x: 0;
@if ($top-width > $bottom-width) {
$top-left-x: 0;
$top-right-x: $top-width;
$bottom-left-x: $half-width-diff;
$bottom-right-x: $top-width - $half-width-diff;
} @else {
$top-left-x: $half-width-diff;
$top-right-x: $bottom-width - $half-width-diff;
$bottom-left-x: 0;
$bottom-right-x: $bottom-width;
}
clip-path: polygon($top-left-x 0, $top-right-x 0, $bottom-right-x $height, $bottom-left-x $height);
width: $width;
height: $height;
}
.my-div {
@include trapezoid(8rem, 6rem, 2rem);
}
这是一个老问题...但我想添加一种未提及的方法。可以使用半色半透明的渐变来绘制三角形,然后可以从3个渐变形状构建梯形。以下是示例代码,3个块以不同颜色绘制,以便更好地理解:
#example {
width: 250px;
height: 100px;
background-image:
linear-gradient(to top left, red 0 50%, transparent 50% 100%),
linear-gradient(to top right, green 0 50%, transparent 50% 100%),
linear-gradient(blue 0 100%);
background-size:
20% 100%, 20% 100%, 60% 100%;
background-position:
left top, right top, center top;
background-repeat: no-repeat;
}
<div id="example"></div>
你有几个选项可供选择。你可以直接使用图像,在svg中绘制一些内容或使用css变换扭曲常规div。图像是最简单的,可以在所有浏览器上工作。在svg中绘制比较复杂,并不能保证在所有浏览器上都能正常工作。
另一方面,使用css变换意味着你必须将形状div放在背景中,然后在另一个元素中将实际文本覆盖在它们上面,以便文本不会被扭曲。同样,浏览器支持也不能保证。