仅使用CSS实现折角效果的Div

11

我试过了网上的几种方法,却无法让任何一种方法奏效。

我有一个需要自适应宽度和高度的 div。它位于可平铺的背景图片上方,并且周围有 1像素的边框

我需要该 div 的右下角像折叠一张纸一样。

我试过在底部设置一个带图像的 div,但那需要一个固定的宽度或高度。

我也试过这个方法,但那需要一个纯色的背景。而我的背景是一个可重复的图片。

我还尝试过这个方法,它使用渐变来控制角落处的不透明度,这个方法几乎可以奏效,但我的 div 需要一个边框,而添加边框会破坏这个效果。

background:
    linear-gradient(45deg,  rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
    linear-gradient(135deg, rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
    linear-gradient(225deg, transparent 10px,  rgba(255,0,0,0.4)
background-size: 14px 14px, 50% 100%, 50% 50%, 50% 50%;
background-position: 100% 0, 0 0, 100% 100%, 100% 0;
background-repeat: no-repeat;

//then an :after pseudo class to create the corner fold

非常感谢您的帮助。谢谢。


这个不错。我猜你不想使用任何图像,否则通过伪元素背景图像(绿色)将整个div的右侧(折叠的宽度)制作成图像可能更容易。请查看:http://jsfiddle.net/jackweinbender/9btv5/41/ - jackweinbender
看起来有人做了一份不错的工作:[链接](http://jsfiddle.net/necolas/uzaG9/) - Aravind30790
你是否想要像这样有边框的效果 http://jsfiddle.net/9btv5/42/ - Bhojendra Rauniyar
1个回答

4
这个问题让我忙了一段时间,使用仅 CSS 实现似乎非常困难。我成功实现了您想要的效果(带有元素周围的纸张翻转效果),但需要大量的 CSS,并且我不知道您想要达到什么程度。我将 border-radius 应用于右上角,并使用三角形重叠 border-radius。这并没有完全覆盖 border radius,所以我使用了一个 span 来形成 2 个形状以覆盖剩余的空隙。

查看此 fiddle 获取结果,欢迎任何改进意见。

http://jsfiddle.net/EnVnW/

代码:

body {
    background: #444 url('http://leaverou.me/ft2010/img/darker_wood.jpg') bottom;
}

.arrow_box {
    color:red;
    position: relative;
    background: #88b7d5;
    border: 4px solid #c2e1f5;
    height:400px;
    border-radius:0 300px 0 0; /* here we give the box a round corner on the top right side */
}

.arrow_box:after, .arrow_box:before { /* we create 2 triangles in the top right corner, one for the border and one for the filling */
    -ms-transform:rotate(-135deg); /* rotate to make the triangle have the right angle */
    -webkit-transform:rotate(-135deg); 
     transform:rotate(-135deg);
    bottom: 100%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    top:42px;
    right:-20px;
}

/* here we position the triangles in the top right corner */
.arrow_box:after {
    border-color: rgba(200, 265, 0, 0);
    border-bottom-color: #00b7d5;
    border-width: 100px;
    left: 100%;
    margin-left: -240px;
}
.arrow_box:before {
    border-color: rgba(194, 225, 245, 0);
    border-bottom-color: #c2e1f5;
    border-width: 105px;
    left: 100%;
    top:39px;
    margin-left: -245px;
}

/* we still have a massive gap next to the triangle, so we fill it up with 2 rectangles. One on the left side of the triangle and one on the bottom side of the triangle, we also will give them a border to finish the line around the element */
.arrow_box span {
    border-top: 4px solid #c2e1f5;
    position:absolute;
    width:150px;
    height:140px;
    background-color:black;
    right:140px;
    top:-4px;
    background: #88b7d5;
}

.arrow_box span:after {
    border-right: 4px solid #c2e1f5;  
    content: "";
    position:absolute;
    width:150px;
    height:150px;
    background: #88b7d5;
    left:140px;
    top:140px;
}

使用CSS4,这将更容易实现。在这里,您可以阅读相关内容。

http://dev.w3.org/csswg/css-backgrounds-4/#border-corner-shape


@Sonyflat,完成了,我还添加了一些注释来澄清我的代码。 - koningdavid
已经实现。谢谢大家!迫不及待地等待CSS4! - Michael Botelho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接