加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.html 119.23 KB
一键复制 编辑 原始数据 按行查看 历史
atomiks 提交于 2018-10-05 14:11 . rebuild docs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
<!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="./favicon-32x32.png">
<script src="./src/js/index.js" defer=""></script>
<script async="" defer="" src="https://buttons.github.io/buttons.js"></script>
</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="#display-table-centering"><span>Display table centering</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="#last-item-with-remaining-available-height">
<img alt="New" draggable="false" class="sidebar__new" src="./src/img/new.svg"><span>Last item with remaining available height</span></a>
<a class="sidebar__link" href="#transform-centering"><span>Transform centering</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="#offscreen"><span>Offscreen</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>
<a class="sidebar__link" href="#toggle-switch">
<img alt="New" draggable="false" class="sidebar__new" src="./src/img/new.svg"><span>Toggle switch</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__leaves" draggable="false" id="header__leaves1" src="./src/img/leaves.svg" alt="">
<img class="header__leaves" draggable="false" id="header__leaves2" src="./src/img/leaves.svg" alt="">
<img id="header__blob" draggable="false" src="./src/img/blob.png" alt="">
<div class="header__content">
<img class="header__logo" draggable="false" src="./src/img/logo.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/30-seconds/30-seconds-of-css" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star 30-seconds/30-seconds-of-css on GitHub">Star</a>
</div>
</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">&lt;div class="bouncing-loader"&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
</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 &gt; div {
width: 1rem;
height: 1rem;
margin: 3rem 0.2rem;
background: #8385aa;
border-radius: 50%;
animation: bouncing-loader 0.6s infinite alternate;
}
.bouncing-loader &gt; div:nth-child(2) {
animation-delay: 0.2s;
}
.bouncing-loader &gt; div:nth-child(3) {
animation-delay: 0.4s;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="bouncing-loader.md">
<div class="bouncing-loader">
<div></div>
<div></div>
<div></div>
</div>
</div>
<style>
@keyframes bouncing-loader {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0.1;
transform: translateY(-1rem);
}
}
[data-scope="bouncing-loader.md"] .bouncing-loader {
display: flex;
justify-content: center;
}
[data-scope="bouncing-loader.md"] .bouncing-loader>div {
width: 1rem;
height: 1rem;
margin: 3rem 0.2rem;
background: #8385aa;
border-radius: 50%;
animation: bouncing-loader 0.6s infinite alternate;
}
[data-scope="bouncing-loader.md"] .bouncing-loader>div:nth-child(2) {
animation-delay: 0.2s;
}
[data-scope="bouncing-loader.md"] .bouncing-loader>div:nth-child(3) {
animation-delay: 0.4s;
}
</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 &gt; 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>
<!-- tags: animation -->
</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="HTML">HTML</h4><pre><code class="lang-html">&lt;div class="box"&gt;border-box&lt;/div&gt;
&lt;div class="box content-box"&gt;content-box&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.box {
display: inline-block;
width: 150px;
height: 150px;
padding: 10px;
background: tomato;
color: white;
border: 10px solid red;
}
.content-box {
box-sizing: content-box;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="box-sizing-reset.md">
<div class="box">border-box</div>
<div class="box content-box">content-box</div>
</div>
<style>
[data-scope="box-sizing-reset.md"] html {
box-sizing: border-box;
}
[data-scope="box-sizing-reset.md"] *,
[data-scope="box-sizing-reset.md"] *::before,
[data-scope="box-sizing-reset.md"] *::after {
box-sizing: inherit;
}
[data-scope="box-sizing-reset.md"] .box {
display: inline-block;
width: 150px;
height: 150px;
padding: 10px;
background: tomato;
color: white;
border: 10px solid red;
}
[data-scope="box-sizing-reset.md"] .content-box {
box-sizing: content-box;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;div class="circle"&gt;&lt;/div&gt;
</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" data-scope="circle.md">
<div class="circle"></div>
</div>
<style>
[data-scope="circle.md"] .circle {
border-radius: 50%;
width: 2rem;
height: 2rem;
background: #333;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="clearfix"&gt;
&lt;div class="floated"&gt;float a&lt;/div&gt;
&lt;div class="floated"&gt;float b&lt;/div&gt;
&lt;div class="floated"&gt;float c&lt;/div&gt;
&lt;/div&gt;
</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" data-scope="clearfix.md">
<div class="clearfix">
<div class="floated">float a</div>
<div class="floated">float b</div>
<div class="floated">float c</div>
</div>
</div>
<style>
[data-scope="clearfix.md"] .clearfix::after {
content: '';
display: block;
clear: both;
}
[data-scope="clearfix.md"] .floated {
float: left;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;div class="constant-width-to-height-ratio"&gt;&lt;/div&gt;
</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>
<div class="snippet-demo" data-scope="constant-width-to-height-ratio.md">
<div class="constant-width-to-height-ratio"></div>
</div>
<style>
[data-scope="constant-width-to-height-ratio.md"] .constant-width-to-height-ratio {
background: #333;
width: 50%;
}
[data-scope="constant-width-to-height-ratio.md"] .constant-width-to-height-ratio::before {
content: '';
padding-top: 100%;
float: left;
}
[data-scope="constant-width-to-height-ratio.md"] .constant-width-to-height-ratio::after {
content: '';
display: block;
clear: both;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;ul&gt;
&lt;li&gt;List item&lt;/li&gt;
&lt;li&gt;List item&lt;/li&gt;
&lt;li&gt;
List item
&lt;ul&gt;
&lt;li&gt;List item&lt;/li&gt;
&lt;li&gt;List item&lt;/li&gt;
&lt;li&gt;List item&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
</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" data-scope="counter.md">
<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>
<style>
[data-scope="counter.md"] ul {
counter-reset: counter;
}
[data-scope="counter.md"] li::before {
counter-increment: counter;
content: counters(counter, ".") " ";
}
</style>
<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 receive 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 receive 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>
<!-- tags: visual, other -->
</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">&lt;div class="custom-scrollbar"&gt;
&lt;p&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit.&lt;br&gt;
Iure id exercitationem nulla qui repellat laborum vitae, &lt;br&gt;
molestias tempora velit natus. Quas, assumenda nisi. &lt;br&gt;
Quisquam enim qui iure, consequatur velit sit?
&lt;/p&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.custom-scrollbar {
height: 70px;
overflow-y: scroll;
}
/* To style the document scrollbar, remove `.custom-scrollbar` */
.custom-scrollbar::-webkit-scrollbar {
width: 8px;
}
.custom-scrollbar::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="custom-scrollbar.md">
<div class="custom-scrollbar">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
<br> Iure id exercitationem nulla qui repellat laborum vitae,
<br> molestias tempora velit natus. Quas, assumenda nisi.
<br> Quisquam enim qui iure, consequatur velit sit?
</p>
</div>
</div>
<style>
[data-scope="custom-scrollbar.md"] {
/* To style the document scrollbar, remove `.custom-scrollbar` */
}
[data-scope="custom-scrollbar.md"] .custom-scrollbar {
height: 70px;
overflow-y: scroll;
}
[data-scope="custom-scrollbar.md"] .custom-scrollbar::-webkit-scrollbar {
width: 8px;
}
[data-scope="custom-scrollbar.md"] .custom-scrollbar::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
}
[data-scope="custom-scrollbar.md"] .custom-scrollbar::-webkit-scrollbar-thumb {
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;p class="custom-text-selection"&gt;Select some of this text.&lt;/p&gt;
</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" data-scope="custom-text-selection.md">
<p class="custom-text-selection">Select some of this text.</p>
</div>
<style>
[data-scope="custom-text-selection.md"] ::selection {
background: aquamarine;
color: black;
}
[data-scope="custom-text-selection.md"] .custom-text-selection::selection {
background: deeppink;
color: white;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;p class="custom-variables"&gt;CSS is awesome!&lt;/p&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">:root {
/* Place variables within here to use the variables globally. */
}
.custom-variables {
--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;
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" data-scope="custom-variables.md">
<p class="custom-variables">CSS is awesome!</p>
</div>
<style>
[data-scope="custom-variables.md"] :root {
/* Place variables within here to use the variables globally. */
}
[data-scope="custom-variables.md"] .custom-variables {
--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;
color: var(--some-color);
font-size: var(--some-size);
font-style: var(--some-keyword);
text-shadow: var(--some-complex-value);
}
</style>
<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>
<!-- tags: other -->
</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">&lt;p&gt;You can select me.&lt;/p&gt;
&lt;p class="unselectable"&gt;You can't select me!&lt;/p&gt;
</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" data-scope="disable-selection.md">
<p>You can select me.</p>
<p class="unselectable">You can't select me!</p>
</div>
<style>
[data-scope="disable-selection.md"] .unselectable {
user-select: none;
}
</style>
<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>
<!-- tags: interactivity -->
</div>
<div class="snippet">
<h3 id="display-table-centering"><span>Display table centering</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3>
<p>Vertically and horizontally centers a child element within its parent element using <code>display: table</code> (as an alternative to <code>flexbox</code>).</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;div class="container"&gt;
&lt;div class="center"&gt;
&lt;span&gt;Centered content&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.container {
border: 1px solid #333;
height: 250px;
width: 250px;
}
.center {
display: table;
height: 100%;
width: 100%;
}
.center &gt; span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="display-table-centering.md">
<div class="container">
<div class="center">
<span>Centered content</span>
</div>
</div>
</div>
<style>
[data-scope="display-table-centering.md"] .container {
border: 1px solid #333;
height: 250px;
width: 250px;
}
[data-scope="display-table-centering.md"] .center {
display: table;
height: 100%;
width: 100%;
}
[data-scope="display-table-centering.md"] .center>span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
<h4>Explanation</h4>
<ol>
<li><code>display: table</code> on '.center' allows the element to behave like a <code>&lt;table&gt;</code> HTML element.</li>
<li>100% height and width on '.center' allows the element to fill the available space within its parent element.</li>
<li><code>display: table-cell</code> on '.center &gt; span' allows the element to behave like an HTML element.</li>
<li><code>text-align: center</code> on '.center &gt; span' centers the child element horizontally.</li>
<li><code>vertical-align: middle</code> on '.center &gt; span' centers the child element vertically.</li>
</ol>
<p>The outer parent ('.container' in this case) must have a fixed height and width.</p>
<h4>Browser support</h4>
<div>
<div class="snippet__browser-support">
99+%
</div>
</div>
<p><span class="snippet__support-note">✅ No caveats.</span></p>
<ul>
<li>
<a href="https://caniuse.com/#search=display%3A%20table" target="_blank">https://caniuse.com/#search=display%3A%20table</a>
</li>
</ul>
<!-- tags: layout -->
</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">&lt;div class="donut"&gt;&lt;/div&gt;
</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" data-scope="donut-spinner.md">
<div class="donut"></div>
</div>
<style>
@keyframes donut-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
[data-scope="donut-spinner.md"] .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;
}
</style>
<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>
<!-- tags: animation -->
</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">&lt;div class="dynamic-shadow"&gt;&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.dynamic-shadow {
position: relative;
width: 10rem;
height: 10rem;
background: linear-gradient(75deg, #6d78ff, #00ffb8);
z-index: 1;
}
.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" data-scope="dynamic-shadow.md">
<div class="dynamic-shadow"></div>
</div>
<style>
[data-scope="dynamic-shadow.md"] .dynamic-shadow {
position: relative;
width: 10rem;
height: 10rem;
background: linear-gradient(75deg, #6d78ff, #00ffb8);
z-index: 1;
}
[data-scope="dynamic-shadow.md"] .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;
}
</style>
<h4>Explanation</h4>
<ol>
<li><code>position: relative</code> on the element establishes a Cartesian positioning context for psuedo-elements.</li>
<li><code>z-index: 1</code> establishes a new stacking context.</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 but in front of the background.</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>
<!-- tags: visual -->
</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">&lt;div class="easing-variables"&gt;Hover&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">:root {
/* Place variables in here to use globally */
}
.easing-variables {
--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);
display: inline-block;
width: 75px;
height: 75px;
padding: 10px;
color: white;
line-height: 50px;
text-align: center;
background: #333;
transition: transform 1s var(--ease-out-quart);
}
.easing-variables:hover {
transform: rotate(45deg);
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="easing-variables.md">
<div class="easing-variables">Hover</div>
</div>
<style>
[data-scope="easing-variables.md"] :root {
/* Place variables in here to use globally */
}
[data-scope="easing-variables.md"] .easing-variables {
--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);
display: inline-block;
width: 75px;
height: 75px;
padding: 10px;
color: white;
line-height: 50px;
text-align: center;
background: #333;
transition: transform 1s var(--ease-out-quart);
}
[data-scope="easing-variables.md"] .easing-variables:hover {
transform: rotate(45deg);
}
</style>
<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>&lt;html&gt;</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>
<!-- tags: animation -->
</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">&lt;p class="etched-text"&gt;I appear etched into the background.&lt;/p&gt;
</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" data-scope="etched-text.md">
<p class="etched-text">I appear etched into the background.</p>
</div>
<style>
[data-scope="etched-text.md"] .etched-text {
text-shadow: 0 2px white;
font-size: 1.5rem;
font-weight: bold;
color: #b8bec5;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="evenly-distributed-children"&gt;
&lt;p&gt;Item1&lt;/p&gt;
&lt;p&gt;Item2&lt;/p&gt;
&lt;p&gt;Item3&lt;/p&gt;
&lt;/div&gt;
</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" data-scope="evenly-distributed-children.md">
<div class="evenly-distributed-children">
<p>Item1</p>
<p>Item2</p>
<p>Item3</p>
</div>
</div>
<style>
[data-scope="evenly-distributed-children.md"] .evenly-distributed-children {
display: flex;
justify-content: space-between;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;div class="flexbox-centering"&gt;
&lt;div class="child"&gt;Centered content.&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="flexbox-centering.md">
<div class="flexbox-centering">
<div class="child">Centered content.</div>
</div>
</div>
<style>
[data-scope="flexbox-centering.md"] .flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;p class="gradient-text"&gt;Gradient text&lt;/p&gt;
</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" data-scope="gradient-text.md">
<p class="gradient-text">Gradient text</p>
</div>
<style>
[data-scope="gradient-text.md"] .gradient-text {
background: -webkit-linear-gradient(pink, red);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="grid-centering"&gt;
&lt;div class="child"&gt;Centered content.&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.grid-centering {
display: grid;
justify-content: center;
align-items: center;
height: 100px;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="grid-centering.md">
<div class="grid-centering">
<div class="child">Centered content.</div>
</div>
</div>
<style>
[data-scope="grid-centering.md"] .grid-centering {
display: grid;
justify-content: center;
align-items: center;
height: 100px;
}
</style>
<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>
<!-- tags: layout -->
</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">&lt;div class="hairline-border"&gt;text&lt;/div&gt;
</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" data-scope="hairline-border.md">
<div class="hairline-border">text</div>
</div>
<style>
[data-scope="hairline-border.md"] .hairline-border {
box-shadow: 0 0 0 1px;
}
@media (min-resolution: 2dppx) {
[data-scope="hairline-border.md"] .hairline-border {
box-shadow: 0 0 0 0.5px;
}
}
@media (min-resolution: 3dppx) {
[data-scope="hairline-border.md"] .hairline-border {
box-shadow: 0 0 0 0.33333333px;
}
}
@media (min-resolution: 4dppx) {
[data-scope="hairline-border.md"] .hairline-border {
box-shadow: 0 0 0 0.25px;
}
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="trigger"&gt;
Hover me to see a height transition.
&lt;div class="el"&gt;content&lt;/div&gt;
&lt;/div&gt;
</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 &gt; .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" data-scope="height-transition.md">
<div class="trigger">
Hover me to see a height transition.
<div class="el">content</div>
</div>
</div>
<style>
[data-scope="height-transition.md"] .el {
transition: max-height 0.5s;
overflow: hidden;
max-height: 0;
}
[data-scope="height-transition.md"] .trigger:hover>.el {
max-height: var(--max-height);
}
</style>
<script>
(function() {
var el = document.querySelector('.el')
var height = el.scrollHeight
el.style.setProperty('--max-height', height + 'px')
})();
</script>
<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 &gt; .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>
<!-- tags: animation -->
</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">&lt;p class="hover-underline-animation"&gt;Hover this text to see the effect!&lt;/p&gt;
</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" data-scope="hover-underline-animation.md">
<p class="hover-underline-animation">Hover this text to see the effect!</p>
</div>
<style>
[data-scope="hover-underline-animation.md"] .hover-underline-animation {
display: inline-block;
position: relative;
color: #0087ca;
}
[data-scope="hover-underline-animation.md"] .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;
}
[data-scope="hover-underline-animation.md"] .hover-underline-animation:hover::after {
transform: scaleX(1);
transform-origin: bottom left;
}
</style>
<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>
<!-- tags: animation -->
</div>
<div class="snippet">
<img alt="New" draggable="false" class="snippet__new" src="./src/img/new.svg">
<h3 id="last-item-with-remaining-available-height"><span>Last item with remaining available height</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3>
<p>Take advantage of available viewport space by giving the last element the remaining available space in current viewport, even when resizing the window.</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;div class="container"&gt;
&lt;div&gt;Div 1&lt;/div&gt;
&lt;div&gt;Div 2&lt;/div&gt;
&lt;div&gt;Div 3&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">html,
body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
display: flex;
flex-direction: column;
}
.container &gt; div:last-child {
background-color: tomato;
flex: 1;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="last-item-with-remaining-available-height.md">
<div class="container">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
</div>
</div>
<style>
[data-scope="last-item-with-remaining-available-height.md"] html,
[data-scope="last-item-with-remaining-available-height.md"] body {
height: 100%;
margin: 0;
}
[data-scope="last-item-with-remaining-available-height.md"] .container {
height: 100%;
display: flex;
flex-direction: column;
}
[data-scope="last-item-with-remaining-available-height.md"] .container>div:last-child {
background-color: tomato;
flex: 1;
}
</style>
<h4>Explanation</h4>
<ol>
<li><code>height: 100%</code> set the height of container as viewport height.</li>
<li><code>display: flex</code> enables flexbox.</li>
<li><code>flex-direction: column</code> set the direction of flex items' order from top to down.</li>
<li><code>flex-grow: 1</code> the flexbox will apply remaining available space of container to last child element.</li>
</ol>
<p>The parent must have a viewport height. <code>flex-grow: 1</code> could be applied to the first or second element, which will have all available space.</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>
<!-- tags: layout -->
<!-- date: 2018-09-30 -->
</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">&lt;button class="mouse-cursor-gradient-tracking"&gt;
&lt;span&gt;Hover me&lt;/span&gt;
&lt;/button&gt;
</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 - btn.offsetParent.offsetLeft
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
btn.style.setProperty('--x', x + 'px')
btn.style.setProperty('--y', y + 'px')
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="mouse-cursor-gradient-tracking.md">
<button class="mouse-cursor-gradient-tracking">
<span>Hover me</span>
</button>
</div>
<style>
[data-scope="mouse-cursor-gradient-tracking.md"] .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;
}
[data-scope="mouse-cursor-gradient-tracking.md"] .mouse-cursor-gradient-tracking span {
position: relative;
}
[data-scope="mouse-cursor-gradient-tracking.md"] .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;
}
[data-scope="mouse-cursor-gradient-tracking.md"] .mouse-cursor-gradient-tracking:hover::before {
--size: 200px;
}
</style>
<script>
(function() {
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
btn.onmousemove = function(e) {
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
btn.style.setProperty('--x', x + 'px')
btn.style.setProperty('--y', y + 'px')
}
})();
</script>
<h4>Explanation</h4>
<p><em>TODO</em></p>
<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>
<!-- tags: visual, interactivity -->
</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">&lt;ul class="css-not-selector-shortcut"&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;li&gt;Four&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.css-not-selector-shortcut {
display: flex;
}
ul {
padding-left: 0;
}
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" data-scope="not-selector.md">
<ul class="css-not-selector-shortcut">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</div>
<style>
[data-scope="not-selector.md"] .css-not-selector-shortcut {
display: flex;
}
[data-scope="not-selector.md"] ul {
padding-left: 0;
}
[data-scope="not-selector.md"] li {
list-style-type: none;
margin: 0;
padding: 0 0.75rem;
}
[data-scope="not-selector.md"] li:not(:last-child) {
border-right: 2px solid #d2d5e4;
}
</style>
<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>
<!-- tags: visual -->
</div>
<div class="snippet">
<h3 id="offscreen"><span>Offscreen</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span><span class="tags__tag snippet__tag" data-type="visual"><i data-feather="eye"></i>visual</span></h3>
<p>A bulletproof way to completely hide an element visually and positionally in the DOM while still allowing it to be accessed by JavaScript and readable by screen readers. This method is very useful for accessibility (
<a href="https://adata.org/learn-about-ada"
target="_blank">ADA</a>) development when more context is needed for visually-impaired users. As an alternative to <code>display: none</code> which is not readable by screen readers or <code>visibility: hidden</code> which takes up physical space in the
DOM.</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;a class="button" href="http://pantswebsite.com"&gt;
Learn More
&lt;span class="offscreen"&gt; about pants&lt;/span&gt;
&lt;/a&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.offscreen {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="offscreen.md">
<a class="button" href="http://pantswebsite.com">
Learn More
<span class="offscreen"> about pants</span>
</a>
</div>
<style>
[data-scope="offscreen.md"] .offscreen {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
</style>
<h4>Explanation</h4>
<ol>
<li>Remove all borders.</li>
<li>Use <code>clip</code> to indicate that no part of the element should be shown.</li>
<li>Make the height and width of the element 1px.</li>
<li>Negate the elements height and width using <code>margin: -1px</code>.</li>
<li>Hide the element's overflow.</li>
<li>Remove all padding.</li>
<li>Position the element absolutely so that it does not take up space in the DOM.</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>
<p>(Although <code>clip</code> technically has been depreciated, the newer <code>clip-path</code> currently has very limited browser support.)</p>
<ul>
<li>
<a href="https://caniuse.com/#search=clip" target="_blank">https://caniuse.com/#search=clip</a>
</li>
</ul>
<!-- tags: layout, visual -->
</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">&lt;div class="overflow-scroll-gradient"&gt;
&lt;div class="overflow-scroll-gradient__scroller"&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit. &lt;br&gt;
Iure id exercitationem nulla qui repellat laborum vitae, &lt;br&gt;
molestias tempora velit natus. Quas, assumenda nisi. &lt;br&gt;
Quisquam enim qui iure, consequatur velit sit? &lt;br&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit.&lt;br&gt;
Iure id exercitationem nulla qui repellat laborum vitae, &lt;br&gt;
molestias tempora velit natus. Quas, assumenda nisi. &lt;br&gt;
Quisquam enim qui iure, consequatur velit sit?
&lt;/div&gt;
&lt;/div&gt;
</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;
line-height: 1.2;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="overflow-scroll-gradient.md">
<div class="overflow-scroll-gradient">
<div class="overflow-scroll-gradient__scroller">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
<br> Iure id exercitationem nulla qui repellat laborum vitae,
<br> molestias tempora velit natus. Quas, assumenda nisi.
<br> Quisquam enim qui iure, consequatur velit sit?
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit.
<br> Iure id exercitationem nulla qui repellat laborum vitae,
<br> molestias tempora velit natus. Quas, assumenda nisi.
<br> Quisquam enim qui iure, consequatur velit sit?
</div>
</div>
</div>
<style>
[data-scope="overflow-scroll-gradient.md"] .overflow-scroll-gradient {
position: relative;
}
[data-scope="overflow-scroll-gradient.md"] .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;
}
[data-scope="overflow-scroll-gradient.md"] .overflow-scroll-gradient__scroller {
overflow-y: scroll;
background: white;
width: 240px;
height: 200px;
padding: 15px;
line-height: 1.2;
}
</style>
<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>
<!-- tags: visual -->
</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 and focus.</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;div class="reference" tabindex="0"&gt;
&lt;div class="popout-menu"&gt;
Popout menu
&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.reference {
position: relative;
background: tomato;
width: 100px;
height: 100px;
}
.popout-menu {
position: absolute;
visibility: hidden;
left: 100%;
background: #333;
color: white;
padding: 15px;
}
.reference:hover &gt; .popout-menu,
.reference:focus &gt; .popout-menu {
visibility: visible;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="popout-menu.md">
<div class="reference" tabindex="0">
<div class="popout-menu">
Popout menu
</div>
</div>
</div>
<style>
[data-scope="popout-menu.md"] .reference {
position: relative;
background: tomato;
width: 100px;
height: 100px;
}
[data-scope="popout-menu.md"] .popout-menu {
position: absolute;
visibility: hidden;
left: 100%;
background: #333;
color: white;
padding: 15px;
}
[data-scope="popout-menu.md"] .reference:hover>.popout-menu,
[data-scope="popout-menu.md"] .reference:focus>.popout-menu {
visibility: visible;
}
</style>
<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 &gt; .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>
<!-- tags: interactivity -->
</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">&lt;p class="pretty-text-underline"&gt;Pretty text underline without clipping descending letters.&lt;/p&gt;
</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" data-scope="pretty-text-underline.md">
<p class="pretty-text-underline">Pretty text underline without clipping descending letters.</p>
</div>
<style>
[data-scope="pretty-text-underline.md"] .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;
}
[data-scope="pretty-text-underline.md"] .pretty-text-underline::-moz-selection {
background-color: rgba(0, 150, 255, 0.3);
text-shadow: none;
}
[data-scope="pretty-text-underline.md"] .pretty-text-underline::selection {
background-color: rgba(0, 150, 255, 0.3);
text-shadow: none;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="reset-all-styles"&gt;
&lt;h5&gt;Title&lt;/h5&gt;
&lt;p&gt;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?&lt;/p&gt;
&lt;/div&gt;
</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" data-scope="reset-all-styles.md">
<div class="reset-all-styles">
<h5>Title</h5>
<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>
<style>
[data-scope="reset-all-styles.md"] .reset-all-styles {
all: initial;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;div class="shape-separator"&gt;&lt;/div&gt;
</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,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 12'%3E%3Cpath d='m12 0l12 12h-24z' fill='%23fff'/%3E%3C/svg%3E");
position: absolute;
width: 100%;
height: 12px;
bottom: 0;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="shape-separator.md">
<div class="shape-separator"></div>
</div>
<style>
[data-scope="shape-separator.md"] .shape-separator {
position: relative;
height: 48px;
background: #333;
}
[data-scope="shape-separator.md"] .shape-separator::after {
content: '';
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 12'%3E%3Cpath d='m12 0l12 12h-24z' fill='%23fff'/%3E%3C/svg%3E");
position: absolute;
width: 100%;
height: 12px;
bottom: 0;
}
</style>
<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 24x12 triangle) 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. For other shapes, we can
use
<a href="http://yoksel.github.io/url-encoder/" target="_blank">the URL-encoder for SVG</a>.</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: 12px</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>
<!-- tags: visual -->
</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">&lt;div class="sibling-fade"&gt;
&lt;span&gt;Item 1&lt;/span&gt;
&lt;span&gt;Item 2&lt;/span&gt;
&lt;span&gt;Item 3&lt;/span&gt;
&lt;span&gt;Item 4&lt;/span&gt;
&lt;span&gt;Item 5&lt;/span&gt;
&lt;span&gt;Item 6&lt;/span&gt;
&lt;/div&gt;
</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" data-scope="sibling-fade.md">
<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>
</div>
<style>
[data-scope="sibling-fade.md"] span {
padding: 0 1rem;
transition: opacity 0.2s;
}
[data-scope="sibling-fade.md"] .sibling-fade:hover span:not(:hover) {
opacity: 0.5;
}
</style>
<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>
<!-- tags: interactivity -->
</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">&lt;p class="system-font-stack"&gt;This text uses the system font.&lt;/p&gt;
</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" data-scope="system-font-stack.md">
<p class="system-font-stack">This text uses the system font.</p>
</div>
<style>
[data-scope="system-font-stack.md"] .system-font-stack {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
</style>
<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>
<!-- tags: visual -->
</div>
<div class="snippet">
<img alt="New" draggable="false" class="snippet__new" src="./src/img/new.svg">
<h3 id="toggle-switch"><span>Toggle switch</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>Creates a toggle switch with CSS only.</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;input type="checkbox" id="toggle" class="offscreen" /&gt;
&lt;label for="toggle" class="switch"&gt;&lt;/label&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
background-color: rgba(0, 0, 0, 0.25);
border-radius: 20px;
transition: all 0.3s;
}
.switch::after {
content: '';
position: absolute;
width: 18px;
height: 18px;
border-radius: 18px;
background-color: white;
top: 1px;
left: 1px;
transition: all 0.3s;
}
input[type='checkbox']:checked + .switch::after {
transform: translateX(20px);
}
input[type='checkbox']:checked + .switch {
background-color: #7983ff;
}
.offscreen {
position: absolute;
left: -9999px;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="toggle-switch.md">
<input type="checkbox" id="toggle" class="offscreen">
<label for="toggle" class="switch"></label>
</div>
<style>
[data-scope="toggle-switch.md"] .switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
background-color: rgba(0, 0, 0, 0.25);
border-radius: 20px;
transition: all 0.3s;
}
[data-scope="toggle-switch.md"] .switch::after {
content: '';
position: absolute;
width: 18px;
height: 18px;
border-radius: 18px;
background-color: white;
top: 1px;
left: 1px;
transition: all 0.3s;
}
[data-scope="toggle-switch.md"] input[type='checkbox']:checked+.switch::after {
transform: translateX(20px);
}
[data-scope="toggle-switch.md"] input[type='checkbox']:checked+.switch {
background-color: #7983ff;
}
[data-scope="toggle-switch.md"] .offscreen {
position: absolute;
left: -9999px;
}
</style>
<h4>Explanation</h4>
<p>This effect is styling only the <code>&lt;label&gt;</code> element to look like a toggle switch, and hiding the actual <code>&lt;input&gt;</code> checkbox by positioning it offscreen. When clicking the label associated with the <code>&lt;input&gt;</code> element, it sets the <code>&lt;input&gt;</code> checkbox into the <code>:checked</code> state.</p>
<ol>
<li>The <code>for</code> attribute associates the <code>&lt;label&gt;</code> with the appropriate <code>&lt;input&gt;</code> checkbox element by its <code>id</code>.</li>
<li><code>.switch::after</code> defines a pseudo-element for the <code>&lt;label&gt;</code> to create the circular knob.</li>
<li><code>input[type='checkbox']:checked + .switch::after</code> targets the <code>&lt;label&gt;</code>'s pseudo-element's style when the checkbox is <code>checked</code>.</li>
<li><code>transform: translateX(20px)</code> moves the pseudo-element (knob) 20px to the right when the checkbox is <code>checked</code>.</li>
<li><code>background-color: #7983ff;</code> sets the background-color of the switch to a different color when the checkbox is <code>checked</code>.</li>
<li><code>.offscreen</code> moves the <code>&lt;input&gt;</code> checkbox element, which does not comprise any part of the actual toggle switch, out of the flow of document and positions it far away from the view, but does not hide it so it
is accessible via keyboard and screen readers.</li>
<li><code>transition: all 0.3s</code> specifies all property changes will be transitioned over 0.3 seconds, therefore transitioning the <code>&lt;label&gt;</code>'s <code>background-color</code> and the pseudo-element's <code>transform</code> property when the checkbox is checked.</li>
</ol>
<h4>Browser support</h4>
<div>
<div class="snippet__browser-support">
95.5%
</div>
</div>
<p><span class="snippet__support-note">⚠️ Requires prefixes for full support.</span></p>
<ul>
<li>
<a href="https://caniuse.com/#feat=transforms2d" target="_blank">https://caniuse.com/#feat=transforms2d</a>
</li>
</ul>
<!-- tags: visual, interactivity -->
<!-- date: 2018-10-03 -->
</div>
<div class="snippet">
<h3 id="transform-centering"><span>Transform centering</span><span class="tags__tag snippet__tag" data-type="layout"><i data-feather="layout"></i>layout</span></h3>
<p>Vertically and horizontally centers a child element within its parent element using <code>position: absolute</code> and <code>transform: translate()</code> (as an alternative to <code>flexbox</code> or <code>display: table</code>). Similar
to <code>flexbox</code>, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.</p>
<h4 data-type="HTML">HTML</h4><pre><code class="lang-html">&lt;div class="parent"&gt;
&lt;div class="child"&gt;Centered content&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<h4 data-type="CSS">CSS</h4><pre><code class="lang-css">.parent {
border: 1px solid #333;
height: 250px;
position: relative;
width: 250px;
}
.child {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
</code></pre>
<h4>Demo</h4>
<div class="snippet-demo" data-scope="transform-centering.md">
<div class="parent">
<div class="child">Centered content</div>
</div>
</div>
<style>
[data-scope="transform-centering.md"] .parent {
border: 1px solid #333;
height: 250px;
position: relative;
width: 250px;
}
[data-scope="transform-centering.md"] .child {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
</style>
<h4>Explanation</h4>
<ol>
<li><code>position: absolute</code> on the child element allows it to be positioned based on its containing block.</li>
<li><code>left: 50%</code> and <code>top: 50%</code> offsets the child 50% from the left and top edge of its containing block.</li>
<li><code>transform: translate(-50%, -50%)</code> allows the height and width of the child element to be negated so that it is vertically and horizontally centered.</li>
</ol>
<p>Note: Fixed height and width on parent element is for the demo only.</p>
<h4>Browser support</h4>
<div>
<div class="snippet__browser-support">
99+%
</div>
</div>
<p><span class="snippet__support-note">⚠️ Requires prefix for full support.</span></p>
<ul>
<li>
<a href="https://caniuse.com/#search=transform" target="_blank">https://caniuse.com/#search=transform</a>
</li>
</ul>
<!-- tags: layout -->
</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">&lt;div class="triangle"&gt;&lt;/div&gt;
</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" data-scope="triangle.md">
<div class="triangle"></div>
</div>
<style>
[data-scope="triangle.md"] .triangle {
width: 0;
height: 0;
border-top: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
</style>
<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>
<!-- tags: visual -->
</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">&lt;p class="truncate-text"&gt;If I exceed one line's width, I will be truncated.&lt;/p&gt;
</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" data-scope="truncate-text.md">
<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
</div>
<style>
[data-scope="truncate-text.md"] .truncate-text {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 200px;
}
</style>
<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>
<!-- tags: layout -->
</div>
</div>
</main>
</div>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化