diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..0a3613911 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: node_js +node_js: + - node +install: + - npm install +script: + - npm run build +after_script: + - test $TRAVIS_EVENT_TYPE = cron + && echo -e "\e[95mDeploying to Repository" + && chmod +x .travis/push.sh + && ./.travis/push.sh + - chmod +x .travis/forcepush.sh && ./.travis/forcepush.sh +cache: + directories: + - node_modules \ No newline at end of file diff --git a/.travis/forcepush.sh b/.travis/forcepush.sh new file mode 100644 index 000000000..54f15f5a5 --- /dev/null +++ b/.travis/forcepush.sh @@ -0,0 +1,3 @@ +if [[ $TRAVIS_COMMIT_MESSAGE == *"--force-build"* ]]; + then echo -e "\e[95mFORCE-DEPLOY: Deploying to Repository" && chmod +x .travis/push.sh && ./.travis/push.sh; +fi diff --git a/.travis/push.sh b/.travis/push.sh new file mode 100644 index 000000000..a6c1eb6b1 --- /dev/null +++ b/.travis/push.sh @@ -0,0 +1,33 @@ +#!/bin/bash +setup_git() { + git config --global user.email "30secondsofcode@gmail.com" + git config --global user.name "30secondsofcode" +} + +commit_website_files() { + if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then + if [ $TRAVIS_BRANCH == "master" ]; then + echo "Committing to master branch..." + git checkout master + git add * + if [ $TRAVIS_EVENT_TYPE == "cron" ]; then + git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [cron]" + else + git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [FORCED]" + fi + fi + fi +} + +upload_files() { + if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then + if [ $TRAVIS_BRANCH == "master" ]; then + echo "Pushing to master branch..." + git push --force --quiet "https://${GH_TOKEN}@github.com/30-seconds/30-seconds-of-css.git" master > /dev/null 2>&1 + fi + fi +} + +setup_git +commit_website_files +upload_files diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 4831c6b97..879096ccc 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting +- The use of sexualized language or imagery and unwelcome sexual attention or advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b96a810ba..74eda05ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,22 +11,11 @@ Brief description #### 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:/(
Note: 1rem is usually 16px.
@keyframes defines an animation that has two states, where the element changes opacity and is translated up on the 2D plane using transform: translateY().
.bouncing-loader is the parent container of the bouncing circles and uses display: flex and justify-content: center to position them in the center.
.bouncing-loader > div, targets the three child divs of the parent to be styled. The divs are given a width and height of 1rem, using border-radius: 50% to turn them from squares to circles.
margin: 3rem 0.2rem specifies that each circle has a top/bottom margin of 3rem and left/right margin of 0.2rem so that they do not directly touch each other, giving them some breathing room.
animation is a shorthand property for the various animation properties: animation-name, animation-duration, animation-iteration-count, animation-direction are used.
nth-child(n) targets the element which is the nth child of its parent.
animation-delay is used on the second and third div respectively, so that each element does not start the animation at the same time.
✅ No caveats.
Resets the box-model so that widths and heights are not affected by their borders or padding.
html {
+ Note: 1rem is usually 16px.
@keyframes defines an animation that has two states, where the element changes opacity and is translated up on the 2D plane using transform: translateY().
.bouncing-loader is the parent container of the bouncing circles and uses display: flex and justify-content: center to position them in the center.
.bouncing-loader > div, targets the three child divs of the parent to be styled. The divs are given a width and height of 1rem, using border-radius: 50% to turn them from squares to circles.
margin: 3rem 0.2rem specifies that each circle has a top/bottom margin of 3rem and left/right margin of 0.2rem so that they do not directly touch each other, giving them some breathing room.
animation is a shorthand property for the various animation properties: animation-name, animation-duration, animation-iteration-count, animation-direction are used.
nth-child(n) targets the element which is the nth child of its parent.
animation-delay is used on the second and third div respectively, so that each element does not start the animation at the same time.
✅ No caveats.
Resets the box-model so that widths and heights are not affected by their borders or padding.
<div class="box">border-box</div>
+<div class="box content-box">content-box</div>
+ html {
box-sizing: border-box;
}
*,
@@ -39,14 +41,26 @@
*::after {
box-sizing: inherit;
}
- box-sizing: border-box makes the addition of padding or borders not affect an element's width or height.box-sizing: inherit makes an element respect its parent's box-sizing rule.✅ No caveats.
Creates a circle shape with pure CSS.
<div class="circle"></div>
+.box {
+ display: inline-block;
+ width: 150px;
+ height: 150px;
+ padding: 10px;
+ background: tomato;
+ color: white;
+ border: 10px solid red;
+}
+.content-box {
+ box-sizing: content-box;
+}
+ box-sizing: border-box makes the addition of padding or borders not affect an element's width or height.box-sizing: inherit makes an element respect its parent's box-sizing rule.✅ No caveats.
Creates a circle shape with pure CSS.
<div class="circle"></div>
.circle {
border-radius: 50%;
width: 2rem;
height: 2rem;
background: #333;
}
- border-radius: 50% curves the borders of an element to create a circle.
Since a circle has the same radius at any given point, the width and height must be the same. Differing values will create an ellipse.
✅ No caveats.
Ensures that an element self-clears its children.
<div class="clearfix">
+ border-radius: 50% curves the borders of an element to create a circle.
Since a circle has the same radius at any given point, the width and height must be the same. Differing values will create an ellipse.
✅ No caveats.
Ensures that an element self-clears its children.
<div class="clearfix">
<div class="floated">float a</div>
<div class="floated">float b</div>
<div class="floated">float c</div>
@@ -59,14 +73,13 @@
.floated {
float: left;
}
- .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.⚠️ 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).
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).
<div class="constant-width-to-height-ratio"></div>
+ .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.⚠️ 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).
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).
<div class="constant-width-to-height-ratio"></div>
.constant-width-to-height-ratio {
background: #333;
width: 50%;
}
.constant-width-to-height-ratio::before {
content: '';
- display: block;
padding-top: 100%;
float: left;
}
@@ -75,25 +88,50 @@
display: block;
clear: both;
}
- Resize your browser window to see the proportion of the element remain the same.
padding-top on the ::before pseudo-element causes the height of the element to equal a percentage of its width. 100% therefore means the element's height will always be 100% of the width, creating a responsive square.
This method also allows content to be placed inside the element normally.
✅ No caveats.
Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.
<div class="custom-scrollbar">
- <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit?</p>
+ padding-top on the ::before pseudo-element causes the height of the element to equal a percentage of its width. 100% therefore means the element's height will always be 100% of the width, creating a responsive square.
This method also allows content to be placed inside the element normally.
✅ No caveats.
Counters are, in essence, variables maintained by CSS whose values may be incremented by CSS rules to track how many times they're used.
<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>
+ ul {
+ counter-reset: counter;
+}
+li::before {
+ counter-increment: counter;
+ content: counters(counter, '.') ' ';
+}
+ You can create a ordered list using any type of HTML.
counter-reset 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.
counter-increment Used in element that will be countable. Once counter-reset initialized, a counter's value can be increased or decreased.
counter(name, style) Displays the value of a section counter. Generally used in a content property. This function can receive two parameters, the first as the name of the counter and the second one can be decimal or upper-roman (decimal by default).
counters(counter, string, style) Displays the value of a section counter. Generally used in a content 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 decimal or upper-roman (decimal by default).
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 counters() function, separating text can be inserted between different levels of nested counters.
✅ No caveats.
Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.
<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>
- /* Document scrollbar */
-::-webkit-scrollbar {
+ .custom-scrollbar {
+ height: 70px;
+ overflow-y: scroll;
+}
+/* To style the document scrollbar, remove `.custom-scrollbar` */
+.custom-scrollbar::-webkit-scrollbar {
width: 8px;
}
-::-webkit-scrollbar-track {
+.custom-scrollbar::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
}
-::-webkit-scrollbar-thumb {
+.custom-scrollbar::-webkit-scrollbar-thumb {
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
-/* Scrollable element */
-.some-element::webkit-scrollbar {
-}
- ::-webkit-scrollbar targets the whole scrollbar element.::-webkit-scrollbar-track targets only the scrollbar track.::-webkit-scrollbar-thumb targets the scrollbar thumb.There are many other pseudo-elements that you can use to style scrollbars. For more info, visit the WebKit Blog
⚠️ Scrollbar styling doesn't appear to be on any standards track.
Changes the styling of text selection.
<p class="custom-text-selection">Select some of this text.</p>
+ ::-webkit-scrollbar targets the whole scrollbar element.::-webkit-scrollbar-track targets only the scrollbar track.::-webkit-scrollbar-thumb targets the scrollbar thumb.There are many other pseudo-elements that you can use to style scrollbars. For more info, visit the WebKit Blog.
⚠️ Scrollbar styling doesn't appear to be on any standards track.
Changes the styling of text selection.
<p class="custom-text-selection">Select some of this text.</p>
::selection {
background: aquamarine;
color: black;
@@ -102,25 +140,46 @@
background: deeppink;
color: white;
}
- Select some of this text.
::selection 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.
⚠️ Requires prefixes for full support and is not actually in any specification.
CSS variables that contain specific values to be reused throughout a document.
<p class="custom-variables">CSS is awesome!</p>
+ Select some of this text.
::selection 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.
⚠️ Requires prefixes for full support and is not actually in any specification.
CSS variables that contain specific values to be reused throughout a document.
<p class="custom-variables">CSS is awesome!</p>
: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;
-}
-.custom-variables {
color: var(--some-color);
font-size: var(--some-size);
font-style: var(--some-keyword);
text-shadow: var(--some-complex-value);
}
- CSS is awesome!
The variables are defined globally within the :root 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.
Declare a variable with --variable-name:.
Reuse variables throughout the document using the var(--variable-name) function.
✅ No caveats.
Makes the content unselectable.
<p>You can select me.</p>
+ CSS is awesome!
The variables are defined globally within the :root 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.
Declare a variable with --variable-name:.
Reuse variables throughout the document using the var(--variable-name) function.
✅ No caveats.
Makes the content unselectable.
<p>You can select me.</p>
<p class="unselectable">You can't select me!</p>
.unselectable {
user-select: none;
}
- You can select me.
You can't select me!
user-select: none specifies that the text cannot be selected.
⚠️ Requires prefixes for full support. ⚠️ This is not a secure method to prevent users from copying content.
Creates a donut spinner that can be used to indicate the loading of content.
<div class="donut"></div>
+ You can select me.
You can't select me!
user-select: none specifies that the text cannot be selected.
⚠️ Requires prefixes for full support. ⚠️ This is not a secure method to prevent users from copying content.
Vertically and horizontally centers a child element within its parent element using display: table (as an alternative to flexbox).
<div class="container">
+ <div class="center">
+ <span>Centered content</span>
+ </div>
+</div>
+ .container {
+ border: 1px solid #333;
+ height: 250px;
+ width: 250px;
+}
+.center {
+ display: table;
+ height: 100%;
+ width: 100%;
+}
+.center > span {
+ display: table-cell;
+ text-align: center;
+ vertical-align: middle;
+}
+ display: table on '.center' allows the element to behave like a <table> HTML element.display: table-cell on '.center > span' allows the element to behave like an HTML element.text-align: center on '.center > span' centers the child element horizontally.vertical-align: middle on '.center > span' centers the child element vertically.The outer parent ('.container' in this case) must have a fixed height and width.
✅ No caveats.
Creates a donut spinner that can be used to indicate the loading of content.
<div class="donut"></div>
@keyframes donut-spin {
0% {
transform: rotate(0deg);
@@ -138,18 +197,13 @@
height: 30px;
animation: donut-spin 1.2s linear infinite;
}
- 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.
⚠️ Requires prefixes for full support.
Creates a shadow similar to box-shadow but based on the colors of the element itself.
<div class="dynamic-shadow-parent">
- <div class="dynamic-shadow"></div>
-</div>
- .dynamic-shadow-parent {
- position: relative;
- z-index: 1;
-}
-.dynamic-shadow {
+ 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.
⚠️ Requires prefixes for full support.
Creates a shadow similar to box-shadow but based on the colors of the element itself.
<div class="dynamic-shadow"></div>
+ .dynamic-shadow {
position: relative;
width: 10rem;
height: 10rem;
background: linear-gradient(75deg, #6d78ff, #00ffb8);
+ z-index: 1;
}
.dynamic-shadow::after {
content: '';
@@ -162,8 +216,11 @@
opacity: 0.7;
z-index: -1;
}
- The snippet requires a somewhat complex case of stacking contexts to get right, such that the pseudo-element will be positioned underneath the element itself while still being visible.
position: relative on the parent establishes a Cartesian positioning context for child elements.z-index: 1 establishes a new stacking context.position: relative on the child establishes a positioning context for pseudo-elements.::after defines a pseudo-element.position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.width: 100% and height: 100% sizes the pseudo-element to fill its parent's dimensions, making it equal in size.background: inherit causes the pseudo-element to inherit the linear gradient specified on the element.top: 0.5rem offsets the pseudo-element down slightly from its parent.filter: blur(0.4rem) will blur the pseudo-element to create the appearance of a shadow underneath.opacity: 0.7 makes the pseudo-element partially transparent.z-index: -1 positions the pseudo-element behind the parent.⚠️ 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>
+ position: relative on the element establishes a Cartesian positioning context for psuedo-elements.z-index: 1 establishes a new stacking context.::after defines a pseudo-element.position: absolute takes the pseudo element out of the flow of the document and positions it in relation to the parent.width: 100% and height: 100% sizes the pseudo-element to fill its parent's dimensions, making it equal in size.background: inherit causes the pseudo-element to inherit the linear gradient specified on the element.top: 0.5rem offsets the pseudo-element down slightly from its parent.filter: blur(0.4rem) will blur the pseudo-element to create the appearance of a shadow underneath.opacity: 0.7 makes the pseudo-element partially transparent.z-index: -1 positions the pseudo-element behind the parent but in front of the background.⚠️ 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">Hover</div>
: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);
@@ -182,24 +239,27 @@
--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;
+ 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);
}
- 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.
✅ 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>
+ 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.
✅ 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.
Evenly distributes child elements within a parent element.
<div class="evenly-distributed-children">
+ 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.
Evenly distributes child elements within a parent element.
<div class="evenly-distributed-children">
<p>Item1</p>
<p>Item2</p>
<p>Item3</p>
@@ -208,65 +268,31 @@
display: flex;
justify-content: space-between;
}
- Item1
Item2
Item3
display: flex enables flexbox.justify-content: space-between evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.Alternatively, use justify-content: space-around to distribute the children with space around them, rather than between them.
⚠️ Needs prefixes for full support.
Horizontally and vertically centers a child element within a parent element using flexbox.
<div class="flexbox-centering">
+ Item1
Item2
Item3
display: flex enables flexbox.justify-content: space-between evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.Alternatively, use justify-content: space-around to distribute the children with space around them, rather than between them.
⚠️ Needs prefixes for full support.
Horizontally and vertically centers a child element within a parent element using flexbox.
<div class="flexbox-centering">
<div class="child">Centered content.</div>
</div>
.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
+ height: 100px;
}
- 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.
Gives text a gradient color.
<p class="gradient-text">Gradient text</p>
+ display: flex enables flexbox.justify-content: center centers the child horizontally.align-items: center centers the child vertically.⚠️ Needs prefixes for full support.
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.
Horizontally and vertically centers a child element within a parent element using grid.
<div class="grid-centering">
+ 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.
Horizontally and vertically centers a child element within a parent element using grid.
<div class="grid-centering">
<div class="child">Centered content.</div>
</div>
.grid-centering {
display: grid;
justify-content: center;
align-items: center;
+ height: 100px;
}
- Centered content.
display: grid enables grid.justify-content: center centers the child horizontally.align-items: center centers the child vertically.✅ No caveats.
Basic website layout using grid.
<div class="grid-layout">
- <div class="header">Header</div>
- <div class="sidebar">Sidebar</div>
- <div class="content">
- Content
- <br>
- Lorem ipsum dolor sit amet, consectetur adipisicing elit.
- </div>
- <div class="footer">Footer</div>
-</div>
- .grid-layout {
- display: grid;
- grid-gap: 10px;
- grid-template-columns: repeat(3, 1fr);
- grid-template-areas:
- 'sidebar header header'
- 'sidebar content content'
- 'sidebar footer footer';
- color: white;
-}
-.grid-layout > div {
- background: #333;
- padding: 10px;
-}
-.sidebar {
- grid-area: sidebar;
-}
-.content {
- grid-area: content;
-}
-.header {
- grid-area: header;
-}
-.footer {
- grid-area: footer;
-}
- display: grid enables grid.grid-gap: 10px defines spacing between the elements.grid-template-columns: repeat(3, 1fr) defines 3 columns of the same size.grid-template-areas defines the names of grid areas.grid-area: sidebar makes the element use the area with the name sidebar.✅ No caveats.
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>
+ display: grid enables grid.justify-content: center centers the child horizontally.align-items: center centers the child vertically.✅ No caveats.
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;
}
@@ -285,7 +311,22 @@
box-shadow: 0 0 0 0.25px;
}
}
- Text with a hairline border around it.
box-shadow, when only using spread, adds a pseudo-border which can use subpixels*.@media (min-resolution: ...) to check the device pixel ratio (1dppx 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.
Creates an animated underline effect when the text is hovered over.
Credit: https://flatuicolors.com/
<p class="hover-underline-animation">Hover this text to see the effect!</p>
+ box-shadow, when only using spread, adds a pseudo-border which can use subpixels*.@media (min-resolution: ...) to check the device pixel ratio (1dppx 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.
Transitions an element's height from 0 to auto when its height is unknown.
<div class="trigger">
+ Hover me to see a height transition.
+ <div class="el">content</div>
+</div>
+ .el {
+ transition: max-height 0.5s;
+ overflow: hidden;
+ max-height: 0;
+}
+.trigger:hover > .el {
+ max-height: var(--max-height);
+}
+ var el = document.querySelector('.el')
+var height = el.scrollHeight
+el.style.setProperty('--max-height', height + 'px')
+ transition: max-height: 0.5s cubic-bezier(...) specifies that changes to max-height should be transitioned over 0.5 seconds, using an ease-out-quint timing function.overflow: hidden prevents the contents of the hidden element from overflowing its container.max-height: 0 specifies that the element has no height initially..target:hover > .el specifies that when the parent is hovered over, target a child .el within it and use the --max-height variable which was defined by JavaScript.el.scrollHeight is the height of the element including overflow, which will change dynamically based on the content of the element.el.style.setProperty(...) sets the --max-height CSS variable which is used to specify the max-height of the element the target is hovered over, allowing it to transition smoothly from 0 to auto.Creates an animated underline effect when the text is hovered over.
Credit: https://flatuicolors.com/
<p class="hover-underline-animation">Hover this text to see the effect!</p>
.hover-underline-animation {
display: inline-block;
position: relative;
@@ -307,7 +348,26 @@
transform: scaleX(1);
transform-origin: bottom left;
}
- Hover this text to see the effect!
display: inline-block makes the block p an inline-block to prevent the underline from spanning the entire parent width rather than just the content (text).position: relative on the element establishes a Cartesian positioning context for pseudo-elements.::after defines a pseudo-element.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 pseudo-element spans the entire width of the text block.transform: scaleX(0) initially scales the pseudo element to 0 so it has no width and is not visible.bottom: 0 and left: 0 position it to the bottom left of the block.transition: transform 0.25s ease-out means changes to transform will be transitioned over 0.25 seconds with an ease-out timing function.transform-origin: bottom right means the transform anchor point is positioned at the bottom right of the block.:hover::after then uses scaleX(1) to transition the width to 100%, then changes the transform-origin to bottom left so that the anchor point is reversed, allowing it transition out in the other direction when hovered off.✅ No caveats.
A hover effect where the gradient follows the mouse cursor.
Credit: Tobias Reich
<button class="mouse-cursor-gradient-tracking">
+ Hover this text to see the effect!
display: inline-block makes the block p an inline-block to prevent the underline from spanning the entire parent width rather than just the content (text).position: relative on the element establishes a Cartesian positioning context for pseudo-elements.::after defines a pseudo-element.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 pseudo-element spans the entire width of the text block.transform: scaleX(0) initially scales the pseudo element to 0 so it has no width and is not visible.bottom: 0 and left: 0 position it to the bottom left of the block.transition: transform 0.25s ease-out means changes to transform will be transitioned over 0.25 seconds with an ease-out timing function.transform-origin: bottom right means the transform anchor point is positioned at the bottom right of the block.:hover::after then uses scaleX(1) to transition the width to 100%, then changes the transform-origin to bottom left so that the anchor point is reversed, allowing it transition out in the other direction when hovered off.✅ No caveats.
Take advantage of available viewport space by giving the last element the remaining available space in current viewport, even when resizing the window.
<div class="container">
+ <div>Div 1</div>
+ <div>Div 2</div>
+ <div>Div 3</div>
+</div>
+ html,
+body {
+ height: 100%;
+ margin: 0;
+}
+.container {
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+}
+.container > div:last-child {
+ background-color: tomato;
+ flex: 1;
+}
+ height: 100% set the height of container as viewport height.display: flex enables flexbox.flex-direction: column set the direction of flex items' order from top to down.flex-grow: 1 the flexbox will apply remaining available space of container to last child element.The parent must have a viewport height. flex-grow: 1 could be applied to the first or second element, which will have all available space.
⚠️ Needs prefixes for full support.
A hover effect where the gradient follows the mouse cursor.
Credit: Tobias Reich
<button class="mouse-cursor-gradient-tracking">
<span>Hover me</span>
</button>
.mouse-cursor-gradient-tracking {
@@ -341,16 +401,55 @@
}
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
btn.onmousemove = function(e) {
- var x = e.pageX - btn.offsetLeft
- var y = e.pageY - btn.offsetTop
+ 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')
}
- 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">
+ TODO
The :not psuedo selector is useful for styling a group of elements, while leaving the last (or specified) element unstyled.
<ul class="css-not-selector-shortcut">
+ <li>One</li>
+ <li>Two</li>
+ <li>Three</li>
+ <li>Four</li>
+</ul>
+ .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;
+}
+ li:not(:last-child) specifies that the styles should apply to all li elements except the :last-child.
✅ No caveats.
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 ( ADA) development when more context is needed for visually-impaired users. As an alternative to display: none which is not readable by screen readers or visibility: hidden which takes up physical space in the DOM.
<a class="button" href="http://pantswebsite.com">
+ Learn More
+ <span class="offscreen"> about pants</span>
+</a>
+ .offscreen {
+ border: 0;
+ clip: rect(0 0 0 0);
+ height: 1px;
+ margin: -1px;
+ overflow: hidden;
+ padding: 0;
+ position: absolute;
+ width: 1px;
+}
+ clip to indicate that no part of the element should be shown.margin: -1px.✅ No caveats.
(Although clip technically has been depreciated, the newer clip-path currently has very limited browser support.)
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
+ 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>
.overflow-scroll-gradient {
@@ -373,36 +472,41 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
background: white;
width: 240px;
height: 200px;
- padding: 15px 0;
+ padding: 15px;
line-height: 1.2;
- text-align: center;
}
- position: relative on the parent establishes a Cartesian positioning context for pseudo-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: 240px 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 pseudo-element, which should be kept relatively small.bottom: 0 positions the pseudo-element at the bottom of the parent.pointer-events: none specifies that the pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.✅ No caveats.
Reveals an interactive popout menu on hover.
<div class="reference">
+ position: relative on the parent establishes a Cartesian positioning context for pseudo-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: 240px 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 pseudo-element, which should be kept relatively small.bottom: 0 positions the pseudo-element at the bottom of the parent.pointer-events: none specifies that the pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.✅ No caveats.
Reveals an interactive popout menu on hover and focus.
<div class="reference" tabindex="0">
<div class="popout-menu">
Popout menu
</div>
</div>
.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 > .popout-menu {
+.reference:hover > .popout-menu,
+.reference:focus > .popout-menu,
+.reference:focus-within > .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>
+ 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..reference:focus > .popout-menu means that when .reference is focused, the popout would be shown..reference:focus-within > .popout-menu ensures that the popout is shown when the focus is within the reference.✅ 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 {
- font-family: Arial, sans-serif;
display: inline;
- font-size: 18px;
- text-shadow: 1px 1px 0 #f5f6f9, -1px 1px 0 #f5f6f9, -1px -1px 0 #f5f6f9, 1px -1px 0 #f5f6f9;
+ 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: 0 0.98em;
- background-repeat: repeat-x;
- background-size: 1px 1px;
+ background-position: bottom;
+ background-repeat: no-repeat;
+ background-size: 100% 1px;
}
.pretty-text-underline::-moz-selection {
background-color: rgba(0, 150, 255, 0.3);
@@ -412,14 +516,14 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
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. ⚠️ The distance of the underline from the text depends on the internal metrics of a font, so you must ensure everyone sees the same font (i.e. no system fonts which will change based on the OS).
Resets all styles to default values with one property. This will not affect direction and unicode-bidi properties.
<div class="reset-all-styles">
- <h4>Title</h4>
+ Pretty text underline without clipping descending letters.
text-shadow 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 px size. Additional values can create an even thicker shadow, and subpixel values can also be used.background-image: linear-gradient(...) creates a 90deg gradient using the text color (currentColor).background-* 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.::selection pseudo selector rule ensures the text shadow does not interfere with text selection. ✅ No caveats.
Resets all styles to default values with one property. This will not affect direction and unicode-bidi properties.
<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>
.reset-all-styles {
all: initial;
}
- 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?
The all property allows you to reset all styles (inherited or not) to default values.
⚠️ MS Edge status is under consideration.
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>
+ 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?
The all property allows you to reset all styles (inherited or not) to default values.
⚠️ MS Edge status is under consideration.
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;
@@ -427,13 +531,13 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
}
.shape-separator::after {
content: '';
- background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsMTItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+);
+ 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: 24px;
+ height: 12px;
bottom: 0;
}
- position: relative on the element establishes a Cartesian positioning context for pseudo 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 pseudo 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.
Fades out the siblings of a hovered item.
<div class="sibling-fade">
+ position: relative on the element establishes a Cartesian positioning context for pseudo elements.::after defines a pseudo element.background-image: url(...) 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 the URL-encoder for SVG.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: 12px is the same height as the shape.bottom: 0 positions the pseudo element at the bottom of the parent.✅ No caveats.
Fades out the siblings of a hovered item.
<div class="sibling-fade">
<span>Item 1</span>
<span>Item 2</span>
<span>Item 3</span>
@@ -448,12 +552,60 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
.sibling-fade:hover span:not(:hover) {
opacity: 0.5;
}
- transition: opacity 0.2s specifies that changes to opacity will be transitioned over 0.2 seconds..sibling-fade:hover span:not(:hover) specifies that when the parent is hovered, select any span children that are not currently being hovered and change their opacity to 0.5.✅ 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>
+ transition: opacity 0.2s specifies that changes to opacity will be transitioned over 0.2 seconds..sibling-fade:hover span:not(:hover) specifies that when the parent is hovered, select any span children that are not currently being hovered and change their opacity to 0.5.✅ 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 if 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>
+ 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 if 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 toggle switch with CSS only.
<input type="checkbox" id="toggle" class="offscreen" />
+<label for="toggle" class="switch"></label>
+ .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;
+}
+ This effect is styling only the <label> element to look like a toggle switch, and hiding the actual <input> checkbox by positioning it offscreen. When clicking the label associated with the <input> element, it sets the <input> checkbox into the :checked state.
for attribute associates the <label> with the appropriate <input> checkbox element by its id..switch::after defines a pseudo-element for the <label> to create the circular knob.input[type='checkbox']:checked + .switch::after targets the <label>'s pseudo-element's style when the checkbox is checked.transform: translateX(20px) moves the pseudo-element (knob) 20px to the right when the checkbox is checked.background-color: #7983ff; sets the background-color of the switch to a different color when the checkbox is checked..offscreen moves the <input> 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.transition: all 0.3s specifies all property changes will be transitioned over 0.3 seconds, therefore transitioning the <label>'s background-color and the pseudo-element's transform property when the checkbox is checked.⚠️ Requires prefixes for full support.
Vertically and horizontally centers a child element within its parent element using position: absolute and transform: translate() (as an alternative to flexbox or display: table). Similar to flexbox, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.
<div class="parent">
+ <div class="child">Centered content</div>
+</div>
+ .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;
+}
+ position: absolute on the child element allows it to be positioned based on its containing block.left: 50% and top: 50% offsets the child 50% from the left and top edge of its containing block.transform: translate(-50%, -50%) allows the height and width of the child element to be negated so that it is vertically and horizontally centered.Note: Fixed height and width on parent element is for the demo only.
⚠️ Requires prefix for full support.
Creates a triangle shape with pure CSS.
<div class="triangle"></div>
.triangle {
width: 0;
height: 0;
@@ -461,11 +613,11 @@ var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
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>
+ 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;
width: 200px;
}
- 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.width: 200px; ensures the element has a dimension, to know when to get ellipsis⚠️ Only works for single line elements.