diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..670154e35 --- /dev/null +++ b/LICENSE @@ -0,0 +1,116 @@ +CC0 1.0 Universal + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator and +subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for the +purpose of contributing to a commons of creative, cultural and scientific +works ("Commons") that the public can reliably and without fear of later +claims of infringement build upon, modify, incorporate in other works, reuse +and redistribute as freely as possible in any form whatsoever and for any +purposes, including without limitation commercial purposes. These owners may +contribute to the Commons to promote the ideal of a free culture and the +further production of creative, cultural and scientific works, or to gain +reputation or greater distribution for their Work in part through the use and +efforts of others. + +For these and/or other purposes and motivations, and without any expectation +of additional consideration or compensation, the person associating CC0 with a +Work (the "Affirmer"), to the extent that he or she is an owner of Copyright +and Related Rights in the Work, voluntarily elects to apply CC0 to the Work +and publicly distribute the Work under its terms, with knowledge of his or her +Copyright and Related Rights in the Work and the meaning and intended legal +effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not limited +to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, communicate, + and translate a Work; + + ii. moral rights retained by the original author(s) and/or performer(s); + + iii. publicity and privacy rights pertaining to a person's image or likeness + depicted in a Work; + + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + + v. rights protecting the extraction, dissemination, use and reuse of data in + a Work; + + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation thereof, + including any amended or successor version of such directive); and + + vii. other similar, equivalent or corresponding rights throughout the world + based on applicable law or treaty, and any national implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention of, +applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and +unconditionally waives, abandons, and surrenders all of Affirmer's Copyright +and Related Rights and associated claims and causes of action, whether now +known or unknown (including existing as well as future claims and causes of +action), in the Work (i) in all territories worldwide, (ii) for the maximum +duration provided by applicable law or treaty (including future time +extensions), (iii) in any current or future medium and for any number of +copies, and (iv) for any purpose whatsoever, including without limitation +commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes +the Waiver for the benefit of each member of the public at large and to the +detriment of Affirmer's heirs and successors, fully intending that such Waiver +shall not be subject to revocation, rescission, cancellation, termination, or +any other legal or equitable action to disrupt the quiet enjoyment of the Work +by the public as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason be +judged legally invalid or ineffective under applicable law, then the Waiver +shall be preserved to the maximum extent permitted taking into account +Affirmer's express Statement of Purpose. In addition, to the extent the Waiver +is so judged Affirmer hereby grants to each affected person a royalty-free, +non transferable, non sublicensable, non exclusive, irrevocable and +unconditional license to exercise Affirmer's Copyright and Related Rights in +the Work (i) in all territories worldwide, (ii) for the maximum duration +provided by applicable law or treaty (including future time extensions), (iii) +in any current or future medium and for any number of copies, and (iv) for any +purpose whatsoever, including without limitation commercial, advertising or +promotional purposes (the "License"). The License shall be deemed effective as +of the date CC0 was applied by Affirmer to the Work. Should any part of the +License for any reason be judged legally invalid or ineffective under +applicable law, such partial invalidity or ineffectiveness shall not +invalidate the remainder of the License, and in such case Affirmer hereby +affirms that he or she will not (i) exercise any of his or her remaining +Copyright and Related Rights in the Work or (ii) assert any associated claims +and causes of action with respect to the Work, in either case contrary to +Affirmer's express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + + b. Affirmer offers the Work as-is and makes no representations or warranties + of any kind concerning the Work, express, implied, statutory or otherwise, + including without limitation warranties of title, merchantability, fitness + for a particular purpose, non infringement, or the absence of latent or + other defects, accuracy, or the present or absence of errors, whether or not + discoverable, all to the greatest extent permissible under applicable law. + + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without limitation + any person's Copyright and Related Rights in the Work. Further, Affirmer + disclaims responsibility for obtaining any necessary consents, permissions + or other rights required for any use of the Work. + + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to this + CC0 or use of the Work. + +For more information, please see + diff --git a/README.md b/README.md index 85d026aa9..9f2090ac9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # 30 Seconds of CSS +![logo](https://i.imgur.com/2L1bMyy.png) + A curated collection of useful CSS snippets you can understand in 30 seconds or less. Inspired by [30 seconds of code](https://github.com/Chalarangelo/30-seconds-of-code). diff --git a/docs/4d4edbda57043aeeabeac31d2dd12d0d.js b/docs/4d4edbda57043aeeabeac31d2dd12d0d.js index 8ac739f4b..39f493b20 100644 --- a/docs/4d4edbda57043aeeabeac31d2dd12d0d.js +++ b/docs/4d4edbda57043aeeabeac31d2dd12d0d.js @@ -1,6 +1,6 @@ -require=function(r,e,n){function t(n,o){function i(r){return t(i.resolve(r))}function f(e){return r[n][1][e]||e}if(!e[n]){if(!r[n]){var c="function"==typeof require&&require;if(!o&&c)return c(n,!0);if(u)return u(n,!0);var l=new Error("Cannot find module '"+n+"'");throw l.code="MODULE_NOT_FOUND",l}i.resolve=f;var s=e[n]=new t.Module(n);r[n][0].call(s.exports,i,s,s.exports)}return e[n].exports}function o(r){this.id=r,this.bundle=t,this.exports={}}var u="function"==typeof require&&require;t.isParcelRequire=!0,t.Module=o,t.modules=r,t.cache=e,t.parent=u;for(var i=0;ie.length)return;if(!(w instanceof o)){p.lastIndex=0;var x=1;if(!(C=p.exec(w))&&f&&v!=t.length-1){if(p.lastIndex=k,!(C=p.exec(e)))break;for(var F=C.index+(h?C[1].length:0),S=C.index+C[0].length,A=v,j=k,P=t.length;A=(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+""},!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:/()[\s\S]*?(?=<\/style>)/i,lookbehind:!0,inside:a.languages.css,alias:"language-css",greedy:!0}}),a.languages.insertBefore("inside","attr-value",{"style-attr":{pattern:/\s*style=("|')(?:\\[\s\S]|(?!\1)[^\\])*\1/i,inside:{"attr-name":{pattern:/^\s*style/i,inside:a.languages.markup.tag.inside},punctuation:/^\s*=\s*['"]|['"]\s*$/,"attr-value":{pattern:/.+/i,inside:a.languages.css}},alias:"language-css"}},a.languages.markup.tag)),a.languages.clike={comment:[{pattern:/(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:{pattern:/(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,greedy:!0},"class-name":{pattern:/((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[\w.\\]+/i,lookbehind:!0,inside:{punctuation:/[.\\]/}},keyword:/\b(?:if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,boolean:/\b(?:true|false)\b/,function:/[a-z0-9_]+(?=\()/i,number:/\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)\b/i,operator:/--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,punctuation:/[{}[\];(),.:]/},a.languages.javascript=a.languages.extend("clike",{keyword:/\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/,number:/\b-?(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|\d*\.?\d+(?:[Ee][+-]?\d+)?|NaN|Infinity)\b/,function:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,operator:/-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/}),a.languages.insertBefore("javascript","keyword",{regex:{pattern:/(^|[^/])\/(?!\/)(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,lookbehind:!0,greedy:!0},"function-variable":{pattern:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,alias:"function"}}),a.languages.insertBefore("javascript","string",{"template-string":{pattern:/`(?:\\[\s\S]|[^\\`])*`/,greedy:!0,inside:{interpolation:{pattern:/\$\{[^}]+\}/,inside:{"interpolation-punctuation":{pattern:/^\$\{|\}$/,alias:"punctuation"},rest:a.languages.javascript}},string:/[\s\S]+/}}}),a.languages.markup&&a.languages.insertBefore("markup","tag",{script:{pattern:/()[\s\S]*?(?=<\/script>)/i,lookbehind:!0,inside:a.languages.javascript,alias:"language-javascript",greedy:!0}}),a.languages.js=a.languages.javascript,"undefined"!=typeof self&&self.Prism&&self.document&&document.querySelector&&(self.Prism.fileHighlight=function(){var e={js:"javascript",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell",sh:"bash",bat:"batch",h:"c",tex:"latex"};Array.prototype.slice.call(document.querySelectorAll("pre[data-src]")).forEach(function(t){for(var n,r=t.getAttribute("data-src"),i=t,s=/\blang(?:uage)?-(?!\*)(\w+)\b/i;i&&!s.test(i.className);)i=i.parentNode;if(i&&(n=(t.className.match(s)||[,""])[1]),!n){var l=(r.match(/\.(\w+)$/)||[,""])[1];n=e[l]||l}var o=document.createElement("code");o.className="language-"+n,t.textContent="",o.textContent="Loading…",t.appendChild(o);var u=new XMLHttpRequest;u.open("GET",r,!0),u.onreadystatechange=function(){4==u.readyState&&(u.status<400&&u.responseText?(o.textContent=u.responseText,a.highlightElement(o)):u.status>=400?o.textContent="✖ Error "+u.status+" while fetching file: "+u.statusText:o.textContent="✖ Error: File does not exist or is empty")},u.send(null)})},document.addEventListener("DOMContentLoaded",self.Prism.fileHighlight)); },{}],11:[function(require,module,exports) { @@ -16,4 +16,4 @@ var o=(0,eval)("this"),n="function"==typeof Symbol&&"symbol"==typeof Symbol.iter "use strict";var e=require("../deps/jump"),t=s(e),i=require("../deps/utils");function s(e){return e&&e.__esModule?e:{default:e}}var u=(0,i.select)(".back-to-top-button");window.addEventListener("scroll",function(){u.classList[(0,i.scrollY)()>500?"add":"remove"]("is-visible")}),u.onclick=function(){(0,t.default)(".header",{duration:750,easing:i.easeOutQuint})}; },{"../deps/jump":14,"../deps/utils":15}],7:[function(require,module,exports) { "use strict";require("normalize.css"),require("prismjs"),require("../css/deps/prism.css"),require("../css/index.scss"),require("./deps/polyfills");var e=require("./components/Menu"),s=u(e),r=require("./components/BackToTopButton"),i=u(r);function u(e){return e&&e.__esModule?e:{default:e}} -},{"normalize.css":16,"prismjs":17,"../css/deps/prism.css":16,"../css/index.scss":16,"./deps/polyfills":11,"./components/Menu":12,"./components/BackToTopButton":13}]},{},[7]) \ No newline at end of file +},{"normalize.css":17,"prismjs":16,"../css/deps/prism.css":17,"../css/index.scss":17,"./deps/polyfills":11,"./components/Menu":12,"./components/BackToTopButton":13}]},{},[7]) \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index e56801e8f..547e46d77 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,9 +1,65 @@ - 30 Seconds of CSS

