我正在尝试从 div 中切出一个三角形并显示 div 后面的背景...射击:-)
这就是我想要实现的(抱歉图像模糊):
请不要用 3 列解决方案或类似的解决方案来回答这个问题,我完全有能力以这种方式实现这一点......我只是想知道是否有任何很酷的 CSS 技巧可以通过少数几个来实现这一点图形,如果不是没有图形,尽可能?
可以通过一些幻想和一些小技巧来伪造它:
border-radius
切出div的透明角;::before
(或::after
)伪元素创建一个与 div 相同背景颜色的大三角形;z-index
将三角形放在背景 div 之上,但在div内容之下;使用transparent
与你的div大小相同的容器来容纳它,并申请overflow: hidden
切割三角形的超出部分;
等等!
演示:http: //jsfiddle.net/e2Umv/
不,我不知道这是可能的,我只是喜欢挑战和棘手的问题:)
HTML
<div class="someText" >
this text is partially visible;<br/>
this text is partially visible;<br/>
this text is partially visible;<br/>
this text is partially visible;<br/>
this text is partially visible;<br/>
</div>
<div class="underneathContainer">
<div class="cutout">Hello cut out div</div>
</div>
CSS
.someText{
background: green;
position: absolute;
z-index: 0;
top: 0;
}
.underneathContainer{
margin: 20px;
width: 260px;
height: 260px;
background: rgba(0,0,0,0);
overflow: hidden;
}
.cutout{
width: 200px;
height: 200px;
background: silver;
padding: 30px;
position: relative;
z-index: 1;
border-top-left-radius:30%;
}
.cutout:before{
content: '';
position: absolute;
top: -44px;
left: -30px;
border-width: 60px 60px 60px 60px;
border-style: solid;
border-color: rgba(0,0,0,0) silver silver rgba(0,0,0,0);
z-index: -1;
}
我能想到的唯一适用于此的是图像蒙版。您可能必须将一个元素放在另一个元素之上才能在蒙版上显示内容。
对图像掩码的支持并不普遍(我相信目前只有 webkit),但这可能会随着时间的推移而改变。
您可以将三角形与矩形分开并使用以下内容:
<div id="rectangle"><div id="mask"></div></div>
#rectangle{width:300px; height:120px; position:relative; margin-top:100px; background: rgb(30,87,153); /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzFlNTc5OSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3ZGI5ZTgiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(125,185,232,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(100%,rgba(125,185,232,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(125,185,232,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(125,185,232,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(125,185,232,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(30,87,153,1) 0%,rgba(125,185,232,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-8 */
}
#rectangle:after{content:" "; position:absolute; width:0px; height:0px; top:0; left:100px;border:1px solid white; border-color:transparent white transparent white; border-width:0px 50px 50px 50px;}
#mask{position:absolute; left:0; width:100px; height:50px; background:white;}
#mask:after{position:absolute; content:" "; left:200px; width:100px; background:white; height:50px; }
在这里拉小提琴。