<div id=”demo”></div>
#demo { position: relative; width: 100px; height: 100px; border: 2px solid #000; background-color: #FFF; } #demo::before, #demo::after { content: ""; position: absolute; top: 20px; left: 100%; display: block; width: 0; height: 0; border: 10px solid transparent; border-right-width: 0; } #demo::before { border-left-color: #000; margin-left: 2px; } #demo::after { border-left-color: #FFF; }
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> </head> <style type="text/css"> .one{ width: 100px; height: 100px; border:2px solid #000; position: relative; background-color: #fff; } .one:before{ content: ""; width: 0px; height: 0px; border: 10px solid transparent; border-left-color: #000; position: absolute; top: 20px; left: 100%; } .one:after{ content: ""; border: 8px solid transparent; border-left-color: #fff; position: absolute; top: 22px; left: 100%; } </style> <body> <div class="one"></div> </body> </html>
#demo { width: 100px; height: 100px; background-color: #fff; position: relative; border: 2px solid #333; } #demo:after, #demo:before { border: solid transparent; content: ' '; height: 0; left: 100%; position: absolute; width: 0; } #demo:after { border-width: 10px; border-left-color: #fff; top: 20px; } #demo:before { border-width: 12px; border-left-color: #000; top: 18px; }
#demo { width: 100px; height: 100px; border: 2px solid #000; position: relative; } #demo:after { content: ''; display: block; width: 14.1421px; height: 14.1421px; border-top: 2px solid #000; border-right: 2px solid #000; position: absolute; right: -10px; top: 20px; transform: rotate(45deg); background-color: #fff; } 解题思路: 将三角形用一个正方形来实现 设置其上border,右border的宽度 设置其背景颜色为白色以覆盖掉父元素的border 使用transform: rotate 来让该正方形旋转 使用top,left来对其定位
知识扩展:
可用css中的border实现:
width:0;
height:0;
border-right:10px solid red;
border-top:10px solid transparent;//设置透明
border-bottom:10px solid transparent;
三角形 顶点在哪边 就 不设置哪边的border值 ,三角形向左或向右,则设置border的顶部和底部为相同像素的透明色。三角形的高则为像素的点素,即px值。
<div id="demo">
<div id="a"></div>
<div id="b"></div>
</div>
解题思路:
1、 id为demo的实际宽度等于宽度100px+2px=102px;
2、 设置a、b同时以demo为标准向左偏移100px,即可覆盖2px的边框。
3、 设置a的边框大小为12px;背景色为黑色;
设置b的边框大小为10px,背景色为白色;
b相对a向下偏移2px,即可得到边框为2px的三角边;
即该效果:
代码实现:
#demo1 {
width: 100px;
height: 100px;
background-color: #fff;
position: relative;
border: 2px solid #333;
}
#a,#b
{
width:0;
height:0;
position:absolute;
left:100px;
border:solid transparent;
}
#a
{
top:18px;
border-width:12px;
border-left-color:#000;
}
#b
{
top:20px;//b的top值要比a多2px
border-width:10px;
border-left-color:#fff;
#demo { position: relative; width: 100px; height: 100px; background-color: #fff; border: 2px solid #333; } #demo:after, #demo:before { content: ' '; width: 0; height: 0; border: 12px solid transparent; top: 18px; position: absolute; } #demo:after { border-left-color: #fff; left:100px; } #demo:before { border-left-color: #000; left: 102px; }思路:两个三角形,一个黑色一个白色,边框大小,距顶部top都相同,只有位置和颜色不同,让白色的覆盖住黑色往左移动2px即可
<!DOCTYPE html><html><head><title>texst</title><metacharset="utf-8"><styletype="text/css">.big{width:100px;height:100px;border:1px solid black;float:left;}.smallOne{float:left;width:0px;height:0px;border-width:10px;border-style: solid;border-color:transparent transparent transparent black;margin-top:20px;}.smallTwo{float:left;width:0px;height:0px;border-width:10px;border-style: solid;border-color:transparent transparent transparent white;margin-top:20px;margin-left: -21px;}</style></head><body><divid="demo"><divclass="big"></div><divclass="smallOne"></div><divclass="smallTwo"></div></div></body></html>
#demo{ width:98px; height:98px; background-color: #FFF; border: 2px solid #000; position: relative; }#demo:after{ content: ''; position: absolute; width: 20px; height: 20px; border: 2px solid transparent; border-left-color: black; border-bottom-color: black; transform: rotate(-135deg); right: -12px; top: 20px; }#demo:before{
content: '';
position: absolute;
background-color: #FFF;
width: 20px;
height: 20px;
transform: rotate(45deg);
right: -12px;
top: 20px;
border: 2px solid #FFF;
}