30 Seconds of CSS

A curated collection of useful CSS snippets you can understand in 30 seconds or less.

Star

Clearfix

Ensures that an element self-clears its children.

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.

HTML

<div class="clearfix">
+
+
+
+  
+    
+    
+    30 Seconds of CSS
+    
+    
+    
+  
+  
+    
+    
+    
+
+
+ +

30 Seconds of CSS

+

+ A curated collection of useful CSS snippets you can understand in 30 seconds or less. +

+ Star +
+
+
+
+
+

Clearfix

+

Ensures that an element self-clears its children.

+
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.
+

HTML

<div class="clearfix">
   <div class="floated">float a</div>
   <div class="floated">float b</div>
   <div class="floated">float c</div>
 </div>
-

CSS

.clearfix::after {
+
+

CSS

.clearfix::after {
   content: "";
   display: block;
   clear: both;
@@ -11,33 +67,119 @@
 .floated {
   float: left;
 }
-

Demo

float a
float b
float c

Explanation

  1. .clearfix::after defines a pseudo-element.
  2. content: '' allows the pseudo-element to affect layout.
  3. 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.

Browser support

99+%

✅ No caveats.

Constant width to height ratio

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).

HTML

<div class="constant-width-to-height-ratio"></div>
-

CSS

.constant-width-to-height-ratio {
+
+

Demo

+
+
+
float a
+
float b
+
float c
+
+
+ +

Explanation

+
    +
  1. .clearfix::after defines a pseudo-element.
  2. +
  3. content: '' allows the pseudo-element to affect layout.
  4. +
  5. 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.
  6. +
+

Browser support

+
+
+ 99+% +
+
+

✅ No caveats.

+
+
+

Constant width to height ratio

+

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).

+

HTML

<div class="constant-width-to-height-ratio"></div>
+
+

CSS

.constant-width-to-height-ratio {
   background: #333;
   width: 50%;
   padding-top: 50%;
 }
-

Demo

Resize your browser window to see the proportion of the element remain the same.

Explanation

padding-top and padding-bottom can be used as an alternative to height such that the percentage value causes an element's height to become a percentage of its width, i.e. 50% means the height will be 50% of the element's width. This allows its proportion to remain constant.

Browser support

99+%

⚠️ padding-top pushes any content within the element to the bottom.

Custom text selection

Changes the styling of text selection.

HTML

<p class="custom-text-selection">Select some of this text.</p>
-

CSS

.custom-text-selection::selection {
+
+

Demo

+

Resize your browser window to see the proportion of the element remain the same.

+
+
+
+ +

Explanation

+

padding-top and padding-bottom can be used as an alternative to height such that the percentage value causes an element's height to become a percentage of its parent's width, i.e. 50% means + the height will be 50% of the parent element's width, which means it acts the same as width. This allows its proportion to remain constant.

+

Browser support

+
+
+ 99+% +
+
+

⚠️ padding-top pushes any content within the element to the bottom.

+
+
+

Custom text selection

+

Changes the styling of text selection.

+

HTML

<p class="custom-text-selection">Select some of this text.</p>
+
+

CSS

.custom-text-selection::selection {
   background: red;
   color: white;
 }
-

Demo

Select some of this text.

Explanation

::selection defines a pseudo selector on an element to style text within it when selected.

Browser support

84.6%

⚠️ Requires prefixes for full support and is not actually in any specification.

Donut spinner

Creates a donut spinner that can be used to indicate the loading of content.

HTML

<div class="donut"></div>
-

CSS

@keyframes donut-spin {
-    0% { transform: rotate(0deg); }
-    100% { transform: rotate(360deg);}
-}
-.donut {
-  display: inline-block;
-  border: 4px solid rgba(0, 0, 0, 0.25);
-  border-left-color: blue;
-  border-radius: 50%;
-  width: 20px;
-  height: 20px;
-  animation: donut-spin 1.2s linear infinite;
-}
-

Demo

Explanation

Use a semi-transparent border for the whole element, except one side that will serve as the loading indicator for the donut. Use animation to rotate the element.

Browser support

94.8%

⚠️ Requires prefixes for full support.

Easing variables

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.

HTML

<div class="easing-variables"></div>
-

CSS

:root {
+
+

Demo

+
+

Select some of this text.

+
+ +

Explanation

+

::selection defines a pseudo selector on an element to style text within it when selected.

+

Browser support

+
+
+ 84.6% +
+
+

⚠️ Requires prefixes for full support and is not actually +in any specification.

+ +
+
+

Easing variables

+

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.

+

HTML

<div class="easing-variables"></div>
+
+

CSS

:root {
   --ease-in-quad: cubic-bezier(0.55, 0.085, 0.68, 0.53);
   --ease-in-cubic: cubic-bezier(0.55, 0.055, 0.675, 0.19);
   --ease-in-quart: cubic-bezier(0.895, 0.03, 0.685, 0.22);
@@ -66,21 +208,157 @@
 .easing-variables:hover {
   transform: rotate(45deg);
 }
-

Demo

Hover

Explanation

The variables are defined globally within 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.

Browser support

87.2%

✅ No caveats.

Etched text

Creates an effect where text appears to be "etched" or engraved into the background.

HTML

<p class="etched-text">I appear etched into the background.</p>
-

CSS

.etched-text {
+
+

Demo

+
+
Hover
+
+ +

Explanation

+

The variables are defined globally within 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.

+

Browser support

+
+
+ 87.2% +
+
+

✅ No caveats.

+ +
+
+

Etched text

+

Creates an effect where text appears to be "etched" or engraved into the background.

+

HTML

<p class="etched-text">I appear etched into the background.</p>
+
+

CSS

.etched-text {
   text-shadow: 0 2px white;
   font-size: 1.5rem;
   font-weight: bold;
   color: #b8bec5;
 }
-

Demo

I appear etched into the background.

Explanation

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.

Browser support

97.9%

✅ No caveats.

Gradient text

Gives text a gradient color.

HTML

<p class="gradient-text">Gradient text</p>
-

CSS

.gradient-text {
+
+

Demo

+
+

I appear etched into the background.

+
+ +

Explanation

+

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.

+

Browser support

+
+
+ 97.9% +
+
+

✅ No caveats.

+ +
+
+

Gradient text

+

Gives text a gradient color.

+

HTML

<p class="gradient-text">Gradient text</p>
+
+

CSS

.gradient-text {
   background: -webkit-linear-gradient(pink, red);
   -webkit-text-fill-color: transparent;
   -webkit-background-clip: text;
 }
-

Demo

Gradient text

Explanation

  1. background: -webkit-linear-gradient(...) gives the text element a gradient background.
  2. webkit-text-fill-color: transparent fills the text with a transparent color.
  3. webkit-background-clip: text clips the background with the text, filling the text with the gradient background as the color.

Browser support

90.7%

⚠️ Uses non-standard properties.

Hairline border

Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.

HTML

<div class="hairline-border">text</div>
-

CSS

.hairline-border {
+
+

Demo

+
+

+ Gradient text +

+
+ +

Explanation

+
    +
  1. background: -webkit-linear-gradient(...) gives the text element a gradient background.
  2. +
  3. webkit-text-fill-color: transparent fills the text with a transparent color.
  4. +
  5. webkit-background-clip: text clips the background with the text, filling the text with the gradient background as the color.
  6. +
+

Browser support

+
+
+ 90.7% +
+
+

⚠️ Uses non-standard properties.

+ +
+
+

Hairline border

+

Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.

+

HTML

<div class="hairline-border">text</div>
+
+

CSS

.hairline-border {
   box-shadow: 0 0 0 1px;
 }
 @media (min-resolution: 2dppx) {
@@ -98,18 +376,108 @@
     box-shadow: 0 0 0 0.25px;
   }
 }
-

Demo

Text with a hairline border around it.

Explanation

  1. box-shadow, when only using spread, adds a psuedo-border which can use subpixels*.
  2. Use @media (min-resolution: ...) to check the device pixel ratio (1ddpx equals 96 DPI), setting the spread of the box-shadow equal to 1 / dppx.

Browser Support

95.0%

⚠️ 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.

Horizontal and vertical centering

Horizontally and vertically centers a child element within a parent element.

HTML

<div class="horizontal-and-vertical-centering">
+
+

Demo

+
+

Text with a hairline border around it.

+
+ +

Explanation

+
    +
  1. box-shadow, when only using spread, adds a psuedo-border which can use subpixels*.
  2. +
  3. Use @media (min-resolution: ...) to check the device pixel ratio (1ddpx equals 96 DPI), setting the spread of the box-shadow equal to 1 / dppx.
  4. +
+

Browser Support

+
+
+ 95.0% +
+
+

⚠️ 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.

+
+
+

Horizontal and vertical centering

+

Horizontally and vertically centers a child element within a parent element.

+

HTML

<div class="horizontal-and-vertical-centering">
   <div class="child"></div>
 </div>
-

CSS

.horizontal-and-vertical-centering {
+
+

CSS

.horizontal-and-vertical-centering {
   display: flex;
   justify-content: center;
   align-items: center;
 }
-

Demo

Centered content.

Explanation

  1. display: flex enables flexbox.
  2. justify-content: center centers the child horizontally.
  3. align-items: center centers the child vertically.

Browser support

97.8%

⚠️ Needs prefixes for full support.

Mouse cursor gradient tracking

A hover effect where the gradient follows the mouse cursor.

HTML

<button class="mouse-cursor-gradient-tracking">
+
+

Demo

+
+
+

Centered content.

+
+
+ +

Explanation

+
    +
  1. display: flex enables flexbox.
  2. +
  3. justify-content: center centers the child horizontally.
  4. +
  5. align-items: center centers the child vertically.
  6. +
+

Browser support

+
+
+ 97.8% +
+
+

⚠️ Needs prefixes for full support.

+ +
+
+

Mouse cursor gradient tracking

+

A hover effect where the gradient follows the mouse cursor.

+

HTML

<button class="mouse-cursor-gradient-tracking">
   <span>Hover me</span>
 </button>
-

CSS

.mouse-cursor-gradient-tracking {
+
+

CSS

.mouse-cursor-gradient-tracking {
   position: relative;
   background: #2379f7;
   padding: 0.5rem 1rem;
@@ -138,21 +506,96 @@
 .mouse-cursor-gradient-tracking:hover::before {
   --size: 200px;
 }
-

JavaScript

var btn = document.querySelector('.mouse-cursor-gradient-tracking')
+
+

JavaScript

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')
 }
-

Demo

Explanation

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
+
+

Demo

+
+ +
+ + +

Explanation

+

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
-

Browser support

87.2%

Requires JavaScript
⚠️ Requires JavaScript.

Overflow scroll gradient

Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.

HTML

<div class="overflow-scroll-gradient">
+
+

Browser support

+
+
+ 87.2% +
+
+

+
Requires JavaScript
+ ⚠️ Requires JavaScript. +

+ +
+
+

Overflow scroll gradient

+

Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.

+

HTML

<div class="overflow-scroll-gradient">
   <div class="overflow-scroll-gradient__scroller">
     Content to be scrolled
   </div>
 </div>
-

CSS

.overflow-scroll-gradient {
+
+

CSS

.overflow-scroll-gradient {
   position: relative;
 }
 .overflow-scroll-gradient::after {
@@ -162,6 +605,7 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
   width: 300px;
   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;
@@ -172,12 +616,75 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
   line-height: 1.2;
   text-align: center;
 }
-

Demo

Content to be scrolled

Explanation

  1. position: relative on the parent establishes a Cartesian positioning context for psuedo elements.
  2. ::after defines a pseudo element.
  3. background-image: linear-gradient(...) adds a linear gradient that fades from transparent to white (top to bottom).
  4. position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.
  5. width: 300px matches the size of the scrolling element (which is a child of the parent that has the pseudo element).
  6. height: 25px is the height of the fading gradient psuedo element, which should be kept relatively small.
  7. bottom: 0 positions the pseudo element at the bottom of the parent.

Browser support

94.8%

✅ No caveats.

Popout menu

Reveals an interactive popout menu on hover.

HTML

<div class="reference">
+
+

Demo

+
+
+
+ Content to be scrolled +
+
+
+ + +

Explanation

+
    +
  1. position: relative on the parent establishes a Cartesian positioning context for psuedo-elements.
  2. +
  3. ::after defines a pseudo element.
  4. +
  5. background-image: linear-gradient(...) adds a linear gradient that fades from transparent to white (top to bottom).
  6. +
  7. position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.
  8. +
  9. width: 300px matches the size of the scrolling element (which is a child of the parent that has the pseudo element).
  10. +
  11. height: 25px is the height of the fading gradient psuedo-element, which should be kept relatively small.
  12. +
  13. bottom: 0 positions the pseudo-element at the bottom of the parent.
  14. +
  15. pointer-events: none specifies that the psuedo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.
  16. +
+

Browser support

+
+
+ 94.8% +
+
+

✅ No caveats.

+ +
+
+

Popout menu

+

Reveals an interactive popout menu on hover.

+

HTML

<div class="reference">
   <div class="popout-menu">
     Popout menu
   </div>
 </div>
-

CSS

.reference {
+
+

CSS

.reference {
   position: relative;
 }
 .popout-menu {
@@ -188,25 +695,135 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
 .reference:hover > .popout-menu {
   visibility: visible;
 }
-

Demo

Popout menu

Explanation

  1. position: relative on the reference parent establishes a Cartesian positioning context for its child.
  2. position: absolute takes the popout menu out of the flow of the document and positions it in relation to the parent.
  3. left: 100% moves the the popout menu 100% of its parent's width from the left.
  4. visibility: hidden hides the popout menu initially and allows for transitions (unlike display: none).
  5. .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.

Browser support

99+%

✅ No caveats.

Pretty text underline

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.

HTML

<p class="pretty-text-underline">Pretty text underline without clipping descending letters.</p>
-

CSS

.pretty-text-underline {
+
+

Demo

+
+
+
+ Popout menu +
+
+
+ +

Explanation

+
    +
  1. position: relative on the reference parent establishes a Cartesian positioning context for its child.
  2. +
  3. position: absolute takes the popout menu out of the flow of the document and positions it in relation to the parent.
  4. +
  5. left: 100% moves the the popout menu 100% of its parent's width from the left.
  6. +
  7. visibility: hidden hides the popout menu initially and allows for transitions (unlike display: none).
  8. +
  9. .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.
  10. +
+

Browser support

+
+
+ 99+% +
+
+

✅ No caveats.

+
+
+

Pretty text underline

+

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.

+

HTML

<p class="pretty-text-underline">Pretty text underline without clipping descending letters.</p>
+
+

CSS

.pretty-text-underline {
   display: inline;
-  font-size: 1.25rem;
+  font-size: 18px;
   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-position: 0 18px;
   background-repeat: repeat-x;
   background-size: 1px 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;
 }
-

Demo

Pretty text underline without clipping descending letters.

Explanation

  1. 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.
  2. background-image: linear-gradient(...) creates a 90deg gradient with the current text color (currentColor).
  3. The background-* properties size the gradient as 1x1px at the bottom and repeats it along the x-axis.
  4. The ::selection pseudo selector ensures the text shadow does not interfere with text selection.

Browser support

94.8%

✅ No caveats.

Shape separator

Uses an SVG shape to separate two different blocks to create more a interesting visual appearance compared to standard horizontal separation.

HTML

<div class="shape-separator"></div>
-

CSS

.shape-separator {
+
+

Demo

+
+

Pretty text underline without clipping descending letters.

+
+ +

Explanation

+
    +
  1. 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.
  2. +
  3. background-image: linear-gradient(...) creates a 90deg gradient with the current text color (currentColor).
  4. +
  5. The background-* properties size the gradient as 1x1px at the bottom and repeats it along the x-axis.
  6. +
  7. The ::selection pseudo selector ensures the text shadow does not interfere with text selection. +
  8. +
+

Browser support

+
+
+ 94.8% +
+
+

⚠️ Firefox requires a vendor prefix for the selection pseudo-selector to work.

+ +
+
+

Shape separator

+

Uses an SVG shape to separate two different blocks to create more a interesting visual appearance compared to standard horizontal separation.

+

HTML

<div class="shape-separator"></div>
+
+

CSS

.shape-separator {
   position: relative;
   height: 48px;
 }
@@ -218,22 +835,211 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
   height: 24px;
   bottom: 0;
 }
-

Demo

Explanation

  1. position: relative on the element establishes a Cartesian positioning context for psuedo elements.
  2. ::after defines a pseudo element.
  3. 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.
  4. position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.
  5. width: 100% ensures the element stretches the entire width of its parent.
  6. height: 24px is the same height as the shape.
  7. bottom: 0 positions the pseudo element at the bottom of the parent.

Browser support

98.0%

✅ No caveats.

System font stack

Uses the native font of the operating system to get close to a native app feel.

HTML

<p class="system-font-stack">This text uses the system font.</p>
-

CSS

.system-font-stack {
+
+

Demo

+
+
+
+ +

Explanation

+
    +
  1. position: relative on the element establishes a Cartesian positioning context for psuedo elements.
  2. +
  3. ::after defines a pseudo element.
  4. +
  5. 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. +
  6. +
  7. position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.
  8. +
  9. width: 100% ensures the element stretches the entire width of its parent.
  10. +
  11. height: 24px is the same height as the shape.
  12. +
  13. bottom: 0 positions the pseudo element at the bottom of the parent.
  14. +
+

Browser support

+
+
+ 98.0% +
+
+

✅ No caveats.

+ +
+
+

System font stack

+

Uses the native font of the operating system to get close to a native app feel.

+

HTML

<p class="system-font-stack">This text uses the system font.</p>
+
+

CSS

.system-font-stack {
   font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
 }
-

Demo

This text uses the system font.

Explanation

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).

  1. -apple-system is San Francisco, used on iOS and macOS (not Chrome however)
  2. BlinkMacSystemFont is San Francisco, used on macOS Chrome
  3. Segoe UI is used on Windows 10
  4. Roboto is used on Android
  5. Oxygen-Sans is used on GNU+Linux
  6. Ubuntu is used on Linux
  7. "Helvetica Neue" and Helvetica is used on macOS 10.10 and below (wrapped in quotes because it has a space)
  8. Arial is a font widely supported by all operating systems
  9. sans-serif is the fallback sans-serif font if none of the other fonts are supported

Browser support

99+%

✅ No caveats.

Triangle

Creates a triangle shape with pure CSS.

HTML

<div class="triangle"></div>
-

CSS

.triangle {
+
+

Demo

+
+

This text uses the system font.

+
+ +

Explanation

+

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).

+
    +
  1. -apple-system is San Francisco, used on iOS and macOS (not Chrome however)
  2. +
  3. BlinkMacSystemFont is San Francisco, used on macOS Chrome
  4. +
  5. Segoe UI is used on Windows 10
  6. +
  7. Roboto is used on Android
  8. +
  9. Oxygen-Sans is used on GNU+Linux
  10. +
  11. Ubuntu is used on Linux
  12. +
  13. "Helvetica Neue" and Helvetica is used on macOS 10.10 and below (wrapped in quotes because it has a space)
  14. +
  15. Arial is a font widely supported by all operating systems
  16. +
  17. sans-serif is the fallback sans-serif font if none of the other fonts are supported
  18. +
+

Browser support

+
+
+ 99+% +
+
+

✅ No caveats.

+
+
+

Triangle

+

Creates a triangle shape with pure CSS.

+

HTML

<div class="triangle"></div>
+
+

CSS

.triangle {
   width: 0;
   height: 0;
   border-top: 20px solid #333;
   border-left: 20px solid transparent;
   border-right: 20px solid transparent;
 }
-

Demo

Explanation

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.

Browser support

99+%

✅ No caveats.

Truncate text

If the text is longer than one line, it will be truncated and end with an ellipsis ....

HTML

<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
-

CSS

.truncate-text {
+
+

Demo

+
+
+
+
+
+
+
+
+
+ +

Explanation

+

+ 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.

+

Browser support

+
+
+ 99+% +
+
+

✅ No caveats.

+
+
+

Truncate text

+

If the text is longer than one line, it will be truncated and end with an ellipsis .

+

HTML

<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
+
+

CSS

.truncate-text {
   overflow: hidden;
   white-space: nowrap;
   text-overflow: ellipsis;
 }
-

Demo

This text will be truncated if it exceeds 200px in width.

Explanation

  1. overflow: hidden prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
  2. white-space: nowrap prevents the text from exceeding one line in height.
  3. text-overflow: ellipsis makes it so that if the text exceeds its dimensions, it will end with an ellipsis.

Browser support

98.1%

✅ No caveats.

\ No newline at end of file +
+

Demo

+
+

+ This text will be truncated if it exceeds 200px in width. +

+
+ +

Explanation

+
    +
  1. overflow: hidden prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
  2. +
  3. white-space: nowrap prevents the text from exceeding one line in height.
  4. +
  5. text-overflow: ellipsis makes it so that if the text exceeds its dimensions, it will end with an ellipsis.
  6. +
+

Browser support

+
+
+ 98.1% +
+
+

⚠️ Only works for single line elements.

+ +
+
+
+
+
+ + diff --git a/index.html b/index.html index 24ce6079f..9a565ff72 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,5 @@ + + @@ -8,9 +10,9 @@ - - +
@@ -43,12 +45,10 @@

A curated collection of useful CSS snippets you can understand in 30 seconds or less.

- - - Star + Star
-
+

Clearfix

@@ -125,8 +125,8 @@ }

Explanation

-

padding-top and padding-bottom can be used as an alternative to height such that the percentage value causes an element's height to become a percentage of its width, i.e. 50% means the height - will be 50% of the element's width. This allows its proportion to remain constant.

+

padding-top and padding-bottom can be used as an alternative to height such that the percentage value causes an element's height to become a percentage of its parent's width, i.e. 50% means + the height will be 50% of the parent element's width, which means it acts the same as width. This allows its proportion to remain constant.

Browser support

@@ -666,6 +666,7 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop width: 300px; 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; @@ -696,6 +697,7 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop width: 300px; height: 25px; bottom: 0; + pointer-events: none; } .snippet-demo__overflow-scroll-gradient__scroller { overflow-y: scroll; @@ -712,13 +714,14 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop

Explanation

    -
  1. position: relative on the parent establishes a Cartesian positioning context for psuedo elements.
  2. +
  3. position: relative on the parent establishes a Cartesian positioning context for psuedo-elements.
  4. ::after defines a pseudo element.
  5. background-image: linear-gradient(...) adds a linear gradient that fades from transparent to white (top to bottom).
  6. position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.
  7. width: 300px matches the size of the scrolling element (which is a child of the parent that has the pseudo element).
  8. -
  9. height: 25px is the height of the fading gradient psuedo element, which should be kept relatively small.
  10. -
  11. bottom: 0 positions the pseudo element at the bottom of the parent.
  12. +
  13. height: 25px is the height of the fading gradient psuedo-element, which should be kept relatively small.
  14. +
  15. bottom: 0 positions the pseudo-element at the bottom of the parent.
  16. +
  17. pointer-events: none specifies that the psuedo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.

Browser support

@@ -809,16 +812,20 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop

CSS

.pretty-text-underline {
   display: inline;
-  font-size: 1.25rem;
+  font-size: 18px;
   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-position: 0 18px;
   background-repeat: repeat-x;
   background-size: 1px 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;
@@ -834,10 +841,14 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
                 font-size: 18px !important;
                 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-position: 0 18px;
                 background-repeat: repeat-x;
                 background-size: 1px 1px;
               }
+              .snippet-demo__pretty-text-underline::-moz-selection {
+                background-color: rgba(0, 150, 255, 0.3);
+                text-shadow: none;
+              }
               .snippet-demo__pretty-text-underline::selection {
                 background-color: rgba(0, 150, 255, 0.3);
                 text-shadow: none;
@@ -858,7 +869,7 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
                 94.8%
               
-

✅ No caveats.

+

⚠️ Firefox requires a vendor prefix for the selection pseudo-selector to work.

Truncate text

-

If the text is longer than one line, it will be truncated and end with an ellipsis ....

+

If the text is longer than one line, it will be truncated and end with an ellipsis .

HTML

<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
 

CSS

.truncate-text {
@@ -1080,7 +1091,7 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
                 98.1%
               
-

✅ No caveats.

+

⚠️ Only works for single line elements.

- \ No newline at end of file + diff --git a/scripts/build.js b/scripts/build.js index c1b9902ed..11cd3b39e 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -67,4 +67,10 @@ for (const snippetFile of fs.readdirSync(SNIPPETS_PATH)) { ) } -fs.writeFileSync('./index.html', pretty(document.documentElement.outerHTML, { ocd: true })) +// doctype declaration gets stripped, add it back in +const html = ` + +${pretty(document.documentElement.outerHTML, { ocd: true })} +` + +fs.writeFileSync('./index.html', html) diff --git a/snippets/constant-width-to-height-ratio.md b/snippets/constant-width-to-height-ratio.md index fb2eb53e9..ff7ef0122 100644 --- a/snippets/constant-width-to-height-ratio.md +++ b/snippets/constant-width-to-height-ratio.md @@ -38,7 +38,7 @@ Resize your browser window to see the proportion of the element remain the same. #### Explanation `padding-top` and `padding-bottom` can be used as an alternative to `height` such that the percentage value -causes an element's height to become a percentage of its width, i.e. `50%` means the height will be 50% of the element's width. This allows its proportion to remain constant. +causes an element's height to become a percentage of its parent's width, i.e. `50%` means the height will be 50% of the parent element's width, which means it acts the same as `width`. This allows its proportion to remain constant. #### Browser support diff --git a/snippets/overflow-scroll-gradient.md b/snippets/overflow-scroll-gradient.md index de850bd81..0c4c3e644 100644 --- a/snippets/overflow-scroll-gradient.md +++ b/snippets/overflow-scroll-gradient.md @@ -25,6 +25,7 @@ Adds a fading gradient to an overflowing element to better indicate there is mor width: 300px; 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; @@ -58,6 +59,7 @@ Adds a fading gradient to an overflowing element to better indicate there is mor width: 300px; height: 25px; bottom: 0; + pointer-events: none; } .snippet-demo__overflow-scroll-gradient__scroller { overflow-y: scroll; @@ -76,15 +78,16 @@ document.querySelector('.snippet-demo__overflow-scroll-gradient__scroller').inne #### Explanation -1. `position: relative` on the parent establishes a Cartesian positioning context for psuedo elements. +1. `position: relative` on the parent establishes a Cartesian positioning context for psuedo-elements. 2. `::after` defines a pseudo element. 3. `background-image: linear-gradient(...)` adds a linear gradient that fades from transparent to white (top to bottom). 4. `position: absolute` takes the pseudo element out of the flow of the document and positions it in relation to the parent. 5. `width: 300px` matches the size of the scrolling element (which is a child of the parent that has the pseudo element). -6. `height: 25px` is the height of the fading gradient psuedo element, which should be kept relatively small. -7. `bottom: 0` positions the pseudo element at the bottom of the parent. +6. `height: 25px` is the height of the fading gradient psuedo-element, which should be kept relatively small. +7. `bottom: 0` positions the pseudo-element at the bottom of the parent. +8. `pointer-events: none` specifies that the psuedo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive. #### Browser support diff --git a/snippets/pretty-text-underline.md b/snippets/pretty-text-underline.md index 3f3012ce5..171fe9d80 100644 --- a/snippets/pretty-text-underline.md +++ b/snippets/pretty-text-underline.md @@ -14,16 +14,20 @@ Natively implemented as `text-decoration-skip-ink: auto` but it has less control ```css .pretty-text-underline { display: inline; - font-size: 1.25rem; + font-size: 18px; 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-position: 0 18px; background-repeat: repeat-x; background-size: 1px 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; @@ -45,11 +49,16 @@ Natively implemented as `text-decoration-skip-ink: auto` but it has less control -1px -1px 0 #f5f6f9, 1px -1px 0 #f5f6f9; background-image: linear-gradient(90deg, currentColor 100%, transparent 100%); - background-position: 0 1.04em; + background-position: 0 18px; background-repeat: repeat-x; background-size: 1px 1px; } +.snippet-demo__pretty-text-underline::-moz-selection { + background-color: rgba(0, 150, 255, 0.3); + text-shadow: none; +} + .snippet-demo__pretty-text-underline::selection { background-color: rgba(0, 150, 255, 0.3); text-shadow: none; @@ -69,7 +78,7 @@ Natively implemented as `text-decoration-skip-ink: auto` but it has less control #### Browser support -✅ No caveats. +⚠️ Firefox requires a vendor prefix for the selection pseudo-selector to work. * https://caniuse.com/#feat=css-textshadow * https://caniuse.com/#feat=css-gradients diff --git a/snippets/truncate-text.md b/snippets/truncate-text.md index 653432f7a..ce2fe0779 100644 --- a/snippets/truncate-text.md +++ b/snippets/truncate-text.md @@ -1,6 +1,6 @@ ### Truncate text -If the text is longer than one line, it will be truncated and end with an ellipsis `...`. +If the text is longer than one line, it will be truncated and end with an ellipsis `…`. #### HTML @@ -46,6 +46,6 @@ If the text is longer than one line, it will be truncated and end with an ellips #### Browser support -✅ No caveats. +⚠️ Only works for single line elements. * https://caniuse.com/#feat=text-overflow