diff --git a/docs/index.html b/docs/index.html index d9c8bff78..8485039c3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -111,9 +111,9 @@ li::before { }

Demo

Explanation

You can create a ordered list using any type of HTML.

  1. counter-reset Initializes a counter, the value is the name of the counter. By default, the counter starts at 0. This property can also be used to change its value to any specific number.

  2. counter-increment Used in element that will be countable. Once counter-reset initialized, a counter's value can be increased or decreased.

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

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

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

Browser support

95.9%

✅ No caveats.

Custom scrollbarvisual

Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.

HTML

<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>
+    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>
@@ -162,9 +162,7 @@ li::before {
   user-select: none;
 }
 

Demo

You can select me.

You can't select me!

Explanation

user-select: none specifies that the text cannot be selected.

Browser support

89.2%

⚠️ Requires prefixes for full support. ⚠️ This is not a secure method to prevent users from copying content.

Display table centeringlayout

Vertically and horizontally centers a child element within its parent element using display: table (as an alternative to flexbox).

HTML

<div class="container">
-  <div class="center">
-    <span>Centered content</span>
-  </div>
+  <div class="center"><span>Centered content</span></div>
 </div>
 

CSS

.container {
   border: 1px solid #333;
@@ -181,7 +179,7 @@ li::before {
   text-align: center;
   vertical-align: middle;
 }
-

Demo

Centered content

Explanation

  1. display: table on '.center' allows the element to behave like a <table> HTML element.
  2. 100% height and width on '.center' allows the element to fill the available space within its parent element.
  3. display: table-cell on '.center > span' allows the element to behave like an HTML element.
  4. text-align: center on '.center > span' centers the child element horizontally.
  5. 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.

Browser support

99+%

✅ No caveats.

Donut spinneranimation

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

HTML

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

Demo

Centered content

Explanation

  1. display: table on '.center' allows the element to behave like a <table> HTML element.
  2. 100% height and width on '.center' allows the element to fill the available space within its parent element.
  3. display: table-cell on '.center > span' allows the element to behave like an HTML element.
  4. text-align: center on '.center > span' centers the child element horizontally.
  5. 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.

Browser support

99+%

✅ No caveats.

Donut spinneranimation

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);
@@ -270,8 +268,8 @@ li::before {
   display: flex;
   justify-content: space-between;
 }
-

Demo

Item1

Item2

Item3

Explanation

  1. display: flex enables flexbox.
  2. 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.

Browser support

95.5%

⚠️ Needs prefixes for full support.

Fit image in containerlayoutvisual

Changes the fit and position of an image within its container while preserving its aspect ratio. Previously only possible using a background image and the background-size property.

HTML

<img class="image image-contain" src="https://picsum.photos/600/200">
-<img class="image image-cover" src="https://picsum.photos/600/200">
+

Demo

Item1

Item2

Item3

Explanation

  1. display: flex enables flexbox.
  2. 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.

Browser support

95.5%

⚠️ Needs prefixes for full support.

Fit image in containerlayoutvisual

Changes the fit and position of an image within its container while preserving its aspect ratio. Previously only possible using a background image and the background-size property.

HTML

<img class="image image-contain" src="https://picsum.photos/600/200" />
+<img class="image image-cover" src="https://picsum.photos/600/200" />
 

CSS

