我想在这张照片上创建形状:
我在这张照片上创建了三角形,并将边距设置为右上角,但我不知道如何让它看起来像左边的div一样,如图所示。
我是否必须“剪切”左侧div以保持其灰色边框并且看起来与绿色三角形分开?
有什么想法怎么做?
编辑:
答案 0 :(得分:46)
我建议,鉴于以下加价:
<div id="box"></div>
CSS:
#box {
width: 10em;
height: 6em;
border: 4px solid #ccc;
background-color: #fff;
position: relative;
}
#box::before,
#box::after {
content: '';
position: absolute;
top: 0;
right: 0;
border-color: transparent;
border-style: solid;
}
#box::before {
border-width: 1.5em;
border-right-color: #ccc;
border-top-color: #ccc;
}
#box::after {
border-radius: 0.4em;
border-width: 1.35em;
border-right-color: #0c0;
border-top-color: #0c0;
}
答案 1 :(得分:4)
将两个绝对定位的div放在容器div中,位置相对。然后使外三角形略大于内三角形的三角形。然后将这些元素放在容器的右上角。
<强> HTML 强>
<div class="container">
<div class="inner-triangle"></div>
<div class="outer-triangle"></div>
</div>
<强> CSS 强>
.container{
border: 2px solid gray;
position: relative;
height: 100px;
}
.inner-triangle{
border-left: 20px solid transparent;
border-right: 20px solid green;
border-bottom: 20px solid transparent;
height: 0;
width: 0;
position: absolute;
right: 0px;
z-index: 2;
}
.outer-triangle{
border-left: 22px solid transparent;
border-right: 22px solid gray;
border-bottom: 22px solid transparent;
height: 0;
width: 0;
position: absolute;
right: 0px;
z-index: 1;
}
JS小提琴: http://jsfiddle.net/u8euZ/1
答案 2 :(得分:1)
您可以将rotate伪元素与父级的overflow:hidden
结合使用。
从这里你可以使用position:absolute
将伪定位到右上角,你应该好好去!
div {
height: 250px;
width: 300px;
background: lightgray;
border-radius: 10px;
border: 5px solid dimgray;
position: relative;
overflow: hidden;
margin: 30px auto;
}
div:before {
content: "";
position: absolute;
top: -60px;
right: -60px;
height: 100px;
width: 100px;
background: green;
border: 5px solid dimgray;
transform: rotate(45deg);
}
/***********FOR DEMO ONLY*******************/
html, body {
margin:0;
padding:0;height:100%;
vertical-align:top;overflow:hidden;
background: rgb(79, 79, 79);
background: -moz-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, rgba(79, 79, 79, 1)), color-stop(100%, rgba(34, 34, 34, 1)));
background: -webkit-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
background: -o-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
background: -ms-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
background: radial-gradient(ellipse at center, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#4f4f4f', endColorstr='#222222', GradientType=1);
}
<div></div>