1.4 KiB
1.4 KiB
Border with top triangle
Use pure CSS to create div with top triangle.
HTML
<div class="container">
Border with top triangle
</div>
CSS
.container {
display: block;
position: relative;
background: #ffffff;
padding: 15px;
border: 1px solid #dddddd;
margin-top: 20px;
}
.container:before, .container:after {
content: '';
display: block;
position: absolute;
bottom: 100%;
width: 0;
height: 0;
}
.container:before {
left: 19px;
border: 11px solid transparent;
border-bottom-color: #dddddd;
}
.container:after {
left: 20px;
border: 10px solid transparent;
border-bottom-color: #ffffff;
}
Demo
Explanation
- Use pseudo-element
beforeandafterto create two triangles. How to create triangle can see the Triangle. - The color of the
beforetriangle as same as the container border color. The color of theaftertriangle as same as the container background color. - The border width of the
beforetriangle is wider 1px(depend on the border width of container) than theaftertriangle, then we can see the top triangle border on the container. - The
aftertriangle is on the right of thebeforetriangle(1px) so that can make the left part border of thebeforetriangle to visible.
Browser support
✅ No caveats.