diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..f2e31279 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["env", "stage-2"] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..17447dfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +.cache/ +.DS_Store diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..89e30332 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,44 @@ +### Snippet name + +Brief description + +#### HTML + +```html + +``` + +#### CSS + +```css + +``` + +#### Demo + + + +
=(j+=t[A].length)&&(++v,k=j);if(t[v]instanceof o||t[A-1].greedy)continue;x=A-v,w=e.slice(k,j),C.index-=k}if(C){h&&(m=C[1].length);S=(F=C.index+m)+(C=C[0].slice(m)).length;var C,N=w.slice(0,F),O=w.slice(S),E=[v,x];N&&(++v,k+=N.length,E.push(N));var $=new o(u,d?n.tokenize(C,d):C,y,C,f);if(E.push($),O&&E.push(O),Array.prototype.splice.apply(t,E),1!=x&&n.matchGrammar(e,t,a,v,k,!0,u),s)break}else if(s)break}}}}},tokenize:function(e,t,a){var r=[e],i=t.rest;if(i){for(var s in i)t[s]=i[s];delete t.rest}return n.matchGrammar(e,r,t,0,0,!1),r},hooks:{all:{},add:function(e,t){var a=n.hooks.all;a[e]=a[e]||[],a[e].push(t)},run:function(e,t){var a=n.hooks.all[e];if(a&&a.length)for(var r,i=0;r=a[i++];)r(t)}}},r=n.Token=function(e,t,a,n,r){this.type=e,this.content=t,this.alias=a,this.length=0|(n||"").length,this.greedy=!!r};if(r.stringify=function(e,t,a){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(a){return r.stringify(a,t,e)}).join("");var i={type:e.type,content:r.stringify(e.content,t,a),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:a};if(e.alias){var s="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,s)}n.hooks.run("wrap",i);var l=Object.keys(i.attributes).map(function(e){return e+'="'+(i.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+i.tag+' class="'+i.classes.join(" ")+'"'+(l?" "+l:"")+">"+i.content+""+i.tag+">"},!t.document)return t.addEventListener?(n.disableWorkerMessageHandler||t.addEventListener("message",function(e){var a=JSON.parse(e.data),r=a.language,i=a.code,s=a.immediateClose;t.postMessage(n.highlight(i,n.languages[r],r)),s&&t.close()},!1),t.Prism):t.Prism;var i=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return i&&(n.filename=i.src,n.manual||i.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.highlightAll))),t.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=a),void 0!==e&&(e.Prism=a),a.languages.markup={comment://,prolog:/<\?[\s\S]+?\?>/,doctype://i,cdata://i,tag:{pattern:/<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/i,inside:{tag:{pattern:/^<\/?[^\s>\/]+/i,inside:{punctuation:/^<\/?/,namespace:/^[^\s>\/:]+:/}},"attr-value":{pattern:/=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+)/i,inside:{punctuation:[/^=/,{pattern:/(^|[^\\])["']/,lookbehind:!0}]}},punctuation:/\/?>/,"attr-name":{pattern:/[^\s>\/]+/,inside:{namespace:/^[^\s>\/:]+:/}}}},entity:/?[\da-z]{1,8};/i},a.languages.markup.tag.inside["attr-value"].inside.entity=a.languages.markup.entity,a.hooks.add("wrap",function(e){"entity"===e.type&&(e.attributes.title=e.content.replace(/&/,"&"))}),a.languages.xml=a.languages.markup,a.languages.html=a.languages.markup,a.languages.mathml=a.languages.markup,a.languages.svg=a.languages.markup,a.languages.css={comment:/\/\*[\s\S]*?\*\//,atrule:{pattern:/@[\w-]+?.*?(?:;|(?=\s*\{))/i,inside:{rule:/@[\w-]+/}},url:/url\((?:(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|.*?)\)/i,selector:/[^{}\s][^{};]*?(?=\s*\{)/,string:{pattern:/("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,greedy:!0},property:/[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*(?=\s*:)/i,important:/\B!important\b/i,function:/[-a-z0-9]+(?=\()/i,punctuation:/[(){};:]/},a.languages.css.atrule.inside.rest=a.util.clone(a.languages.css),a.languages.markup&&(a.languages.insertBefore("markup","tag",{style:{pattern:/(
.clearfix::after
defines a pseudo element.content: ''
allows the pseudo element to affect layout.clear: both
indicates that the left, right or both sides of the element cannot be adjacent to earlier floated elements within the same block formatting context.✅ No caveats.
Changes the styling of text selection.
<p class="custom-text-selection">Select some of this text.</p>
+
.text-selection::selection {
+ background: red;
+ color: white;
+}
+
Select some of this text.
::selection
defines a pseudo selector on an element to style text within it when selected.
⚠️ Requires prefixes for full support.
Variables that can be reused for transition-timing-function
properties, more powerful than the built-in ease
, ease-in
, ease-out
and ease-in-out
.
<div class="easing-variables"></div>
+
:root {
+ --ease-in-quad: cubic-bezier(0.55, 0.085, 0.68, 0.53);
+ --ease-in-cubic: cubic-bezier(0.55, 0.055, 0.675, 0.19);
+ --ease-in-quart: cubic-bezier(0.895, 0.03, 0.685, 0.22);
+ --ease-in-quint: cubic-bezier(0.755, 0.05, 0.855, 0.06);
+ --ease-in-expo: cubic-bezier(0.95, 0.05, 0.795, 0.035);
+ --ease-in-circ: cubic-bezier(0.6, 0.04, 0.98, 0.335);
+ --ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
+ --ease-out-cubic: cubic-bezier(0.215, 0.61, 0.355, 1);
+ --ease-out-quart: cubic-bezier(0.165, 0.84, 0.44, 1);
+ --ease-out-quint: cubic-bezier(0.23, 1, 0.32, 1);
+ --ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1);
+ --ease-out-circ: cubic-bezier(0.075, 0.82, 0.165, 1);
+ --ease-in-out-quad: cubic-bezier(0.455, 0.03, 0.515, 0.955);
+ --ease-in-out-cubic: cubic-bezier(0.645, 0.045, 0.355, 1);
+ --ease-in-out-quart: cubic-bezier(0.77, 0, 0.175, 1);
+ --ease-in-out-quint: cubic-bezier(0.86, 0, 0.07, 1);
+ --ease-in-out-expo: cubic-bezier(1, 0, 0, 1);
+ --ease-in-out-circ: cubic-bezier(0.785, 0.135, 0.15, 0.86);
+}
+.easing-variables {
+ width: 50px;
+ height: 50px;
+ background: #333;
+ transition: transform 1s var(--ease-out-quart);
+}
+.easing-variables:hover {
+ transform: rotate(45deg);
+}
+
The variables are defined globally with the :root
CSS pseudo-class, which matches the root element of a tree representing the document. In HTML, :root
represents the <html>
element and is identical to the selector html, except that its specificity is higher.
✅ No caveats.
Creates an effect where text appears to be "etched" or engraved into the background.
<p class="etched-text">I appear etched into the background.</p>
+
.etched-text {
+ text-shadow: 0 2px white;
+ font-size: 1.5rem;
+ font-weight: bold;
+ color: #b8bec5;
+}
+
I appear etched into the background.
text-shadow: 0 2px white
creates a white shadow offset 0px
horizontally and 2px
vertically from the origin position.
The background must be darker than the shadow for the effect to work.
The text color should be slightly faded to make it look like it's engraved/carved out of the background.
✅ No caveats.
Gives text a gradient color.
<p class="gradient-text">Gradient text</p>
+
.gradient-text {
+ background: -webkit-linear-gradient(pink, red);
+ -webkit-text-fill-color: transparent;
+ -webkit-background-clip: text;
+}
+
Gradient text
background: -webkit-linear-gradient(...)
gives the text element a gradient background.webkit-text-fill-color: transparent
fills the text with a transparent color.webkit-background-clip: text
clips the background with the text, filling the text with the gradient background as the color.⚠️ Uses non-standard properties.
Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.
<div class="hairline-border">text</div>
+
.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;
+ }
+}
+
Text with a hairline border around it.
box-shadow
, when only using spread, adds a psuedo-border which can use subpixels*.@media (min-resolution: ...)
to check the device pixel ratio (1ddpx
equals 96 DPI), setting the spread of the box-shadow
equal to 1 / dppx
.⚠️ Needs alternate syntax and JavaScript user agent checking for full support.
*Chrome does not support subpixel values on border
. Safari does not support subpixel values on box-shadow
. Firefox supports subpixel values on both.
Horizontally and vertically centers a child element within a parent element.
<div class="horizontal-and-vertical-centering">
+ <div class="child"></div>
+</div>
+
.horizontal-and-vertical-centering {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+}
+
Centered content.
display: flex
enables flexbox.justify-content: center
centers the child horizontally.align-items: center
centers the child vertically.⚠️ Needs prefixes for full support.
A hover effect where the gradient follows the mouse cursor.
<button class="mouse-cursor-gradient-tracking">
+ <span>Hover me</span>
+</button>
+
.mouse-cursor-gradient-tracking {
+ position: relative;
+ background: #2379f7;
+ 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 .2s ease, height .2s ease;
+}
+.mouse-cursor-gradient-tracking:hover::before {
+ --size: 200px;
+}
+
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
+btn.onmousemove = function (e) {
+ var x = e.pageX - btn.offsetLeft
+ var y = e.pageY - btn.offsetTop
+ btn.style.setProperty('--x', x + 'px')
+ btn.style.setProperty('--y', y + 'px')
+}
+
TODO
Note!
If the element's parent has a positioning context (position: relative
), you will need to subtract its offsets as well.
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
+var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
+
Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.
<div class="overflow-scroll-gradient">
+ <div class="overflow-scroll-gradient__scroller">
+ Content to be scrolled
+ </div>
+</div>
+
.overflow-scroll-gradient {
+ position: relative;
+}
+.overflow-scroll-gradient::after {
+ content: '';
+ position: absolute;
+ bottom: 0;
+ width: 300px;
+ height: 25px;
+ background: linear-gradient(rgba(255, 255, 255, 0.001), white); /* transparent keyword is broken in Safari */
+}
+.overflow-scroll-gradient__scroller {
+ overflow-y: scroll;
+ background: white;
+ width: 300px;
+ height: 250px;
+ line-height: 1.2;
+ text-align: center;
+}
+
position: relative
on the parent establishes a Cartesian positioning context for psuedo elements.::after
defines a pseudo element.background-image: linear-gradient(...)
adds a linear gradient that fades from transparent to white (top to bottom).position: absolute
takes the pseudo element out of the flow of the document and positions it in relation to the parent.width: 300px
matches the size of the scrolling element (which is a child of the parent that has the pseudo element).height: 25px
is the height of the fading gradient psuedo element, which should be kept relatively small.bottom: 0
positions the pseudo element at the bottom of the parent.✅ No caveats.
Reveals an interactive popout menu on hover.
<div class="reference">
+ <div class="popout-menu">
+ Popout menu
+ </div>
+</div>
+
.reference {
+ position: relative;
+}
+.popout-menu {
+ position: absolute;
+ visibility: hidden;
+ left: 100%;
+}
+.reference:hover > .popout-menu {
+ visibility: visible;
+}
+
position: relative
on the reference parent establishes a Cartesian positioning context for its child.position: absolute
takes the popout menu out of the flow of the document and positions it in relation to the parent.left: 100%
moves the the popout menu 100% of its parent's width from the left.visibility: hidden
hides the popout menu initially and allows for transitions (unlike display: none
)..reference:hover > .popout-menu
means that when .reference
is hovered over, select immediate children with a class of .popout-menu
and change their visibility
to visible
, which shows the popout.✅ No caveats.
A nicer alternative to text-decoration: underline
where descenders do not clip the underline. Natively implemented as text-decoration-skip-ink: auto
but it has less control over the underline.
<p class="pretty-text-underline">Pretty text underline without clipping descending letters.</p>
+
.pretty-text-underline {
+ display: inline;
+ font-size: 1.25rem;
+ text-shadow: 1px 1px 0 #f5f6f9,
+ -1px 1px 0 #f5f6f9,
+ -1px -1px 0 #f5f6f9,
+ 1px -1px 0 #f5f6f9;
+ background-image: linear-gradient(90deg, currentColor 100%, transparent 100%);
+ background-position: 0 1.04em;
+ background-repeat: repeat-x;
+ background-size: 1px 1px;
+}
+.pretty-text-underline::selection {
+ background-color: rgba(0, 150, 255, 0.3);
+ text-shadow: none;
+}
+
Pretty text underline without clipping descending letters.
text-shadow: ...
has 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 px
size.background-image: linear-gradient(...)
creates a 90deg gradient with the current text color (currentColor
).background-*
properties size the gradient as 1x1px at the bottom and repeats it along the x-axis.::selection
pseudo selector ensures the text shadow does not interfere with text selection. ✅ No caveats.
Uses an SVG shape to separate two different blocks to create more a interesting visual appearance compared to standard horizontal separation.
<div class="shape-separator"></div>
+
.shape-separator {
+ position: relative;
+ height: 48px;
+}
+.shape-separator::after {
+ content: '';
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsMTItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+);
+ position: absolute;
+ width: 100%;
+ height: 24px;
+ bottom: 0;
+}
+
position: relative
on the element establishes a Cartesian positioning context for psuedo elements.::after
defines a pseudo element.background-image: url(...)
adds the SVG shape (a 24x24 triangle in base64 format) as the background image of the psuedo element, which repeats by default. It must be the same color as the block that is being separated. position: absolute
takes the pseudo element out of the flow of the document and positions it in relation to the parent.width: 100%
ensures the element stretches the entire width of its parent.height: 24px
is the same height as the shape.bottom: 0
positions the pseudo element at the bottom of the parent.✅ No caveats.
Uses the native font of the operating system to get close to a native app feel.
<p class="system-font-stack">This text uses the system font.</p>
+
.system-font-stack {
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
+}
+
This text uses the system font.
The browser looks for each successive font, preferring the first one if possible, and falls back to the next of it cannot find the font (on the system or defined in CSS).
-apple-system
is San Francisco, used on iOS and macOS (not Chrome however)BlinkMacSystemFont
is San Francisco, used on macOS ChromeSegoe UI
is used on Windows 10Roboto
is used on AndroidOxygen-Sans
is used on GNU+LinuxUbuntu
is used on Linux"Helvetica Neue"
and Helvetica
is used on macOS 10.10 and below (wrapped in quotes because it has a space)Arial
is a font widely supported by all operating systemssans-serif
is the fallback sans-serif font if none of the other fonts are supported✅ No caveats.
Creates a triangle shape with pure CSS.
<div class="triangle"></div>
+
.triangle {
+ width: 0;
+ height: 0;
+ border-top: 20px solid #333;
+ border-left: 20px solid transparent;
+ border-right: 20px solid transparent;
+}
+
View this link for a detailed explanation.
The color of the border is the color of the triangle. The side the triangle tip points corresponds to the opposite border-*
property. For example, a color on border-top
means the arrow points downward.
Experiment with the px
values to change the proportion of the triangle.
✅ No caveats.
If the text is longer than one line, it will be truncated and end with an ellipsis ...
.
<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
+
.truncate-text {
+ overflow: hidden;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+}
+
This text will be truncated if it exceeds 200px in width.
overflow: hidden
prevents the text from overflowing its dimensions (for a block, 100% width and auto height).white-space: nowrap
prevents the text from exceeding one line in height.text-overflow: ellipsis
makes it so that if the text exceeds its dimensions, it will end with an ellipsis.✅ No caveats.