Box-sizing reset

Resets the box-model so that widths and heights are not affected by their borders or padding.

CSS

html {
  box-sizing: border-box;
}
*,
*::before,
*::after {
  box-sizing: inherit;
}
Copy the code

Demo

Explanation

  1. box-sizing: border-box makes the addition of padding or borders not affect an element’s width or height.
  2. box-sizing: inherit makes an element respect its parent’s box-sizing rule.

Browser support

✅ No caveats.

  • Caniuse.com/#feat=css3-…

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>
Copy the code

CSS

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}
.floated {
  float: left;
}
Copy the code

Demo

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

✅ 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>
Copy the code

CSS

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

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

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

Custom scrollbar

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. Iure id exercitationem nulla qui repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit? </p> </div>Copy the code

CSS

/* Document scrollbar */ ::-webkit-scrollbar { width: 8px; } ::-webkit-scrollbar-track {box-shadow: inset 0 0 6px rgba(0,0,0,0.3); border-radius: 10px; } ::-webkit-scrollbar-thumb { border-radius: 10px; Box-shadow: inset 0 0 6px rgba(0,0,0,0.5); } /* Scrollable element */ .some-element::webkit-scrollbar { }Copy the code

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. ::-webkit-scrollbar targets the whole scrollbar element.
  2. ::-webkit-scrollbar-track targets only the scrollbar track.
  3. ::-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

Browser support

⚠️ Scrollbar styling doesn’t appear to be on any standards track.

  • Caniuse.com/#feat=css-s…

Custom text selection

Changes the styling of text selection.

HTML

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

CSS

::selection {
  background: aquamarine;
  color: black;
}
.custom-text-selection::selection {
  background: deeppink;
  color: white;
}
Copy the code

Demo

Select some of this text.

Explanation

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

Browser support

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

  • Caniuse.com/#feat=css-s…

Disable selection

Makes the content unselectable.

HTML

<p>You can select me.</p> <p class="unselectable">You can't select me! </p>Copy the code

CSS

.unselectable {
  user-select: none;
}
Copy the code

Demo

You can select me.

You can’t select me!

Explanation

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

Browser support

⚠️ Requires prefixes for full support.

  • Caniuse.com/#feat=user-…

Donut spinner

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

HTML

<div class="donut"></div>
Copy the code

CSS

@keyframes donut-spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
.donut {
  display: inline-block;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-left-color: #7983ff;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  animation: donut-spin 1.2s linear infinite;
}
Copy the code

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

⚠️ Requires prefixes for full support.

  • Caniuse.com/#feat=css-a…
  • Caniuse.com/#feat=trans…

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>
Copy the code

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); -- ease-in-QUint: Cubic bezier(0.755, 0.05, 0.855, 0.06); -- Ease-in-EXPO: Cubic bezier(0.95, 0.05, 0.795, 0.035); -- Ease-in-CIRC: Cubic bezier(0.6, 0.04, 0.98, 0.335); --ease-out-quad: cubic- Bezier (0.25, 0.46, 0.45, 0.94); --ease-out-cubic: cubic-bezier(0.215, 0.61, 0.355, 1); -- ease-out-of-quart: cubic bezier(0.165, 0.84, 0.44, 1); --ease-out-quint: cubic bezier(0.23, 1, 0.32, 1); Ease-out-of-expo: cubic bezier(0.19, 1, 0.22, 1); -- ease-out-cirC: cubic-bezier(0.075, 0.82, 0.165, 1); -- Ease-in-out-quad: cubic- Bezier (0.455, 0.03, 0.515, 0.955); --ease-in-out cubic: cubic-bezier(0.645, 0.045, 0.355, 1); --ease-in-out-quart: cubic- Bezier (0.77, 0, 0.175, 1); --ease-in-out-quint: cubic bezier(0.86, 0, 0.07, 1); --ease-in-out-expo: cubic-bezier(1, 0, 0, 1); -- ease-in-out-CIRC: cubic- Bezier (0.785, 0.135, 0.15, 0.86); } .easing-variables { width: 50px; height: 50px; background: #333; transition: transform 1s var(--ease-out-quart); } .easing-variables:hover { transform: rotate(45deg); }Copy the code

Demo

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

✅ No caveats.

  • Caniuse.com/#feat=css-v…

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>
Copy the code

CSS

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

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

✅ No caveats.

  • Caniuse.com/#feat=css-t…

Evenly distributed children

Evenly distributes child elements within a parent element.

HTML

<div class="evenly-distributed-children">
  <p>Item1</p>
  <p>Item2</p>
  <p>Item3</p>
</div>
Copy the code

CSS

.evenly-distributed-children {
  display: flex;
  justify-content: space-between;
}
Copy the code

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

⚠️ Needs prefixes for full support.

  • Caniuse.com/#feat=flexb…

Gradient text

Gives text a gradient color.

HTML

<p class="gradient-text">Gradient text</p>
Copy the code

CSS

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

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

⚠ ️ Uses non – standard properties.

  • Caniuse.com/#feat=text-…

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>
Copy the code

CSS

.hairline-border { box-shadow: 0 0 0 1px; } @media (min-resolution: 2dppx) {. Hairline-shadow: 0 0 0 0.5px; }} @media (min-resolution: 3dppx) {. Hairline-shadow: 0 0 0 0.33333333px; }} @media (min-resolution: 4dppx) {. Hairline-shadow: 0 0 0 0.25px; }}Copy the code

