forked from Chalarangelo/30-seconds-of-css
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add border with top triangle snippet
- Loading branch information
zhaozhiming
committed
Jan 18, 2019
1 parent
bb566c6
commit ba58815
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
### Border with top triangle | ||
|
||
Use pure CSS to create div with top triangle. | ||
|
||
#### HTML | ||
|
||
```html | ||
<div class="container"> | ||
Border with top triangle | ||
</div> | ||
``` | ||
|
||
#### CSS | ||
|
||
```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 | ||
|
||
1. Use pseudo-element `before` and `after` to create two triangles. How to create triangle can see the [Triangle](https://30-seconds.github.io/30-seconds-of-css/#triangle). | ||
2. The color of the `before` triangle as same as the container border color. The color of the `after` triangle as same as the container background color. | ||
3. The border width of the `before` triangle is wider 1px(depend on the border width of container) than the `after` triangle, then we can see the top triangle border on the container. | ||
4. The `after` triangle is on the right of the `before` triangle(1px) so that can make the left part border of the `before` triangle to visible. | ||
|
||
#### Browser support | ||
|
||
<span class="snippet__support-note">✅ No caveats.</span> | ||
|
||
<!-- tags: visual --> |