没有图像的CSS3多边形,怎么样?

时间:2013-05-29 12:11:29

标签: html5 css3 polygon css-shapes

我正在建立一个类似这样的网站 without expansion

顶部静态和底部四个的两个结构是动态的,当我说动态时,我的意思是可扩展,类似这样的

with expansion

由于这些只是颜色,形状和阴影,我相信可以使用CSS3来创建它们。但是我很难这样做。我甚至试过使用CSSHat,但结果很糟糕。

这是示例形状 individual shape

这是生成的代码:

width: 1920px;
height: 341px;
-moz-border-radius: 0 1920px 1920px / 0 181px 56px;
-webkit-border-radius: 0 1920px 1920px / 0 181px 56px;
border-radius: 0 1920px 1920px / 0 181px 56px; /* border radius */
-moz-background-clip: padding;
-webkit-background-clip: padding-box;
background-clip: padding-box; /* prevents bg color from leaking outside the border */
background-color: #3b0f1d; /* layer fill content */

结果如下: converted shape 可以在此处找到实时示例http://codepen.io/seraphzz/pen/mgDwd

实现这一目标的最佳途径是什么?

4 个答案:

答案 0 :(得分:4)

您可以使用CSS transforms来旋转div。

我已将底部div 动态,并将一些内容添加到黄色绿色 div。



body {
    margin: 0;
    padding: 0;
    overflow-x: hidden;
}
.top, .top2, .yellow, .green, .bottom, .bottom2 {
    height: 200px;
    width: 140%;
    position: relative;
    left: -10%;
    transform-origin: 0% 100%;
    transition: 0.6s ease;
}
.top {
    top: -180px;
    background: #3C101D;
    transform: rotate(4deg);
}
.top2 {
    background: #931930;
    transform: rotate(-1.5deg);
}
.yellow {
    background: #F0810E;
    top: -200px;
    transform: rotate(0.6deg);
}
.content {
    display: block;
    width: 65%;
    position: relative;
    top: 10%;
    left: 10%;
}
.yellow .content {
    transform: rotate(-0.6deg);
}
.green {
    background: #425949;
    top: -320px;
    transform: rotate(-1.4deg);
}
.green .content {
    transform: rotate(1.4deg);
}
.bottom {
    background: #501B2B;
    top: -480px;
    transform: rotate(1.4deg);
}
.bottom2 {
    background: #3C101D;
    top: -600px;
    transform: rotate(-0.6deg);
}
.yellow:hover, .green:hover, .bottom:hover, .bottom2:hover {
    height: 400px;
}

<div class="top"></div>
<div class="top2"></div>
<div class="yellow">
    <div class="content">But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</div>
</div>
<div class="green">
<div class="content">But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</div>
</div>
</div>
<div class="bottom"></div>
<div class="bottom2"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用CSS transformations:before / :after元素上的)来模拟效果。

类似({em>演示在http://codepen.io/gpetrioli/pen/LHbIE

<div class="background">
    <div class="shape"></div>
</div>

background{background-color: #3c101d}
.shape{
  width: 800px;
  height: 341px;
  background: white;
  display: block;
  margin:10px;
  position:relative;
  overflow:hidden;
}
.shape:before{
  content:'';
  width:110%;
  height:30px;
  background:#3c101d;
  position:absolute;
  height:100px;
  top:0;
  left:0;
  transform-origin:0 100%;
  transform: translateY(-100px) rotate(5deg);
}
.shape:after{
  content:'';
  width:110%;
  height:30px;
  background:#3c101d;
  position:absolute;
  height:100px;
  bottom:0;
  left:0;
  transform-origin:0 0;
  transform: translateY(100px) rotate(-2deg);
}

注意:此代码使用标准CSS属性。在需要的地方添加供应商前缀

答案 2 :(得分:1)

以下是“CSSHat形状”的解决方案: -

<!DOCTYPE html>
<html>
<body>
<style>
    .background{
        background-color: #3c101d;
        padding:22px 28px;
        display:inline-block;
    }
    .shape{
        height: 30px;
        border-left: 645px solid white;
        border-top: 80px solid transparent;
        border-bottom: 40px solid transparent;
    }
</style>
<div class="background">
    <div class="shape"></div>
</div>
</body>
</html>

获得上述所有形状的技巧是: - 所有边框尺寸,所有边框颜色和 div的高度或宽度。

Codepen: - http://codepen.io/mrmoje/pen/EaQNOP

答案 3 :(得分:1)

这些形状看起来像透视转换给我。 http://css-tricks.com/almanac/properties/p/perspective/我不相信有一个生成器可以使左边和右边比第二个图形的橙色部分隐藏溢出的父级更宽。

你可以从这个透视发生器看到旋转时所有角度都是可能的。 http://desandro.github.io/3dtransforms/examples/perspective-03.html

作为替代标记,而不是CSS3,也不需要图像

我建议使用SVG来标记所有多边形。所有CSS3浏览器都更容易和支持。