Demo

Text with a hairline border around it.

Explanation

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

Browser Support

⚠️ Needs alternate syntax and JavaScript user agent checking for full support.

  • Caniuse.com/#feat=css-b…
  • Caniuse.com/#feat=css-m…

*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>
Copy the code

CSS

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

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

⚠️ Needs prefixes for full support.

  • Caniuse.com/#feat=flexb…

Hover underline animation

Creates an animated underline effect when the text is hovered over.

Credit: flatuicolors.com/

HTML

<p class="hover-underline-animation">Hover this text to see the effect! </p>Copy the code

CSS

.hover-underline-animation { display: inline-block; position: relative; color: #0087ca; } .hover-underline-animation::after { content: ''; position: absolute; width: 100%; transform: scaleX(0); height: 2px; bottom: 0; left: 0; background-color: #0087ca; transform-origin: bottom right; Transition: transform 0.25 s ease - out; } .hover-underline-animation:hover::after { transform: scaleX(1); transform-origin: bottom left; }Copy the code

Demo

Hover this text to see the effect!

Explanation

  1. 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).
  2. position: relative on the element establishes a Cartesian positioning context for pseudo-elements.
  3. ::after defines a pseudo-element.
  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 pseudo-element spans the entire width of the text block.
  6. transform: scaleX(0) initially scales the pseudo element to 0 so it has no width and is not visible.
  7. bottom: 0 and left: 0 position it to the bottom left of the block.
  8. The transition: transform 0.25 s ease - out means changes to transformWill be transitioned over 0.25 seconds with anease-out timing function.
  9. transform-origin: bottom right means the transform anchor point is positioned at the bottom right of the block.
  10. :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.

Browser support

✅ No caveats.

  • Caniuse.com/#feat=trans…
  • Caniuse.com/#feat=css-t…

Mouse cursor gradient tracking

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>
Copy the code

CSS

.mouse-cursor-gradient-tracking { position: relative; background: #7983ff; Padding: 0.5 rem 1 rem; The font - size: 1.2 rem; border: none; color: white; cursor: pointer; outline: none; overflow: hidden; } .mouse-cursor-gradient-tracking span { position: relative; } .mouse-cursor-gradient-tracking::before { --size: 0; content: ''; position: absolute; left: var(--x); top: var(--y); width: var(--size); height: var(--size); background: radial-gradient(circle closest-side, pink, transparent); transform: translate(-50%, -50%); transition: width .2s ease, height .2s ease; } .mouse-cursor-gradient-tracking:hover::before { --size: 200px; }Copy the code

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')
}
Copy the code

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
Copy the code

Browser support

  • Caniuse.com/#feat=css-v…

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>
Copy the code

CSS

.overflow-scroll-gradient { position: relative; } .overflow-scroll-gradient::after { content: ''; position: absolute; bottom: 0; width: 300px; height: 25px; Background: Linear-gradient (RGBA (255, 255, 255, 0.001), White); /* transparent keyword is broken in Safari */ pointer-events: none; } .overflow-scroll-gradient__scroller { overflow-y: scroll; background: white; width: 300px; height: 250px; padding: 15px 0; The line - height: 1.2; text-align: center; }Copy the code

Demo

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

✅ No caveats.

  • Caniuse.com/#feat=css-g…

Popout menu

Reveals an interactive popout menu on hover.

HTML

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

CSS

.reference {
  position: relative;
}
.popout-menu {
  position: absolute;
  visibility: hidden;
  left: 100%;
}
.reference:hover > .popout-menu {
  visibility: visible;
}
Copy the code

Demo

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

✅ 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>
Copy the code

CSS

.pretty-text-underline { display: inline; 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: 1.04 em 0; 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; }Copy the code

Demo

Pretty text underline without clipping descending letters.

Explanation

  1. text-shadow: ... has 4 values with offsets that cover a 4×4 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

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

  • Caniuse.com/#feat=css-t…
  • Caniuse.com/#feat=css-g…

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>
Copy the code

CSS

.shape-separator { position: relative; height: 48px; } .shape-separator::after { content: ''; background-image: url(data:image/svg+xml; base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwL XJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsM TItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+); position: absolute; width: 100%; height: 24px; bottom: 0; }Copy the code

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 24×24 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.
  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

✅ No caveats.

  • caniuse.com/#feat=svg

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>
Copy the code

CSS

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

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

✅ No caveats.

Triangle

Creates a triangle shape with pure CSS.

HTML

<div class="triangle"></div>
Copy the code

CSS

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

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

✅ 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>
Copy the code

CSS

.truncate-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 200px;
}
Copy the code

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.
  4. width: 200px; ensures the element has a dimension, to know when to get ellipsis

Browser support

⚠️ Only works for single line elements.

  • Caniuse.com/#feat=text-…