513 lines
79 KiB
HTML
513 lines
79 KiB
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>30 Seconds of CSS</title> <meta name="description" content="A curated collection of useful CSS snippets you can understand in 30 seconds or less. From foundational elements such as clearfix to gradient text color and gradient cursor tracking to CSS easing and far beyond."> <link rel="icon" type="image/png" sizes="32x32" href="a8de561a148aec60dd3697311c540b29.png"> <script src="3df57813b7ec2de885ea6075ff46ed46.js" defer=""></script> <script async="" defer="" src="https://buttons.github.io/buttons.js"></script> <link rel="stylesheet" href="3df57813b7ec2de885ea6075ff46ed46.css"></head> <body> <button class="back-to-top-button" aria-label="back to top"> <i data-feather="arrow-up"></i> </button> <nav class="sidebar" aria-label="Table of Contents"> <button class="hamburger hamburger--spin sidebar__menu" type="button" aria-label="Menu" aria-expanded="false"> <span class="hamburger-box"> <span class="hamburger-inner"></span> </span> </button> <div class="sidebar__links"> <section data-type="layout" class="sidebar__section"> <h4 class="sidebar__section-heading">layout</h4> <a class="sidebar__link" href="#box-sizing-reset"><span>Box-sizing reset</span></a> <a class="sidebar__link" href="#clearfix"><span>Clearfix</span></a> <a class="sidebar__link" href="#constant-width-to-height-ratio"><span>Constant width to height ratio</span></a> <a class="sidebar__link" href="#evenly-distributed-children"><span>Evenly distributed children</span></a> <a class="sidebar__link" href="#flexbox-centering"><span>Flexbox centering</span></a> <a class="sidebar__link" href="#grid-centering"><span>Grid centering</span></a> <a class="sidebar__link" href="#grid-layout"><span>Grid layout</span></a> <a class="sidebar__link" href="#truncate-text"><span>Truncate text</span></a> </section> <section data-type="visual" class="sidebar__section"> <h4 class="sidebar__section-heading">visual</h4> <a class="sidebar__link" href="#circle"><span>Circle</span></a> <a class="sidebar__link" href="#custom-scrollbar"><span>Custom scrollbar</span></a> <a class="sidebar__link" href="#custom-text-selection"><span>Custom text selection</span></a> <a class="sidebar__link" href="#dynamic-shadow"><span>Dynamic shadow</span></a> <a class="sidebar__link" href="#etched-text"><span>Etched text</span></a> <a class="sidebar__link" href="#gradient-text"><span>Gradient text</span></a> <a class="sidebar__link" href="#hairline-border"><span>Hairline border</span></a> <a class="sidebar__link" href="#not-selector"><span>:not selector</span></a> <a class="sidebar__link" href="#overflow-scroll-gradient"><span>Overflow scroll gradient</span></a> <a class="sidebar__link" href="#pretty-text-underline"><span>Pretty text underline</span></a> <a class="sidebar__link" href="#reset-all-styles"><span>Reset all styles</span></a> <a class="sidebar__link" href="#shape-separator"><span>Shape separator</span></a> <a class="sidebar__link" href="#system-font-stack"><span>System font stack</span></a> <a class="sidebar__link" href="#triangle"><span>Triangle</span></a> </section> <section data-type="animation" class="sidebar__section"> <h4 class="sidebar__section-heading">animation</h4> <a class="sidebar__link" href="#bouncing-loader"><span>Bouncing loader</span></a> <a class="sidebar__link" href="#donut-spinner"><span>Donut spinner</span></a> <a class="sidebar__link" href="#easing-variables"><span>Easing variables</span></a> <a class="sidebar__link" href="#height-transition"><span>Height transition</span></a> <a class="sidebar__link" href="#hover-underline-animation"><span>Hover underline animation</span></a> </section> <section data-type="interactivity" class="sidebar__section"> <h4 class="sidebar__section-heading">interactivity</h4> <a class="sidebar__link" href="#disable-selection"><span>Disable selection</span></a> <a class="sidebar__link" href="#mouse-cursor-gradient-tracking"><span>Mouse cursor gradient tracking</span></a> <a class="sidebar__link" href="#popout-menu"><span>Popout menu</span></a> <a class="sidebar__link" href="#sibling-fade"><span>Sibling fade</span></a> </section> <section data-type="other" class="sidebar__section"> <h4 class="sidebar__section-heading">other</h4> <a class="sidebar__link" href="#counter"><span>Counter</span></a> <a class="sidebar__link" href="#custom-variables"><span>Custom variables</span></a> </section> </div> </nav> <div class="content-wrapper"> <header class="header"> <div class="container"> <img class="header__logo" draggable="false" src="e5b4b9ce166177b1bc2dc3772012e0a1.png"> <h1 class="header__heading">30 Seconds of <strong class="header__css">CSS</strong></h1> <p class="header__description"> A curated collection of useful CSS snippets you can understand in 30 seconds or less. </p> <div class="header__github-button-wrapper"> <a class="github-button header__github-button" href="https://github.com/atomiks/30-seconds-of-css" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star atomiks/30-seconds-of-css on GitHub">Star</a> </div> </div> </header> <main class="main" id="main"> <div class="container"> <nav class="tags" aria-label="Filter by tags"> <button class="tags__tag is-large is-active" data-type="all"> <i data-feather="check"></i>all</button> <button class="tags__tag is-large " data-type="layout"> <i data-feather="layout"></i>layout</button> <button class="tags__tag is-large " data-type="visual"> <i data-feather="eye"></i>visual</button> <button class="tags__tag is-large " data-type="animation"> <i data-feather="loader"></i>animation</button> <button class="tags__tag is-large " data-type="interactivity"> <i data-feather="edit-2"></i>interactivity</button> <button class="tags__tag is-large " data-type="other"> <i data-feather="tag"></i>other</button> </nav> <div class="snippet"> <h3 id="bouncing-loader"><span>Bouncing loader</span><span class="tags__tag snippet__tag" data-type="animation"><i data-feather="loader"></i>animation</span></h3> <p>Creates a bouncing loader animation.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="bouncing-loader">
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">@keyframes bouncing-loader {
|
|
from {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
to {
|
|
opacity: 0.1;
|
|
transform: translateY(-1rem);
|
|
}
|
|
}
|
|
.bouncing-loader {
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
.bouncing-loader > div {
|
|
width: 1rem;
|
|
height: 1rem;
|
|
margin: 3rem 0.2rem;
|
|
background: #8385aa;
|
|
border-radius: 50%;
|
|
animation: bouncing-loader 0.6s infinite alternate;
|
|
}
|
|
.bouncing-loader > div:nth-child(2) {
|
|
animation-delay: 0.2s;
|
|
}
|
|
.bouncing-loader > div:nth-child(3) {
|
|
animation-delay: 0.4s;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__bouncing-loader"> <div></div> <div></div> <div></div> </div> </div> <style>@keyframes a{0%{opacity:1;transform:translateY(0)}to{opacity:.1;transform:translateY(-1rem)}}.snippet-demo__bouncing-loader{display:flex;justify-content:center}.snippet-demo__bouncing-loader>div{width:1rem;height:1rem;margin:3rem .2rem;background:#8385aa;border-radius:50%;animation:a .6s infinite alternate}.snippet-demo__bouncing-loader>div:nth-child(2){animation-delay:.2s}.snippet-demo__bouncing-loader>div:nth-child(3){animation-delay:.4s}.snippet-demo__box-sizing-reset{box-sizing:border-box;width:200px;padding:1.5em;color:#7983ff;font-family:sans-serif;background-color:#fff;border:5px solid}.snippet-demo__circle{border-radius:50%;width:2rem;height:2rem;background:#333}.snippet-demo__clearfix:after{content:"";display:block;clear:both}.snippet-demo__floated{float:left}.snippet-demo__constant-width-to-height-ratio{background:#333;width:50%}.snippet-demo__constant-width-to-height-ratio:before{content:"";padding-top:100%;float:left}.snippet-demo__constant-width-to-height-ratio:after{content:"";display:block;clear:both}.snippet-demo__countable-section ul{counter-reset:a;list-style-type:none}.snippet-demo__countable-section li:before{counter-increment:a;content:counters(a,".") " "}.snippet-demo__custom-scrollbar{height:100px;overflow:auto}.snippet-demo__custom-scrollbar::-webkit-scrollbar{width:8px}.snippet-demo__custom-scrollbar::-webkit-scrollbar-track{box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px}.snippet-demo__custom-scrollbar::-webkit-scrollbar-thumb{border-radius:10px;box-shadow:inset 0 0 6px rgba(0,0,0,.5)}.snippet-demo__custom-text-selection::selection{background:#ff1493;color:#fff}.snippet-demo__custom-text-selection::-moz-selection{background:#ff1493;color:#fff}.snippet-demo__custom-variables{--some-color:#686868;--some-keyword:italic;--some-size:1.25em;--some-complex-value:1px 1px 2px #f5f5f5,0 0 1em #708090,0 0 0.2em #708090}.snippet-demo__custom-variables p{color:var(--some-color);font-size:var(--some-size);font-style:var(--some-keyword);text-shadow:var(--some-complex-value)}.snippet-demo__disable-selection{user-select:none}@keyframes b{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}.snippet-demo__donut-spinner{display:inline-block;border:4px solid rgba(0,0,0,.1);border-left-color:#7983ff;border-radius:50%;width:30px;height:30px;animation:b 1.2s linear infinite}.snippet-demo__dynamic-shadow-parent{position:relative;z-index:1}.snippet-demo__dynamic-shadow{position:relative;width:10rem;height:10rem;background:linear-gradient(75deg,#6d78ff,#00ffb8)}.snippet-demo__dynamic-shadow:after{content:"";position:absolute;width:100%;height:100%;background:inherit;top:.5rem;filter:blur(.4rem);opacity:.7;z-index:-1}:root{--ease-in-quad:cubic-bezier(0.55,0.085,0.68,0.53);--ease-in-cubic:cubic-bezier(0.55,0.055,0.675,0.19);--ease-in-quart:cubic-bezier(0.895,0.03,0.685,0.22);--ease-in-quint:cubic-bezier(0.755,0.05,0.855,0.06);--ease-in-expo:cubic-bezier(0.95,0.05,0.795,0.035);--ease-in-circ:cubic-bezier(0.6,0.04,0.98,0.335);--ease-out-quad:cubic-bezier(0.25,0.46,0.45,0.94);--ease-out-cubic:cubic-bezier(0.215,0.61,0.355,1);--ease-out-quart:cubic-bezier(0.165,0.84,0.44,1);--ease-out-quint:cubic-bezier(0.23,1,0.32,1);--ease-out-expo:cubic-bezier(0.19,1,0.22,1);--ease-out-circ:cubic-bezier(0.075,0.82,0.165,1);--ease-in-out-quad:cubic-bezier(0.455,0.03,0.515,0.955);--ease-in-out-cubic:cubic-bezier(0.645,0.045,0.355,1);--ease-in-out-quart:cubic-bezier(0.77,0,0.175,1);--ease-in-out-quint:cubic-bezier(0.86,0,0.07,1);--ease-in-out-expo:cubic-bezier(1,0,0,1);--ease-in-out-circ:cubic-bezier(0.785,0.135,0.15,0.86)}.snippet-demo__easing-variables{width:75px;height:75px;background:#333;color:#fff;font-size:.8rem;font-weight:700;display:flex;justify-content:center;align-items:center;transition:transform 1s var(--ease-out-quart)}.snippet-demo__easing-variables:hover{transform:rotate(45deg)}.snippet-demo__etched-text{font-size:1.5rem;font-weight:700;color:#b8bec5;text-shadow:0 2px 0 #fff}.snippet-demo__evenly-distributed-children{display:flex;width:100%;justify-content:space-between}.snippet-demo__flexbox-centering{display:flex;justify-content:center;align-items:center;height:200px}.snippet-demo__gradient-text{background:-webkit-linear-gradient(pink,red);-webkit-text-fill-color:transparent;-webkit-background-clip:text;font-size:2rem;font-weight:700;margin:0}.snippet-demo__grid-centering{display:grid;justify-content:center;align-items:center;height:200px}.snippet-demo__grid-layout{margin:1em;display:grid;grid-gap:10px;grid-template-columns:repeat(3,1fr);grid-template-areas:"a b b" "a c c" "a d d";background-color:#fff;color:#fff}.snippet-demo__grid-layout>div{background:#333;padding:10px}.snippet-demo__grid-layout__sidebar{grid-area:a}.snippet-demo__grid-layout__content{grid-area:c}.snippet-demo__grid-layout__header{grid-area:b}.snippet-demo__grid-layout__footer{grid-area:d}.snippet-demo__hairline-border{box-shadow:0 0 0 1px}@media (min-resolution:2dppx){.snippet-demo__hairline-border{box-shadow:0 0 0 .5px}}@media (min-resolution:3dppx){.snippet-demo__hairline-border{box-shadow:0 0 0 .33333333px}}@media (min-resolution:4dppx){.snippet-demo__hairline-border{box-shadow:0 0 0 .25px}}.snippet-demo__height-transition__el{transition:max-height .5s cubic-bezier(.23,1,.32,1);overflow:hidden;max-height:0}.snippet-demo__height-transition:hover>.snippet-demo__height-transition__el{max-height:var(--max-height)}.snippet-demo__hover-underline-animation{display:inline-block;position:relative;color:#0087ca}.snippet-demo__hover-underline-animation:after{content:"";position:absolute;width:100%;transform:scaleX(0);height:2px;bottom:0;left:0;background-color:#0087ca;transform-origin:bottom right;transition:transform .25s ease-out}.snippet-demo__hover-underline-animation:hover:after{transform:scaleX(1);transform-origin:bottom left}.snippet-demo__mouse-cursor-gradient-tracking{position:relative;background:#7983ff;padding:.5rem 1rem;font-size:1.2rem;border:none;color:#fff;cursor:pointer;outline:none;overflow:hidden}.snippet-demo__mouse-cursor-gradient-tracking span{position:relative}.snippet-demo__mouse-cursor-gradient-tracking:before{--size:0;content:"";position:absolute;left:var(--x);top:var(--y);width:var(--size);height:var(--size);background:radial-gradient(circle closest-side,aqua,rgba(0,255,255,.0001));transform:translate(-50%,-50%);transition:width .2s ease,height .2s ease}.snippet-demo__mouse-cursor-gradient-tracking:hover:before{--size:200px}.snippet-demo__css-not-selector-shortcut{display:flex;padding:0}.snippet-demo__css-not-selector-shortcut li{list-style-type:none;margin:0;padding:0 .75rem}.snippet-demo__css-not-selector-shortcut li:not(:last-child){border-right:2px solid #d2d5e4}.snippet-demo__overflow-scroll-gradient{position:relative}.snippet-demo__overflow-scroll-gradient:after{content:"";background:linear-gradient(hsla(0,0%,100%,.001),#fff);position:absolute;width:240px;height:25px;bottom:0;pointer-events:none}.snippet-demo__overflow-scroll-gradient__scroller{overflow-y:scroll;background:#fff;width:240px;height:200px;padding:15px 0;line-height:1.2;text-align:center}.snippet-demo__reference{background:linear-gradient(135deg,#ff4c9f,#ff7b74);height:75px;width:75px;position:relative;will-change:transform}.snippet-demo__popout-menu{position:absolute;visibility:hidden;left:100%;background:#333;color:#fff;font-size:.9rem;padding:.4rem .8rem;width:100px;text-align:center}.snippet-demo__reference:hover>.snippet-demo__popout-menu{visibility:visible}.snippet-demo__pretty-text-underline{display:inline;text-shadow:1px 1px 0 #f5f6f9,-1px 1px 0 #f5f6f9,-1px -1px 0 #f5f6f9,1px -1px 0 #f5f6f9;background-image:linear-gradient(90deg,currentColor 100%,transparent 0);background-position:bottom;background-repeat:no-repeat;background-size:100% 1px}.snippet-demo__pretty-text-underline::-moz-selection{background-color:rgba(0,150,255,.3);text-shadow:none}.snippet-demo__pretty-text-underline::selection{background-color:rgba(0,150,255,.3);text-shadow:none}.snippet-demo__reset-all-styles{all:initial}.snippet-demo__shape-separator{position:relative;height:48px;margin:-.75rem -1.25rem}.snippet-demo__shape-separator:after{content:"";background-image:url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsMTItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+);position:absolute;width:100%;height:24px;bottom:0}.snippet-demo__sibling-fade{cursor:default;line-height:2;vertical-align:middle}.snippet-demo__sibling-fade span{padding:1rem;transition:opacity .2s}.snippet-demo__sibling-fade:hover span:not(:hover){opacity:.5}.snippet-demo__system-font-stack{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,Helvetica,Arial,sans-serif}.snippet-demo__triangles{display:flex;align-items:center}.snippet-demo__triangle{display:inline-block;width:0;height:0;margin-right:.25rem}.snippet-demo__triangle-1{border-top:20px solid #333}.snippet-demo__triangle-1,.snippet-demo__triangle-2{border-left:20px solid transparent;border-right:20px solid transparent}.snippet-demo__triangle-2{border-bottom:20px solid #333}.snippet-demo__triangle-3{border-left:20px solid #333}.snippet-demo__triangle-3,.snippet-demo__triangle-4{border-top:20px solid transparent;border-bottom:20px solid transparent}.snippet-demo__triangle-4{border-right:20px solid #333}.snippet-demo__triangle-5{border-top:40px solid #333;border-left:15px solid transparent;border-right:15px solid transparent}.snippet-demo__truncate-text{overflow:hidden;white-space:nowrap;text-overflow:ellipsis;width:200px;margin:0}</style> <h4>Explanation</h4> <p>Note: <code>1rem</code> is usually <code>16px</code>.</p> <ol> <li> <p><code>@keyframes</code> defines an animation that has two states, where the element changes <code>opacity</code> and is translated up on the 2D plane using <code>transform: translateY()</code>.</p> </li> <li> <p><code>.bouncing-loader</code> is the parent container of the bouncing circles and uses <code>display: flex</code> and <code>justify-content: center</code> to position them in the center.</p> </li> <li> <p><code>.bouncing-loader > div</code>, targets the three child <code>div</code>s of the parent to be styled. The <code>div</code>s are given a width and height of <code>1rem</code>, using <code>border-radius: 50%</code> to turn them from squares to circles.</p> </li> <li> <p><code>margin: 3rem 0.2rem</code> specifies that each circle has a top/bottom margin of <code>3rem</code> and left/right margin of <code>0.2rem</code> so that they do not directly touch each other, giving them some breathing room.</p> </li> <li> <p><code>animation</code> is a shorthand property for the various animation properties: <code>animation-name</code>, <code>animation-duration</code>, <code>animation-iteration-count</code>, <code>animation-direction</code> are used.</p> </li> <li> <p><code>nth-child(n)</code> targets the element which is the nth child of its parent.</p> </li> <li> <p><code>animation-delay</code> is used on the second and third <code>div</code> respectively, so that each element does not start the animation at the same time.</p> </li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.3% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-animation" target="_blank">https://caniuse.com/#feat=css-animation</a> </li> </ul> </div> <div class="snippet"> <h3 id="box-sizing-reset"><span>Box-sizing reset</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Resets the box-model so that <code>width</code>s and <code>height</code>s are not affected by their <code>border</code>s or <code>padding</code>.</p> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">html {
|
|
box-sizing: border-box;
|
|
}
|
|
*,
|
|
*::before,
|
|
*::after {
|
|
box-sizing: inherit;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__box-sizing-reset">Demo</div> </div> <h4>Explanation</h4> <ol> <li><code>box-sizing: border-box</code> makes the addition of <code>padding</code> or <code>border</code>s not affect an element's <code>width</code> or <code>height</code>.</li> <li><code>box-sizing: inherit</code> makes an element respect its parent's <code>box-sizing</code> rule.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css3-boxsizing" target="_blank">https://caniuse.com/#feat=css3-boxsizing</a> </li> </ul> </div> <div class="snippet"> <h3 id="circle"><span>Circle</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Creates a circle shape with pure CSS.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="circle"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.circle {
|
|
border-radius: 50%;
|
|
width: 2rem;
|
|
height: 2rem;
|
|
background: #333;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__circle"></div> </div> <h4>Explanation</h4> <p><code>border-radius: 50%</code> curves the borders of an element to create a circle.</p> <p>Since a circle has the same radius at any given point, the <code>width</code> and <code>height</code> must be the same. Differing values will create an ellipse.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.5% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=border-radius" target="_blank">https://caniuse.com/#feat=border-radius</a> </li> </ul> </div> <div class="snippet"> <h3 id="clearfix"><span>Clearfix</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Ensures that an element self-clears its children.</p> <h6>Note: This is only useful if you are still using float to build layouts. Please consider using a modern approach with flexbox layout or grid layout.</h6> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="clearfix">
|
|
<div class="floated">float a</div>
|
|
<div class="floated">float b</div>
|
|
<div class="floated">float c</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.clearfix::after {
|
|
content: '';
|
|
display: block;
|
|
clear: both;
|
|
}
|
|
.floated {
|
|
float: left;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__clearfix"> <div class="snippet-demo__floated">float a</div> <div class="snippet-demo__floated">float b</div> <div class="snippet-demo__floated">float c</div> </div> </div> <h4>Explanation</h4> <ol> <li><code>.clearfix::after</code> defines a pseudo-element.</li> <li><code>content: ''</code> allows the pseudo-element to affect layout.</li> <li><code>clear: both</code> indicates that the left, right or both sides of the element cannot be adjacent to earlier floated elements within the same block formatting context.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 99+% </div> </div> <p><span class="snippet__support-note">⚠️ For this snippet to work properly you need to ensure that there are no non-floating children in the container and that there are no tall floats before the clearfixed container but in the same formatting context (e.g. floated columns).</span></p> </div> <div class="snippet"> <h3 id="constant-width-to-height-ratio"><span>Constant width to height ratio</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Given an element of variable width, it will ensure its height remains proportionate in a responsive fashion (i.e., its width to height ratio remains constant).</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="constant-width-to-height-ratio"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.constant-width-to-height-ratio {
|
|
background: #333;
|
|
width: 50%;
|
|
}
|
|
.constant-width-to-height-ratio::before {
|
|
content: '';
|
|
padding-top: 100%;
|
|
float: left;
|
|
}
|
|
.constant-width-to-height-ratio::after {
|
|
content: '';
|
|
display: block;
|
|
clear: both;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <p>Resize your browser window to see the proportion of the element remain the same.</p> <div class="snippet-demo"> <div class="snippet-demo__constant-width-to-height-ratio"></div> </div> <h4>Explanation</h4> <p><code>padding-top</code> on the <code>::before</code> pseudo-element causes the height of the element to equal a percentage of its width. <code>100%</code> therefore means the element's height will always be <code>100%</code> of the width, creating a responsive square. </p> <p>This method also allows content to be placed inside the element normally.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 99+% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> </div> <div class="snippet"> <h3 id="counter"><span>Counter</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span><span class="tags__tag snippet__tag" data-type="other"><i data-feather="tag"></i>other</span></h3> <p>Counters are, in essence, variables maintained by CSS whose values may be incremented by CSS rules to track how many times they're used.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><ul>
|
|
<li>List item</li>
|
|
<li>List item</li>
|
|
<li>List item</li>
|
|
</ul>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">ul {
|
|
counter-reset: counter;
|
|
}
|
|
li::before {
|
|
counter-increment: counter;
|
|
content: counters(counter, '.') ' ';
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__countable-section"> <ul> <li>List item</li> <li>List item</li> <li> List item <ul> <li>List item</li> <li>List item</li> <li>List item</li> </ul> </li> </ul> </div> </div> <h4>Explanation</h4> <p>You can create a ordered list using any type of HTML.</p> <ol> <li> <p><code>counter-reset</code> Initializes a counter, the value is the name of the counter. By default, the counter starts in 0. This property can also be used to change its value to any specific number.</p> </li> <li> <p><code>counter-increment</code> Used in element that will be countable. Once <code>counter-reset</code> initialized, a counter's value can be increased or decreased.</p> </li> <li> <p><code>counter(name, style)</code> Displays the value of a section counter. Generally used in a <code>content</code> property. This function can recieve two parameters, the first as the name of the counter and the second one can be <code>decimal</code> or <code>upper-roman</code> (<code>decimal</code> by default).</p> </li> <li> <p><code>counters(counter, string, style)</code> Displays the value of a section counter. Generally used in a <code>content</code> property. This function can recieve three parameters, the first as the name of the counter, the second one you can include a string which comes after the counter and the third one can be <code>decimal</code> or <code>upper-roman</code> (<code>decimal</code> by default).</p> </li> <li> <p>A CSS counter can be especially useful for making outlined lists, because a new instance of the counter is automatically created in child elements. Using the <code>counters()</code> function, separating text can be inserted between different levels of nested counters.</p> </li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-counters" target="_blank">https://caniuse.com/#feat=css-counters</a> </li> </ul> </div> <div class="snippet"> <h3 id="custom-scrollbar"><span>Custom scrollbar</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="custom-scrollbar">
|
|
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit?</p>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">/* Document scrollbar */
|
|
::-webkit-scrollbar {
|
|
width: 8px;
|
|
}
|
|
::-webkit-scrollbar-track {
|
|
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
|
|
border-radius: 10px;
|
|
}
|
|
::-webkit-scrollbar-thumb {
|
|
border-radius: 10px;
|
|
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
|
|
}
|
|
/* Scrollable element */
|
|
.some-element::webkit-scrollbar {
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__custom-scrollbar"> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit? </p> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit? </p> </div> </div> <h4>Explanation</h4> <ol> <li><code>::-webkit-scrollbar</code> targets the whole scrollbar element.</li> <li><code>::-webkit-scrollbar-track</code> targets only the scrollbar track.</li> <li><code>::-webkit-scrollbar-thumb</code> targets the scrollbar thumb.</li> </ol> <p>There are many other pseudo-elements that you can use to style scrollbars. For more info, visit the <a href="https://webkit.org/blog/363/styling-scrollbars/" target="_blank">WebKit Blog</a> </p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 88.0% </div> </div> <p><span class="snippet__support-note">⚠️ Scrollbar styling doesn't appear to be on any standards track.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-scrollbar" target="_blank">https://caniuse.com/#feat=css-scrollbar</a> </li> </ul> </div> <div class="snippet"> <h3 id="custom-text-selection"><span>Custom text selection</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Changes the styling of text selection.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="custom-text-selection">Select some of this text.</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">::selection {
|
|
background: aquamarine;
|
|
color: black;
|
|
}
|
|
.custom-text-selection::selection {
|
|
background: deeppink;
|
|
color: white;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__custom-text-selection">Select some of this text.</p> </div> <h4>Explanation</h4> <p><code>::selection</code> defines a pseudo selector on an element to style text within it when selected. Note that if you don't combine any other selector your style will be applied at document root level, to any selectable element.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 84.9% </div> </div> <p><span class="snippet__support-note">⚠️ Requires prefixes for full support and is not actually in any specification.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-selection" target="_blank">https://caniuse.com/#feat=css-selection</a> </li> </ul> </div> <div class="snippet"> <h3 id="custom-variables"><span>Custom variables</span><span class="tags__tag snippet__tag" data-type="other"><i data-feather="tag"></i>other</span></h3> <p>CSS variables that contain specific values to be reused throughout a document.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="custom-variables">CSS is awesome!</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">:root {
|
|
--some-color: #da7800;
|
|
--some-keyword: italic;
|
|
--some-size: 1.25em;
|
|
--some-complex-value: 1px 1px 2px whitesmoke, 0 0 1em slategray, 0 0 0.2em slategray;
|
|
}
|
|
.custom-variables {
|
|
color: var(--some-color);
|
|
font-size: var(--some-size);
|
|
font-style: var(--some-keyword);
|
|
text-shadow: var(--some-complex-value);
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__custom-variables"> <p>CSS is awesome!</p> </div> </div> <h4>Explanation</h4> <p>The variables are defined globally within the <code>:root</code> CSS pseudo-class which matches the root element of a tree representing the document. Variables can also be scoped to a selector if defined within the block.</p> <p>Declare a variable with <code>--variable-name:</code>.</p> <p>Reuse variables throughout the document using the <code>var(--variable-name)</code> function.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 88.0% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-variables" target="_blank">https://caniuse.com/#feat=css-variables</a> </li> </ul> </div> <div class="snippet"> <h3 id="disable-selection"><span>Disable selection</span><span class="tags__tag snippet__tag" data-type="interactivity"><i data-feather="edit-2"></i>interactivity</span></h3> <p>Makes the content unselectable.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p>You can select me.</p>
|
|
<p class="unselectable">You can't select me!</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.unselectable {
|
|
user-select: none;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p>You can select me.</p> <p class="snippet-demo__disable-selection">You can't select me!</p> </div> <h4>Explanation</h4> <p><code>user-select: none</code> specifies that the text cannot be selected.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 87.2% </div> </div> <p><span class="snippet__support-note">⚠️ Requires prefixes for full support.</span> <span class="snippet__support-note">⚠️ This is not a secure method to prevent users from copying content.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=user-select-none" target="_blank">https://caniuse.com/#feat=user-select-none</a> </li> </ul> </div> <div class="snippet"> <h3 id="donut-spinner"><span>Donut spinner</span><span class="tags__tag snippet__tag" data-type="animation"><i data-feather="loader"></i>animation</span></h3> <p>Creates a donut spinner that can be used to indicate the loading of content.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="donut"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">@keyframes donut-spin {
|
|
0% {
|
|
transform: rotate(0deg);
|
|
}
|
|
100% {
|
|
transform: rotate(360deg);
|
|
}
|
|
}
|
|
.donut {
|
|
display: inline-block;
|
|
border: 4px solid rgba(0, 0, 0, 0.1);
|
|
border-left-color: #7983ff;
|
|
border-radius: 50%;
|
|
width: 30px;
|
|
height: 30px;
|
|
animation: donut-spin 1.2s linear infinite;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__donut-spinner"></div> </div> <h4>Explanation</h4> <p>Use a semi-transparent <code>border</code> for the whole element, except one side that will serve as the loading indicator for the donut. Use <code>animation</code> to rotate the element.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.3% </div> </div> <p><span class="snippet__support-note">⚠️ Requires prefixes for full support.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-animation" target="_blank">https://caniuse.com/#feat=css-animation</a> </li> <li> <a href="https://caniuse.com/#feat=transforms2d" target="_blank">https://caniuse.com/#feat=transforms2d</a> </li> </ul> </div> <div class="snippet"> <h3 id="dynamic-shadow"><span>Dynamic shadow</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Creates a shadow similar to <code>box-shadow</code> but based on the colors of the element itself.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="dynamic-shadow-parent">
|
|
<div class="dynamic-shadow"></div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.dynamic-shadow-parent {
|
|
position: relative;
|
|
z-index: 1;
|
|
}
|
|
.dynamic-shadow {
|
|
position: relative;
|
|
width: 10rem;
|
|
height: 10rem;
|
|
background: linear-gradient(75deg, #6d78ff, #00ffb8);
|
|
}
|
|
.dynamic-shadow::after {
|
|
content: '';
|
|
width: 100%;
|
|
height: 100%;
|
|
position: absolute;
|
|
background: inherit;
|
|
top: 0.5rem;
|
|
filter: blur(0.4rem);
|
|
opacity: 0.7;
|
|
z-index: -1;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__dynamic-shadow-parent"> <div class="snippet-demo__dynamic-shadow"></div> </div> </div> <h4>Explanation</h4> <p>The snippet requires a somewhat complex case of stacking contexts to get right, such that the pseudo-element will be positioned underneath the element itself while still being visible.</p> <ol> <li><code>position: relative</code> on the parent establishes a Cartesian positioning context for child elements.</li> <li><code>z-index: 1</code> establishes a new stacking context.</li> <li><code>position: relative</code> on the child establishes a positioning context for pseudo-elements.</li> <li><code>::after</code> defines a pseudo-element.</li> <li><code>position: absolute</code> takes the pseudo element out of the flow of the document and positions it in relation to the parent.</li> <li><code>width: 100%</code> and <code>height: 100%</code> sizes the pseudo-element to fill its parent's dimensions, making it equal in size.</li> <li><code>background: inherit</code> causes the pseudo-element to inherit the linear gradient specified on the element.</li> <li><code>top: 0.5rem</code> offsets the pseudo-element down slightly from its parent.</li> <li><code>filter: blur(0.4rem)</code> will blur the pseudo-element to create the appearance of a shadow underneath.</li> <li><code>opacity: 0.7</code> makes the pseudo-element partially transparent.</li> <li><code>z-index: -1</code> positions the pseudo-element behind the parent.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 91.7% </div> </div> <p><span class="snippet__support-note">⚠️ Requires prefixes for full support.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-filters" target="_blank">https://caniuse.com/#feat=css-filters</a> </li> </ul> </div> <div class="snippet"> <h3 id="easing-variables"><span>Easing variables</span><span class="tags__tag snippet__tag" data-type="animation"><i data-feather="loader"></i>animation</span></h3> <p>Variables that can be reused for <code>transition-timing-function</code> properties, more powerful than the built-in <code>ease</code>, <code>ease-in</code>, <code>ease-out</code> and <code>ease-in-out</code>.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="easing-variables"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">:root {
|
|
--ease-in-quad: cubic-bezier(0.55, 0.085, 0.68, 0.53);
|
|
--ease-in-cubic: cubic-bezier(0.55, 0.055, 0.675, 0.19);
|
|
--ease-in-quart: cubic-bezier(0.895, 0.03, 0.685, 0.22);
|
|
--ease-in-quint: cubic-bezier(0.755, 0.05, 0.855, 0.06);
|
|
--ease-in-expo: cubic-bezier(0.95, 0.05, 0.795, 0.035);
|
|
--ease-in-circ: cubic-bezier(0.6, 0.04, 0.98, 0.335);
|
|
--ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
|
|
--ease-out-cubic: cubic-bezier(0.215, 0.61, 0.355, 1);
|
|
--ease-out-quart: cubic-bezier(0.165, 0.84, 0.44, 1);
|
|
--ease-out-quint: cubic-bezier(0.23, 1, 0.32, 1);
|
|
--ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1);
|
|
--ease-out-circ: cubic-bezier(0.075, 0.82, 0.165, 1);
|
|
--ease-in-out-quad: cubic-bezier(0.455, 0.03, 0.515, 0.955);
|
|
--ease-in-out-cubic: cubic-bezier(0.645, 0.045, 0.355, 1);
|
|
--ease-in-out-quart: cubic-bezier(0.77, 0, 0.175, 1);
|
|
--ease-in-out-quint: cubic-bezier(0.86, 0, 0.07, 1);
|
|
--ease-in-out-expo: cubic-bezier(1, 0, 0, 1);
|
|
--ease-in-out-circ: cubic-bezier(0.785, 0.135, 0.15, 0.86);
|
|
}
|
|
.easing-variables {
|
|
width: 50px;
|
|
height: 50px;
|
|
background: #333;
|
|
transition: transform 1s var(--ease-out-quart);
|
|
}
|
|
.easing-variables:hover {
|
|
transform: rotate(45deg);
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__easing-variables">Hover</div> </div> <h4>Explanation</h4> <p>The variables are defined globally within the <code>:root</code> CSS pseudo-class which matches the root element of a tree representing the document. In HTML, <code>:root</code> represents the <code><html></code> element and is identical to the selector <code>html</code>, except that its specificity is higher.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 88.0% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-variables" target="_blank">https://caniuse.com/#feat=css-variables</a> </li> </ul> </div> <div class="snippet"> <h3 id="etched-text"><span>Etched text</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Creates an effect where text appears to be "etched" or engraved into the background.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="etched-text">I appear etched into the background.</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.etched-text {
|
|
text-shadow: 0 2px white;
|
|
font-size: 1.5rem;
|
|
font-weight: bold;
|
|
color: #b8bec5;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__etched-text">I appear etched into the background.</p> </div> <h4>Explanation</h4> <p><code>text-shadow: 0 2px white</code> creates a white shadow offset <code>0px</code> horizontally and <code>2px</code> vertically from the origin position.</p> <p>The background must be darker than the shadow for the effect to work.</p> <p>The text color should be slightly faded to make it look like it's engraved/carved out of the background.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.1% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-textshadow" target="_blank">https://caniuse.com/#feat=css-textshadow</a> </li> </ul> </div> <div class="snippet"> <h3 id="evenly-distributed-children"><span>Evenly distributed children</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Evenly distributes child elements within a parent element.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="evenly-distributed-children">
|
|
<p>Item1</p>
|
|
<p>Item2</p>
|
|
<p>Item3</p>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.evenly-distributed-children {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__evenly-distributed-children"> <p>Item1</p> <p>Item2</p> <p>Item3</p> </div> </div> <h4>Explanation</h4> <ol> <li><code>display: flex</code> enables flexbox.</li> <li><code>justify-content: space-between</code> evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.</li> </ol> <p>Alternatively, use <code>justify-content: space-around</code> to distribute the children with space around them, rather than between them.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.1% </div> </div> <p><span class="snippet__support-note">⚠️ Needs prefixes for full support.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=flexbox" target="_blank">https://caniuse.com/#feat=flexbox</a> </li> </ul> </div> <div class="snippet"> <h3 id="flexbox-centering"><span>Flexbox centering</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Horizontally and vertically centers a child element within a parent element using <code>flexbox</code>.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="flexbox-centering">
|
|
<div class="child">Centered content.</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.flexbox-centering {
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__flexbox-centering"> <p class="snippet-demo__flexbox-centering__child">Centered content.</p> </div> </div> <h4>Explanation</h4> <ol> <li><code>display: flex</code> enables flexbox.</li> <li><code>justify-content: center</code> centers the child horizontally.</li> <li><code>align-items: center</code> centers the child vertically.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.1% </div> </div> <p><span class="snippet__support-note">⚠️ Needs prefixes for full support.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=flexbox" target="_blank">https://caniuse.com/#feat=flexbox</a> </li> </ul> </div> <div class="snippet"> <h3 id="gradient-text"><span>Gradient text</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Gives text a gradient color.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="gradient-text">Gradient text</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.gradient-text {
|
|
background: -webkit-linear-gradient(pink, red);
|
|
-webkit-text-fill-color: transparent;
|
|
-webkit-background-clip: text;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__gradient-text"> Gradient text </p> </div> <h4>Explanation</h4> <ol> <li><code>background: -webkit-linear-gradient(...)</code> gives the text element a gradient background.</li> <li><code>webkit-text-fill-color: transparent</code> fills the text with a transparent color.</li> <li><code>webkit-background-clip: text</code> clips the background with the text, filling the text with the gradient background as the color.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 91.5% </div> </div> <p><span class="snippet__support-note">⚠️ Uses non-standard properties.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=text-stroke" target="_blank">https://caniuse.com/#feat=text-stroke</a> </li> </ul> </div> <div class="snippet"> <h3 id="grid-centering"><span>Grid centering</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Horizontally and vertically centers a child element within a parent element using <code>grid</code>.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="grid-centering">
|
|
<div class="child">Centered content.</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.grid-centering {
|
|
display: grid;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__grid-centering"> <p class="snippet-demo__grid-centering__child">Centered content.</p> </div> </div> <h4>Explanation</h4> <ol> <li><code>display: grid</code> enables grid.</li> <li><code>justify-content: center</code> centers the child horizontally.</li> <li><code>align-items: center</code> centers the child vertically.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 87.6% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-grid" target="_blank">https://caniuse.com/#feat=css-grid</a> </li> </ul> </div> <div class="snippet"> <h3 id="grid-layout"><span>Grid layout</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>Basic website layout using <code>grid</code>.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="grid-layout">
|
|
<div class="header">Header</div>
|
|
<div class="sidebar">Sidebar</div>
|
|
<div class="content">
|
|
Content
|
|
<br>
|
|
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
|
|
</div>
|
|
<div class="footer">Footer</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.grid-layout {
|
|
display: grid;
|
|
grid-gap: 10px;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
grid-template-areas:
|
|
'sidebar header header'
|
|
'sidebar content content'
|
|
'sidebar footer footer';
|
|
color: white;
|
|
}
|
|
.grid-layout > div {
|
|
background: #333;
|
|
padding: 10px;
|
|
}
|
|
.sidebar {
|
|
grid-area: sidebar;
|
|
}
|
|
.content {
|
|
grid-area: content;
|
|
}
|
|
.header {
|
|
grid-area: header;
|
|
}
|
|
.footer {
|
|
grid-area: footer;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__grid-layout"> <div class="box snippet-demo__grid-layout__header">Header</div> <div class="box snippet-demo__grid-layout__sidebar">Sidebar</div> <div class="box snippet-demo__grid-layout__content">Content <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div> <div class="box snippet-demo__grid-layout__footer">Footer</div> </div> </div> <h4>Explanation</h4> <ol> <li><code>display: grid</code> enables grid.</li> <li><code>grid-gap: 10px</code> defines spacing between the elements.</li> <li><code>grid-template-columns: repeat(3, 1fr)</code> defines 3 columns of the same size.</li> <li><code>grid-template-areas</code> defines the names of grid areas.</li> <li><code>grid-area: sidebar</code> makes the element use the area with the name <code>sidebar</code>.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 87.6% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-grid" target="_blank">https://caniuse.com/#feat=css-grid</a> </li> </ul> </div> <div class="snippet"> <h3 id="hairline-border"><span>Hairline border</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="hairline-border">text</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.hairline-border {
|
|
box-shadow: 0 0 0 1px;
|
|
}
|
|
@media (min-resolution: 2dppx) {
|
|
.hairline-border {
|
|
box-shadow: 0 0 0 0.5px;
|
|
}
|
|
}
|
|
@media (min-resolution: 3dppx) {
|
|
.hairline-border {
|
|
box-shadow: 0 0 0 0.33333333px;
|
|
}
|
|
}
|
|
@media (min-resolution: 4dppx) {
|
|
.hairline-border {
|
|
box-shadow: 0 0 0 0.25px;
|
|
}
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__hairline-border">Text with a hairline border around it.</p> </div> <h4>Explanation</h4> <ol> <li><code>box-shadow</code>, when only using spread, adds a pseudo-border which can use subpixels*.</li> <li>Use <code>@media (min-resolution: ...)</code> to check the device pixel ratio (<code>1dppx</code> equals 96 DPI), setting the spread of the <code>box-shadow</code> equal to <code>1 / dppx</code>.</li> </ol> <h4>Browser Support</h4> <div> <div class="snippet__browser-support"> 95.5% </div> </div> <p><span class="snippet__support-note">⚠️ Needs alternate syntax and JavaScript user agent checking for full support.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-boxshadow" target="_blank">https://caniuse.com/#feat=css-boxshadow</a> </li> <li> <a href="https://caniuse.com/#feat=css-media-resolution" target="_blank">https://caniuse.com/#feat=css-media-resolution</a> </li> </ul> <hr> <p>*Chrome does not support subpixel values on <code>border</code>. Safari does not support subpixel values on <code>box-shadow</code>. Firefox supports subpixel values on both.</p> </div> <div class="snippet"> <h3 id="height-transition"><span>Height transition</span><span class="tags__tag snippet__tag" data-type="animation"><i data-feather="loader"></i>animation</span></h3> <p>Transitions an element's height from <code>0</code> to <code>auto</code> when its height is unknown.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="trigger">
|
|
Hover me to see a height transition.
|
|
<div class="el">content</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.el {
|
|
transition: max-height 0.5s;
|
|
overflow: hidden;
|
|
max-height: 0;
|
|
}
|
|
.trigger:hover > .el {
|
|
max-height: var(--max-height);
|
|
}
|
|
</code></pre> <h4 data-type="JavaScript">JavaScript</h4><pre><code class="lang-js">var el = document.querySelector('.el')
|
|
var height = el.scrollHeight
|
|
el.style.setProperty('--max-height', height + 'px')
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__height-transition"> Hover me to see a height transition. <div class="snippet-demo__height-transition__el"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque congue placerat nunc a volutpat. Etiam placerat libero porttitor purus facilisis vehicula. Mauris risus mauris, varius ac consequat eget, iaculis non enim. Proin ut nunc ac massa iaculis sodales id mattis enim. Cras non diam ac quam pharetra fermentum vel ac nulla. Suspendisse ligula urna, porta non lobortis non, lobortis vel velit. Fusce lectus justo, aliquet eu fringilla auctor, sodales eu orci. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </div> </div> </div> <h4>Explanation</h4> <h5 data-type="CSS">CSS</h5> <ol> <li><code>transition: max-height: 0.5s cubic-bezier(...)</code> specifies that changes to <code>max-height</code> should be transitioned over 0.5 seconds, using an <code>ease-out-quint</code> timing function.</li> <li><code>overflow: hidden</code> prevents the contents of the hidden element from overflowing its container.</li> <li><code>max-height: 0</code> specifies that the element has no height initially.</li> <li><code>.target:hover > .el</code> specifies that when the parent is hovered over, target a child <code>.el</code> within it and use the <code>--max-height</code> variable which was defined by JavaScript.</li> </ol> <h5 data-type="JavaScript">JavaScript</h5> <ol> <li><code>el.scrollHeight</code> is the height of the element including overflow, which will change dynamically based on the content of the element.</li> <li><code>el.style.setProperty(...)</code> sets the <code>--max-height</code> CSS variable which is used to specify the <code>max-height</code> of the element the target is hovered over, allowing it to transition smoothly from 0 to auto.</li> </ol> <h4>Browser Support</h4> <div> <div class="snippet__browser-support"> 88.0% </div> </div> <p></p> <div class="snippet__requires-javascript">Requires JavaScript</div> <span class="snippet__support-note"> ⚠️ Causes reflow on each animation frame, which will be laggy if there are a large number of elements beneath the element that is transitioning in height. </span> <p></p> <ul> <li> <a href="https://caniuse.com/#feat=css-variables" target="_blank">https://caniuse.com/#feat=css-variables</a> </li> <li> <a href="https://caniuse.com/#feat=css-transitions" target="_blank">https://caniuse.com/#feat=css-transitions</a> </li> </ul> </div> <div class="snippet"> <h3 id="hover-underline-animation"><span>Hover underline animation</span><span class="tags__tag snippet__tag" data-type="animation"><i data-feather="loader"></i>animation</span></h3> <p>Creates an animated underline effect when the text is hovered over.</p> <p> <small><strong>Credit:</strong> <a href="https://flatuicolors.com/" target="_blank">https://flatuicolors.com/</a> </small> </p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="hover-underline-animation">Hover this text to see the effect!</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.hover-underline-animation {
|
|
display: inline-block;
|
|
position: relative;
|
|
color: #0087ca;
|
|
}
|
|
.hover-underline-animation::after {
|
|
content: '';
|
|
position: absolute;
|
|
width: 100%;
|
|
transform: scaleX(0);
|
|
height: 2px;
|
|
bottom: 0;
|
|
left: 0;
|
|
background-color: #0087ca;
|
|
transform-origin: bottom right;
|
|
transition: transform 0.25s ease-out;
|
|
}
|
|
.hover-underline-animation:hover::after {
|
|
transform: scaleX(1);
|
|
transform-origin: bottom left;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__hover-underline-animation">Hover this text to see the effect!</p> </div> <h4>Explanation</h4> <ol> <li><code>display: inline-block</code> makes the block <code>p</code> an <code>inline-block</code> to prevent the underline from spanning the entire parent width rather than just the content (text).</li> <li><code>position: relative</code> on the element establishes a Cartesian positioning context for pseudo-elements.</li> <li><code>::after</code> defines a pseudo-element.</li> <li><code>position: absolute</code> takes the pseudo element out of the flow of the document and positions it in relation to the parent.</li> <li><code>width: 100%</code> ensures the pseudo-element spans the entire width of the text block.</li> <li><code>transform: scaleX(0)</code> initially scales the pseudo element to 0 so it has no width and is not visible.</li> <li><code>bottom: 0</code> and <code>left: 0</code> position it to the bottom left of the block.</li> <li><code>transition: transform 0.25s ease-out</code> means changes to <code>transform</code> will be transitioned over 0.25 seconds with an <code>ease-out</code> timing function.</li> <li><code>transform-origin: bottom right</code> means the transform anchor point is positioned at the bottom right of the block.</li> <li><code>:hover::after</code> then uses <code>scaleX(1)</code> to transition the width to 100%, then changes the <code>transform-origin</code> to <code>bottom left</code> so that the anchor point is reversed, allowing it transition out in the other direction when hovered off.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=transforms2d" target="_blank">https://caniuse.com/#feat=transforms2d</a> </li> <li> <a href="https://caniuse.com/#feat=css-transitions" target="_blank">https://caniuse.com/#feat=css-transitions</a> </li> </ul> </div> <div class="snippet"> <h3 id="mouse-cursor-gradient-tracking"><span>Mouse cursor gradient tracking</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span><span class="tags__tag snippet__tag" data-type="interactivity"><i data-feather="edit-2"></i>interactivity</span></h3> <p>A hover effect where the gradient follows the mouse cursor.</p> <p> <small class="snippet__credit"><strong>Credit:</strong> <a href="https://codepen.io/electerious/pen/MQrRxX" target="_blank">Tobias Reich</a> </small> </p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><button class="mouse-cursor-gradient-tracking">
|
|
<span>Hover me</span>
|
|
</button>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.mouse-cursor-gradient-tracking {
|
|
position: relative;
|
|
background: #7983ff;
|
|
padding: 0.5rem 1rem;
|
|
font-size: 1.2rem;
|
|
border: none;
|
|
color: white;
|
|
cursor: pointer;
|
|
outline: none;
|
|
overflow: hidden;
|
|
}
|
|
.mouse-cursor-gradient-tracking span {
|
|
position: relative;
|
|
}
|
|
.mouse-cursor-gradient-tracking::before {
|
|
--size: 0;
|
|
content: '';
|
|
position: absolute;
|
|
left: var(--x);
|
|
top: var(--y);
|
|
width: var(--size);
|
|
height: var(--size);
|
|
background: radial-gradient(circle closest-side, pink, transparent);
|
|
transform: translate(-50%, -50%);
|
|
transition: width 0.2s ease, height 0.2s ease;
|
|
}
|
|
.mouse-cursor-gradient-tracking:hover::before {
|
|
--size: 200px;
|
|
}
|
|
</code></pre> <h4 data-type="JavaScript">JavaScript</h4><pre><code class="lang-js">var btn = document.querySelector('.mouse-cursor-gradient-tracking')
|
|
btn.onmousemove = function(e) {
|
|
var x = e.pageX - btn.offsetLeft
|
|
var y = e.pageY - btn.offsetTop
|
|
btn.style.setProperty('--x', x + 'px')
|
|
btn.style.setProperty('--y', y + 'px')
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <button class="snippet-demo__mouse-cursor-gradient-tracking"> <span>Hover me</span> </button> </div> <h4>Explanation</h4> <p><em>TODO</em></p> <p><strong>Note!</strong></p> <p>If the element's parent has a positioning context (<code>position: relative</code>), you will need to subtract its offsets as well.</p> <pre><code class="lang-js">var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
|
|
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
|
|
</code></pre> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 88.0% </div> </div> <p></p> <div class="snippet__requires-javascript">Requires JavaScript</div> <span class="snippet__support-note">⚠️ Requires JavaScript.</span> <p></p> <ul> <li> <a href="https://caniuse.com/#feat=css-variables" target="_blank">https://caniuse.com/#feat=css-variables</a> </li> </ul> </div> <div class="snippet"> <h3 id="not-selector"><span>:not selector</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>The <code>:not</code> psuedo selector is useful for styling a group of elements, while leaving the last (or specified) element unstyled.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><ul class="css-not-selector-shortcut">
|
|
<li>One</li>
|
|
<li>Two</li>
|
|
<li>Three</li>
|
|
<li>Four</li>
|
|
<li>Five</li>
|
|
</ul>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.css-not-selector-shortcut {
|
|
display: flex;
|
|
}
|
|
li {
|
|
list-style-type: none;
|
|
margin: 0;
|
|
padding: 0 0.75rem;
|
|
}
|
|
li:not(:last-child) {
|
|
border-right: 2px solid #d2d5e4;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <ul class="snippet-demo__css-not-selector-shortcut"> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> <li>Five</li> </ul> </div> <h4>Explanation</h4> <p><code>li:not(:last-child)</code> specifies that the styles should apply to all <code>li</code> elements except the <code>:last-child</code>.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-sel3" target="_blank">https://caniuse.com/#feat=css-sel3</a> </li> </ul> </div> <div class="snippet"> <h3 id="overflow-scroll-gradient"><span>Overflow scroll gradient</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="overflow-scroll-gradient">
|
|
<div class="overflow-scroll-gradient__scroller">
|
|
Content to be scrolled
|
|
</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.overflow-scroll-gradient {
|
|
position: relative;
|
|
}
|
|
.overflow-scroll-gradient::after {
|
|
content: '';
|
|
position: absolute;
|
|
bottom: 0;
|
|
width: 240px;
|
|
height: 25px;
|
|
background: linear-gradient(
|
|
rgba(255, 255, 255, 0.001),
|
|
white
|
|
); /* transparent keyword is broken in Safari */
|
|
pointer-events: none;
|
|
}
|
|
.overflow-scroll-gradient__scroller {
|
|
overflow-y: scroll;
|
|
background: white;
|
|
width: 240px;
|
|
height: 200px;
|
|
padding: 15px 0;
|
|
line-height: 1.2;
|
|
text-align: center;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__overflow-scroll-gradient"> <div class="snippet-demo__overflow-scroll-gradient__scroller"> Content to be scrolled </div> </div> </div> <script>!function(){var e=document.querySelector(".snippet-demo__height-transition__el"),t=e.scrollHeight;e.style.setProperty("--max-height",t+"px")}(),function(){var e=document.querySelector(".snippet-demo__mouse-cursor-gradient-tracking");e.onmousemove=function(t){var o=t.pageX-e.offsetLeft-e.offsetParent.offsetLeft,r=t.pageY-e.offsetTop-e.offsetParent.offsetTop;e.style.setProperty("--x",o+"px"),e.style.setProperty("--y",r+"px")}}(),document.querySelector(".snippet-demo__overflow-scroll-gradient__scroller").innerHTML="content ".repeat(100);</script> <h4>Explanation</h4> <ol> <li><code>position: relative</code> on the parent establishes a Cartesian positioning context for pseudo-elements.</li> <li><code>::after</code> defines a pseudo element.</li> <li><code>background-image: linear-gradient(...)</code> adds a linear gradient that fades from transparent to white (top to bottom).</li> <li><code>position: absolute</code> takes the pseudo element out of the flow of the document and positions it in relation to the parent.</li> <li><code>width: 240px</code> matches the size of the scrolling element (which is a child of the parent that has the pseudo element).</li> <li><code>height: 25px</code> is the height of the fading gradient pseudo-element, which should be kept relatively small.</li> <li><code>bottom: 0</code> positions the pseudo-element at the bottom of the parent.</li> <li><code>pointer-events: none</code> specifies that the pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-gradients" target="_blank">https://caniuse.com/#feat=css-gradients</a> </li> </ul> </div> <div class="snippet"> <h3 id="popout-menu"><span>Popout menu</span><span class="tags__tag snippet__tag" data-type="interactivity"><i data-feather="edit-2"></i>interactivity</span></h3> <p>Reveals an interactive popout menu on hover.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="reference">
|
|
<div class="popout-menu">
|
|
Popout menu
|
|
</div>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.reference {
|
|
position: relative;
|
|
}
|
|
.popout-menu {
|
|
position: absolute;
|
|
visibility: hidden;
|
|
left: 100%;
|
|
}
|
|
.reference:hover > .popout-menu {
|
|
visibility: visible;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__reference"> <div class="snippet-demo__popout-menu"> Popout menu </div> </div> </div> <h4>Explanation</h4> <ol> <li><code>position: relative</code> on the reference parent establishes a Cartesian positioning context for its child.</li> <li><code>position: absolute</code> takes the popout menu out of the flow of the document and positions it in relation to the parent.</li> <li><code>left: 100%</code> moves the the popout menu 100% of its parent's width from the left.</li> <li><code>visibility: hidden</code> hides the popout menu initially and allows for transitions (unlike <code>display: none</code>).</li> <li><code>.reference:hover > .popout-menu</code> means that when <code>.reference</code> is hovered over, select immediate children with a class of <code>.popout-menu</code> and change their <code>visibility</code> to <code>visible</code>, which shows the popout.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 99+% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> </div> <div class="snippet"> <h3 id="pretty-text-underline"><span>Pretty text underline</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>A nicer alternative to <code>text-decoration: underline</code> where descenders do not clip the underline. Natively implemented as <code>text-decoration-skip-ink: auto</code> but it has less control over the underline.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="pretty-text-underline">Pretty text underline without clipping descending letters.</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.pretty-text-underline {
|
|
display: inline;
|
|
text-shadow: 1px 1px #f5f6f9, -1px 1px #f5f6f9, -1px -1px #f5f6f9, 1px -1px #f5f6f9;
|
|
background-image: linear-gradient(90deg, currentColor 100%, transparent 100%);
|
|
background-position: bottom;
|
|
background-repeat: no-repeat;
|
|
background-size: 100% 1px;
|
|
}
|
|
.pretty-text-underline::-moz-selection {
|
|
background-color: rgba(0, 150, 255, 0.3);
|
|
text-shadow: none;
|
|
}
|
|
.pretty-text-underline::selection {
|
|
background-color: rgba(0, 150, 255, 0.3);
|
|
text-shadow: none;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__pretty-text-underline">Pretty text underline without clipping descending letters.</p> </div> <h4>Explanation</h4> <ol> <li><code>text-shadow</code> uses 4 values with offsets that cover a 4x4 px area to ensure the underline has a "thick" shadow that covers the line where descenders clip it. Use a color that matches the background. For a larger font, use a larger <code>px</code> size. Additional values can create an even thicker shadow, and subpixel values can also be used.</li> <li><code>background-image: linear-gradient(...)</code> creates a 90deg gradient using the text color (<code>currentColor</code>).</li> <li>The <code>background-*</code> properties size the gradient as 100% of the width of the block and 1px in height at the bottom and disables repetition, which creates a 1px underline beneath the text.</li> <li>The <code>::selection</code> pseudo selector rule ensures the text shadow does not interfere with text selection. </li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-textshadow" target="_blank">https://caniuse.com/#feat=css-textshadow</a> </li> <li> <a href="https://caniuse.com/#feat=css-gradients" target="_blank">https://caniuse.com/#feat=css-gradients</a> </li> </ul> </div> <div class="snippet"> <h3 id="reset-all-styles"><span>Reset all styles</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Resets all styles to default values with one property. This will not affect <code>direction</code> and <code>unicode-bidi</code> properties.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="reset-all-styles">
|
|
<h4>Title</h4>
|
|
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit?</p>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.reset-all-styles {
|
|
all: initial;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__reset-all-styles"> <h3>Title</h3> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit?</p> </div> </div> <h4>Explanation</h4> <p>The <code>all</code> property allows you to reset all styles (inherited or not) to default values.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 88.3% </div> </div> <p><span class="snippet__support-note">⚠️ MS Edge status is under consideration.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-all" target="_blank">https://caniuse.com/#feat=css-all</a> </li> </ul> </div> <div class="snippet"> <h3 id="shape-separator"><span>Shape separator</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Uses an SVG shape to separate two different blocks to create more a interesting visual appearance compared to standard horizontal separation.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="shape-separator"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.shape-separator {
|
|
position: relative;
|
|
height: 48px;
|
|
background: #333;
|
|
}
|
|
.shape-separator::after {
|
|
content: '';
|
|
background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsMTItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+);
|
|
position: absolute;
|
|
width: 100%;
|
|
height: 24px;
|
|
bottom: 0;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo is-distinct"> <div class="snippet-demo__shape-separator"></div> </div> <h4>Explanation</h4> <ol> <li><code>position: relative</code> on the element establishes a Cartesian positioning context for pseudo elements.</li> <li><code>::after</code> defines a pseudo element.</li> <li><code>background-image: url(...)</code> adds the SVG shape (a 24x24 triangle in base64 format) as the background image of the pseudo element, which repeats by default. It must be the same color as the block that is being separated. </li> <li><code>position: absolute</code> takes the pseudo element out of the flow of the document and positions it in relation to the parent.</li> <li><code>width: 100%</code> ensures the element stretches the entire width of its parent.</li> <li><code>height: 24px</code> is the same height as the shape.</li> <li><code>bottom: 0</code> positions the pseudo element at the bottom of the parent.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.3% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=svg" target="_blank">https://caniuse.com/#feat=svg</a> </li> </ul> </div> <div class="snippet"> <h3 id="sibling-fade"><span>Sibling fade</span><span class="tags__tag snippet__tag" data-type="interactivity"><i data-feather="edit-2"></i>interactivity</span></h3> <p>Fades out the siblings of a hovered item.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="sibling-fade">
|
|
<span>Item 1</span>
|
|
<span>Item 2</span>
|
|
<span>Item 3</span>
|
|
<span>Item 4</span>
|
|
<span>Item 5</span>
|
|
<span>Item 6</span>
|
|
</div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">span {
|
|
padding: 0 1rem;
|
|
transition: opacity 0.2s;
|
|
}
|
|
.sibling-fade:hover span:not(:hover) {
|
|
opacity: 0.5;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <nav class="snippet-demo__sibling-fade"> <span>Item 1</span> <span>Item 2</span> <span>Item 3</span> <span>Item 4</span> <span>Item 5</span> <span>Item 6</span> </nav> </div> <h4>Explanation</h4> <ol> <li><code>transition: opacity 0.2s</code> specifies that changes to opacity will be transitioned over 0.2 seconds.</li> <li><code>.sibling-fade:hover span:not(:hover)</code> specifies that when the parent is hovered, select any <code>span</code> children that are not currently being hovered and change their opacity to <code>0.5</code>.</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 95.4% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=css-sel3" target="_blank">https://caniuse.com/#feat=css-sel3</a> </li> <li> <a href="https://caniuse.com/#feat=css-transitions" target="_blank">https://caniuse.com/#feat=css-transitions</a> </li> </ul> </div> <div class="snippet"> <h3 id="system-font-stack"><span>System font stack</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Uses the native font of the operating system to get close to a native app feel.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="system-font-stack">This text uses the system font.</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.system-font-stack {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu,
|
|
Cantarell, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__system-font-stack">This text uses the system font.</p> </div> <h4>Explanation</h4> <p>The browser looks for each successive font, preferring the first one if possible, and falls back to the next if it cannot find the font (on the system or defined in CSS).</p> <ol> <li><code>-apple-system</code> is San Francisco, used on iOS and macOS (not Chrome however)</li> <li><code>BlinkMacSystemFont</code> is San Francisco, used on macOS Chrome</li> <li><code>Segoe UI</code> is used on Windows 10</li> <li><code>Roboto</code> is used on Android</li> <li><code>Oxygen-Sans</code> is used on GNU+Linux</li> <li><code>Ubuntu</code> is used on Linux</li> <li><code>"Helvetica Neue"</code> and <code>Helvetica</code> is used on macOS 10.10 and below (wrapped in quotes because it has a space)</li> <li><code>Arial</code> is a font widely supported by all operating systems</li> <li><code>sans-serif</code> is the fallback sans-serif font if none of the other fonts are supported</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 99+% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> </div> <div class="snippet"> <h3 id="triangle"><span>Triangle</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3> <p>Creates a triangle shape with pure CSS.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><div class="triangle"></div>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.triangle {
|
|
width: 0;
|
|
height: 0;
|
|
border-top: 20px solid #333;
|
|
border-left: 20px solid transparent;
|
|
border-right: 20px solid transparent;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <div class="snippet-demo__triangles"> <div class="snippet-demo__triangle snippet-demo__triangle-1"></div> <div class="snippet-demo__triangle snippet-demo__triangle-2"></div> <div class="snippet-demo__triangle snippet-demo__triangle-3"></div> <div class="snippet-demo__triangle snippet-demo__triangle-4"></div> <div class="snippet-demo__triangle snippet-demo__triangle-5"></div> </div> </div> <h4>Explanation</h4> <p> <a href="https://stackoverflow.com/q/7073484" target="_blank">View this link for a detailed explanation.</a> </p> <p>The color of the border is the color of the triangle. The side the triangle tip points corresponds to the opposite <code>border-*</code> property. For example, a color on <code>border-top</code> means the arrow points downward.</p> <p>Experiment with the <code>px</code> values to change the proportion of the triangle.</p> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 99+% </div> </div> <p><span class="snippet__support-note">✅ No caveats.</span></p> </div> <div class="snippet"> <h3 id="truncate-text"><span>Truncate text</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3> <p>If the text is longer than one line, it will be truncated and end with an ellipsis <code>…</code>.</p> <h4 data-type="HTML">HTML</h4><pre><code class="lang-html"><p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
|
|
</code></pre> <h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.truncate-text {
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
width: 200px;
|
|
}
|
|
</code></pre> <h4>Demo</h4> <div class="snippet-demo"> <p class="snippet-demo__truncate-text"> This text will be truncated if it exceeds 200px in width. </p> </div> <h4>Explanation</h4> <ol> <li><code>overflow: hidden</code> prevents the text from overflowing its dimensions (for a block, 100% width and auto height).</li> <li><code>white-space: nowrap</code> prevents the text from exceeding one line in height.</li> <li><code>text-overflow: ellipsis</code> makes it so that if the text exceeds its dimensions, it will end with an ellipsis.</li> <li><code>width: 200px;</code> ensures the element has a dimension, to know when to get ellipsis</li> </ol> <h4>Browser support</h4> <div> <div class="snippet__browser-support"> 98.4% </div> </div> <p><span class="snippet__support-note">⚠️ Only works for single line elements.</span></p> <ul> <li> <a href="https://caniuse.com/#feat=text-overflow" target="_blank">https://caniuse.com/#feat=text-overflow</a> </li> </ul> </div> </div> </main> </div> </body> </html> |