Border with top triangle
Creates a text container with a triangle at the top.
<div class="container">
Border with top triangle
</div>
.container {
position: relative;
background: #ffffff;
padding: 15px;
border: 1px solid #dddddd;
margin-top: 20px;
}
.container:before, .container:after {
content: '';
position: absolute;
bottom: 100%;
left: 19px;
border: 11px solid transparent;
border-bottom-color: #dddddd;
}
.container:after {
left: 20px;
border: 10px solid transparent;
border-bottom-color: #ffffff;
}
Explanation
- Use the
:beforeand:afterpseudo-elements to create two triangles. - The color of the
:beforetriangle should be the same as the container’s border color. - The color of the
:aftertriangle should be the same as the container background color. - The border width of the
:beforetriangle should be1pxwider than the:aftertriangle, in order to act as the border. - The
:aftertriangle should be1pxto the right of the:beforetriangle to allow for its left border to be shown.