<h1>Triangle Cutout</h1>
<br>
<div class="triangle-cutout">
<h2>Section Heading</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat blanditiis repellat, sed provident obcaecati explicabo rerum minus eum suscipit beatae laboriosam necessitatibus nihil! Ipsum quasi, aliquid nesciunt quis quod aliquam?</p>
</div>
<section>
<h2>Browser Support <small>(as of 2020-02-21)</small></h2>
<dl>
<dt>IE</dt><dd>✅ 9+</dd>
<dt>Edge</dt><dd>✅</dd>
<dt>Firefox</dt><dd>✅ 4+</dd>
<dt>Chrome</dt><dd>✅ 19+</dd>
<dt>Safari</dt><dd>✅ 6+</dd>
<dt>Opera</dt><dd>✅ 15+</dd>
<dt>iOS Safari</dt><dd>✅ 6.1+</dd>
<dt>Opera Mini</dt><dd>⚠️ Fallback</dd>
<dt>Android Browser</dt><dd>✅ 4.4+</dd>
<dt>Chrome for Android</dt><dd>✅</dd>
</dl>
<h3>Internet Explorer</h3>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/146527/2020-02-21-triangle-cutout-in-internet-explorer-11.png" alt="">
</section>
// ------------------------------
// Helpers
// ------------------------------
@function str-replace($string, $search, $replace: '') {
$string: inspect($string);
$index: str-index($string, $search);
@return if($index,
str-slice($string, 1, $index - 1) + $replace +
str-replace(str-slice($string, $index +
str-length($search)), $search, $replace),
$string);
}
// ------------------------------
// Triangle Cutout
// ------------------------------
/*
1. Use unitless values for the triangle's width/height so they can be used in an SVG without doing extra work to strip the "px" unit (instead, add `px` where needed with `$value * 1px`).
2. Use `filter: drop-shadow` on the parent so the shadow seamlessly applies to the whole element.
3. Use an SVG background-image to create the triangle "cutout" effect.
4. To keep the transparent area behind the SVG, use a linear-gradient to fill in the remaining space.
5. (Optional) Depending on the size of your triangle, some browsers at some screen sizes will approximate the position of the gradient, adding an empty 1px border between the triangle and the gradient fill. To get around this, subtract a half pixel from the gradient's position so its edges always overlap.
6. Reverse the triangle on the rigth side.
*/
.triangle-cutout {
// 1. Unitless values
$triangle-width: 60; // px
$triangle-height: 30; // px
$background-color: #fff;
position: relative;
margin-top: $triangle-height * 1px;
background-color: $background-color;
filter: drop-shadow(0 0 20px rgba(0, 0, 0, 0.2)); // 2. drop-shadow
&::before,
&::after {
content: '';
position: absolute;
bottom: 100%;
width: 50%;
height: $triangle-height * 1px;
background:
// 3. SVG background-image
url("data:image/svg+xml,%3Csvg width='#{$triangle-width / 2}' height='#{$triangle-height}' viewBox='0 0 #{$triangle-width / 2} #{$triangle-height}' xmlns='http://www.w3.org/2000/svg'%3E%3Cpolygon fill='#{str-replace($background-color, '#', '%23')}' points='0 0 0 #{$triangle-height} #{$triangle-width / 2} #{$triangle-height}' /%3E%3C/svg%3E") right top / ($triangle-width / 2 * 1px) ($triangle-height * 1px) no-repeat,
// 4. linear-gradient background-image
// 5. (Optional) Subtract a half-pixel for aliasing
linear-gradient(to right, $background-color calc(100% - #{$triangle-width / 2 * 1px }), rgba($background-color, 0) calc(100% - #{$triangle-width / 2 * 1px }));
}
&::before {
left: 0;
}
&::after {
left: 50%;
background:
// 6. Reverse triangle on the right side
url("data:image/svg+xml,%3Csvg width='#{$triangle-width / 2}' height='#{$triangle-height}' viewBox='0 0 #{$triangle-width / 2} #{$triangle-height}' xmlns='http://www.w3.org/2000/svg'%3E%3Cpolygon fill='#{str-replace($background-color, '#', '%23')}' points='0 #{$triangle-height} #{$triangle-width / 2} #{$triangle-height} #{$triangle-width / 2} 0' /%3E%3C/svg%3E") left top / ($triangle-width / 2 * 1px) ($triangle-height * 1px) no-repeat,
linear-gradient(to left, $background-color calc(100% - #{$triangle-width / 2 * 1px }), rgba($background-color, 0) calc(100% - #{$triangle-width / 2 * 1px }));
// (Optional) If you don't need to support IE11, you can save a few lines of code by replacing `background` with this simple `transform`.
// transform: scaleX(-1);
}
}
// ------------------------------
// Decorations
// ------------------------------
body {
background-color: lightgray;
}
h1 {
text-align: center;
}
h2 {
margin-top: 0;
}
h3 {
margin-top: 2rem;
}
div {
width: 300px;
margin: auto;
padding: 1rem;
}
section {
margin: 6rem auto;
max-width: 42rem;
padding: 1rem;
background: white;
line-height: 1.4;
tab-size: 4;
}
img {
max-width: 100%;
}
// --------------------------------
// Definition List w/ Leading Lines
// https://codepen.io/tannerhodges/pen/YrPMgv
// --------------------------------
dl {
display: grid;
grid-template-columns: auto 1fr;
grid-column-gap: 1em;
grid-row-gap: 0.5em;
margin: 1em;
line-height: 1.3;
}
dt {
position: relative;
grid-column: 1;
font-weight: bold;
overflow: hidden;
}
dd {
grid-column: 2;
margin-left: 0;
}
dt::after {
position: absolute;
top: 0.6em;
width: 100%;
margin-left: 0.75em;
border-bottom: 1px dotted #ccc;
content: '';
}
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.