.image {
   background: #34495e;
   border: 1px solid #34495e;
@@ -286,9 +284,7 @@ li::before {
   object-fit: cover;
   object-position: right top;
 }
-

Demo

Explanation

Browser support

91.5%

✅ No caveats.

Flexbox centeringlayout

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

HTML

<div class="flexbox-centering">
-  <div class="child">Centered content.</div>
-</div>
+

Demo

Explanation

Browser support

91.5%

✅ No caveats.

Flexbox centeringlayout

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

HTML

<div class="flexbox-centering"><div class="child">Centered content.</div></div>
 

CSS

.flexbox-centering {
   display: flex;
   justify-content: center;
@@ -301,9 +297,7 @@ li::before {
   -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.1%

⚠️ Uses non-standard properties.

Grid centeringlayout

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

HTML

<div class="grid-centering">
-  <div class="child">Centered content.</div>
-</div>
+

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.1%

⚠️ Uses non-standard properties.

Grid centeringlayout

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

HTML

<div class="grid-centering"><div class="child">Centered content.</div></div>
 

CSS

.grid-centering {
   display: grid;
   justify-content: center;
@@ -385,9 +379,7 @@ body {
   background-color: tomato;
   flex: 1;
 }
-

Demo

Div 1
Div 2
Div 3

Explanation

  1. height: 100% set the height of container as viewport height.
  2. display: flex enables flexbox.
  3. flex-direction: column set the direction of flex items' order from top to down.
  4. 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.

Browser support

95.5%

⚠️ Needs prefixes for full support.

Mouse cursor gradient trackingvisualinteractivity

A hover effect where the gradient follows the mouse cursor.

Credit: Tobias Reich

HTML

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

Demo

Div 1
Div 2
Div 3

Explanation

  1. height: 100% set the height of container as viewport height.
  2. display: flex enables flexbox.
  3. flex-direction: column set the direction of flex items' order from top to down.
  4. 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.

Browser support

95.5%

⚠️ Needs prefixes for full support.

Mouse cursor gradient trackingvisualinteractivity

A hover effect where the gradient follows the mouse cursor.

Credit: Tobias Reich

HTML

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

CSS

.mouse-cursor-gradient-tracking {
   position: relative;
   background: #7983ff;
@@ -424,7 +416,7 @@ btn.onmousemove = function(e) {
   btn.style.setProperty('--x', x + 'px')
   btn.style.setProperty('--y', y + 'px')
 }
-

Demo

Explanation

TODO

Browser support

87.6%

Requires JavaScript
⚠️ Requires JavaScript.

:not selectorvisual

The :not psuedo selector is useful for styling a group of elements, while leaving the last (or specified) element unstyled.

HTML

<ul class="css-not-selector-shortcut">
+

Demo

Explanation

TODO

Browser support

87.6%

Requires JavaScript
⚠️ Requires JavaScript.

:not selectorvisual

The :not psuedo selector is useful for styling a group of elements, while leaving the last (or specified) element unstyled.

HTML

<ul class="css-not-selector-shortcut">
   <li>One</li>
   <li>Two</li>
   <li>Three</li>
@@ -445,8 +437,7 @@ li:not(:last-child) {
   border-right: 2px solid #d2d5e4;
 }
 

Demo

Explanation

li:not(:last-child) specifies that the styles should apply to all li elements except the :last-child.

Browser support

95.9%

✅ No caveats.

Offscreenlayoutvisual

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.

HTML

<a class="button" href="http://pantswebsite.com">
-  Learn More
-  <span class="offscreen"> about pants</span>
+  Learn More <span class="offscreen"> about pants</span>
 </a>
 

CSS

.offscreen {
   border: 0;
@@ -460,13 +451,13 @@ li:not(:last-child) {
 }
 

Demo

Learn More about pants

Explanation

  1. Remove all borders.
  2. Use clip to indicate that no part of the element should be shown.
  3. Make the height and width of the element 1px.
  4. Negate the elements height and width using margin: -1px.
  5. Hide the element's overflow.
  6. Remove all padding.
  7. Position the element absolutely so that it does not take up space in the DOM.

Browser support

99+%

✅ No caveats.

(Although clip technically has been depreciated, the newer clip-path currently has very limited browser support.)

Overflow scroll gradientvisual

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">
-    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>
+    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>
@@ -493,11 +484,7 @@ li:not(:last-child) {
   padding: 15px;
   line-height: 1.2;
 }
-

Demo

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

Explanation

  1. position: relative on the parent establishes a Cartesian positioning context for pseudo-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: 240px 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 pseudo-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 pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.

Browser support

93.5%

✅ No caveats.

Popout menuinteractivity

Reveals an interactive popout menu on hover and focus.

HTML

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

Demo

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

Explanation

  1. position: relative on the parent establishes a Cartesian positioning context for pseudo-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: 240px 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 pseudo-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 pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.

Browser support

93.5%

✅ No caveats.

Popout menuinteractivity

Reveals an interactive popout menu on hover and focus.

HTML

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

CSS

.reference {
   position: relative;
   background: tomato;
@@ -517,7 +504,7 @@ li:not(:last-child) {
 .reference:focus-within > .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.
  6. .reference:focus > .popout-menu means that when .reference is focused, the popout would be shown.
  7. .reference:focus-within > .popout-menu ensures that the popout is shown when the focus is within the reference.

Browser support

99+%

✅ No caveats.

Pretty text underlinevisual

A nicer alternative to text-decoration: underline or <u></u> 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>
+

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.
  6. .reference:focus > .popout-menu means that when .reference is focused, the popout would be shown.
  7. .reference:focus-within > .popout-menu ensures that the popout is shown when the focus is within the reference.

Browser support

99+%

✅ No caveats.

Pretty text underlinevisual

A nicer alternative to text-decoration: underline or <u></u> 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;
   text-shadow: 1px 1px #f5f6f9, -1px 1px #f5f6f9, -1px -1px #f5f6f9, 1px -1px #f5f6f9;
@@ -536,12 +523,16 @@ li:not(:last-child) {
 }
 

Demo

Pretty text underline without clipping descending letters.

Explanation

  1. 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.
  2. background-image: linear-gradient(...) creates a 90deg gradient using the text color (currentColor).
  3. The 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.
  4. The ::selection pseudo selector rule ensures the text shadow does not interfere with text selection.

Browser support

93.5%

✅ No caveats.

Reset all stylesvisual

Resets all styles to default values with one property. This will not affect direction and unicode-bidi properties.

HTML

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

CSS

.reset-all-styles {
   all: initial;
 }
-

Demo

Title

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?

Explanation

The all property allows you to reset all styles (inherited or not) to default values.

Browser support

87.2%

⚠️ MS Edge status is under consideration.

Shape separatorvisual

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

Demo

Title

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?

Explanation

The all property allows you to reset all styles (inherited or not) to default values.

Browser support

87.2%

⚠️ MS Edge status is under consideration.

Shape separatorvisual

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;
@@ -556,12 +547,8 @@ li:not(:last-child) {
   bottom: 0;
 }
 

Demo

Explanation

  1. position: relative on the element establishes a Cartesian positioning context for pseudo elements.
  2. ::after defines a pseudo element.
  3. 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.
  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: 12px is the same height as the shape.
  7. bottom: 0 positions the pseudo element at the bottom of the parent.

Browser support

95.7%

✅ No caveats.

Sibling fadeinteractivity

Fades out the siblings of a hovered item.

HTML

<div class="sibling-fade">
-  <span>Item 1</span>
-  <span>Item 2</span>
-  <span>Item 3</span>
-  <span>Item 4</span>
-  <span>Item 5</span>
-  <span>Item 6</span>
+  <span>Item 1</span> <span>Item 2</span> <span>Item 3</span> <span>Item 4</span>
+  <span>Item 5</span> <span>Item 6</span>
 </div>
 

CSS

span {
   padding: 0 1rem;
@@ -575,8 +562,7 @@ li:not(:last-child) {
   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.

Toggle switchvisualinteractivity

Creates a toggle switch with CSS only.

HTML

<input type="checkbox" id="toggle" class="offscreen" />
-<label for="toggle" class="switch"></label>
+

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.

Toggle switchvisualinteractivity

Creates a toggle switch with CSS only.

HTML

<input type="checkbox" id="toggle" class="offscreen" /> <label for="toggle" class="switch"></label>
 

CSS

.switch {
   position: relative;
   display: inline-block;
@@ -607,9 +593,7 @@ input[type='checkbox']:checked + .switch {
   position: absolute;
   left: -9999px;
 }
-

Demo

Explanation

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.

  1. The for attribute associates the <label> with the appropriate <input> checkbox element by its id.
  2. .switch::after defines a pseudo-element for the <label> to create the circular knob.
  3. input[type='checkbox']:checked + .switch::after targets the <label>'s pseudo-element's style when the checkbox is checked.
  4. transform: translateX(20px) moves the pseudo-element (knob) 20px to the right when the checkbox is checked.
  5. background-color: #7983ff; sets the background-color of the switch to a different color when the checkbox is checked.
  6. .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.
  7. 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.

Browser support

93.7%

⚠️ Requires prefixes for full support.

Transform centeringlayout

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.

HTML

<div class="parent">
-  <div class="child">Centered content</div>
-</div>
+

Demo

Explanation

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.

  1. The for attribute associates the <label> with the appropriate <input> checkbox element by its id.
  2. .switch::after defines a pseudo-element for the <label> to create the circular knob.
  3. input[type='checkbox']:checked + .switch::after targets the <label>'s pseudo-element's style when the checkbox is checked.
  4. transform: translateX(20px) moves the pseudo-element (knob) 20px to the right when the checkbox is checked.
  5. background-color: #7983ff; sets the background-color of the switch to a different color when the checkbox is checked.
  6. .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.
  7. 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.

Browser support

93.7%

⚠️ Requires prefixes for full support.

Transform centeringlayout

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.

HTML

<div class="parent"><div class="child">Centered content</div></div>
 

CSS

.parent {
   border: 1px solid #333;
   height: 250px;
diff --git a/index.html b/index.html
index 7b8cb2766..217657e75 100644
--- a/index.html
+++ b/index.html
@@ -519,9 +519,9 @@ li::before {
             

Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.

HTML

<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>
+    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>
@@ -719,9 +719,7 @@ in any specification.

Display table centeringlayout

Vertically and horizontally centers a child element within its parent element using display: table (as an alternative to flexbox).

HTML

<div class="container">
-  <div class="center">
-    <span>Centered content</span>
-  </div>
+  <div class="center"><span>Centered content</span></div>
 </div>
 

CSS

.container {
@@ -743,9 +741,7 @@ in any specification.

Demo

-
- Centered content -
+
Centered content