:root {
--width: 300px;
--height: 100px;
--top-color: #f44336;
--bottom-color: #2196F3;
}
.separator {
width: 0;
height: 0;
border-style: solid;
border-width: var(--height) var(--width) 0 0;
border-color: var(--top-color) var(--bottom-color) transparent transparent;
}.separator.reverse {
border-width: var(--height) 0 0 var(--width);
border-color: var(--top-color) transparent transparent var(--bottom-color);
}.separator.vertical {
border-width: var(--height) var(--width) 0 0;
border-color: var(--left-color) var(--right-color) transparent transparent;
}.separator.vertical.reverse {
border-width: 0 var(--width) var(--height) 0;
border-color: transparent var(--left-color) var(--right-color) transparent;
}Notes:
- 👍 The angle is controlled by the element
heightvalue - 👎 Cannot be used with percentage measurement unit (
%), any other unit is valid: (cm,em,ex,in,mm,pc,pt,px) - 👎 For use in a full width or height scenarios matching the viewport, use viewport units: (
vw,vh,vmin) - 👎 Further control is limited
- e.g. creating a shadow effect using
box-shadow
- e.g. creating a shadow effect using
Can be used with the ::before and ::after pseudo-elements to generate HTML content for the separator without directly modifying your DOM.
section {
...
}
section::after {
content: '';
display: block;
width: 0;
height: 0;
border-style: solid;
border-width: 50px 300px 0 0;
border-color: yellow black transparent transparent;
}View Demo, Play on CodePen, or inspect the source files.
