Resets the box-model so that width
s and height
s are not affected by their border
s or padding
.
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
box-sizing: border-box
makes the addition ofpadding
orborder
s not affect an element'swidth
orheight
.box-sizing: inherit
makes an element respect its parent'sbox-sizing
rule.
✅ No caveats.
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.
<div class="clearfix">
<div class="floated">float a</div>
<div class="floated">float b</div>
<div class="floated">float c</div>
</div>
.clearfix::after {
content: '';
display: block;
clear: both;
}
.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.
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;
}
.constant-width-to-height-ratio::after {
content: '';
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.
Evenly distributes child elements within a parent element.
<div class="evenly-distributed-children">
<p>Item1</p>
<p>Item2</p>
<p>Item3</p>
</div>
.evenly-distributed-children {
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.
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;
}
Centered content.
display: flex
enables flexbox.justify-content: center
centers the child horizontally.align-items: center
centers the child vertically.
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;
}
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;
}
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
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 namesidebar
.
✅ 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
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.
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>
</div>
/* 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 {
}
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?
::-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
Changes the styling of text selection.
<p class="custom-text-selection">Select some of this text.</p>
::selection {
background: aquamarine;
color: black;
}
.custom-text-selection::selection {
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.
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 {
position: relative;
width: 10rem;
height: 10rem;
background: linear-gradient(75deg, #6d78ff, #00ffb8);
}
.dynamic-shadow::after {
content: '';
width: 100%;
height: 100%;
position: absolute;
background: inherit;
top: 0.5rem;
filter: blur(0.4rem);
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%
andheight: 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.
Creates an effect where text appears to be "etched" or engraved into the background.
<p class="etched-text">I appear etched into the background.</p>
.etched-text {
text-shadow: 0 2px white;
font-size: 1.5rem;
font-weight: bold;
color: #b8bec5;
}
I appear etched into the background.
text-shadow: 0 2px white
creates a white shadow offset 0px
horizontally and 2px
vertically
from the origin position.
The background must be darker than the shadow for the effect to work.
The text color should be slightly faded to make it look like it's engraved/carved out of the background.
✅ No caveats.
Gives text a gradient color.
<p class="gradient-text">Gradient text</p>
.gradient-text {
background: -webkit-linear-gradient(pink, red);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
}
Gradient text
background: -webkit-linear-gradient(...)
gives the text element a gradient background.webkit-text-fill-color: transparent
fills the text with a transparent color.webkit-background-clip: text
clips the background with the text, filling the text with the gradient background as the color.
Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.
<div class="hairline-border">text</div>
.hairline-border {
box-shadow: 0 0 0 1px;
}
@media (min-resolution: 2dppx) {
.hairline-border {
box-shadow: 0 0 0 0.5px;
}
}
@media (min-resolution: 3dppx) {
.hairline-border {
box-shadow: 0 0 0 0.33333333px;
}
}
@media (min-resolution: 4dppx) {
.hairline-border {
box-shadow: 0 0 0 0.25px;
}
}
Text with a hairline border around it.
box-shadow
, when only using spread, adds a pseudo-border which can use subpixels*.- Use
@media (min-resolution: ...)
to check the device pixel ratio (1dppx
equals 96 DPI), setting the spread of thebox-shadow
equal to1 / dppx
.
*Chrome does not support subpixel values on border
. Safari does not support subpixel values on box-shadow
. Firefox supports subpixel values on both.
Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.
<div class="overflow-scroll-gradient">
<div class="overflow-scroll-gradient__scroller">
Content to be scrolled
</div>
</div>
.overflow-scroll-gradient {
position: relative;
}
.overflow-scroll-gradient::after {
content: '';
position: absolute;
bottom: 0;
width: 240px;
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: 240px;
height: 200px;
padding: 15px 0;
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.
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;
background-image: linear-gradient(90deg, currentColor 100%, transparent 100%);
background-position: 0 0.98em;
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;
}
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 largerpx
size.background-image: linear-gradient(...)
creates a 90deg gradient with the current text color (currentColor
).- The
background-*
properties size the gradient as 1x1px at the bottom and repeats it along the x-axis. - The
::selection
pseudo selector ensures the text shadow does not interfere with text selection.
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>
<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;
}
The all
property allows you to reset all styles (inherited or not) to default values.
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;
background: #333;
}
.shape-separator::after {
content: '';
background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1taXRlcmxpbWl0PSIxLjQxNCI+PHBhdGggZD0iTTEyIDEybDEyIDEySDBsMTItMTJ6IiBmaWxsPSIjZmZmIi8+PC9zdmc+);
position: absolute;
width: 100%;
height: 24px;
bottom: 0;
}
position: relative
on the element establishes a Cartesian positioning context for 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.
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"
andHelvetica
is used on macOS 10.10 and below (wrapped in quotes because it has a space)Arial
is a font widely supported by all operating systemssans-serif
is the fallback sans-serif font if none of the other fonts are supported
✅ No caveats.
Creates a triangle shape with pure CSS.
<div class="triangle"></div>
.triangle {
width: 0;
height: 0;
border-top: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
View this link for a detailed explanation.
The color of the border is the color of the triangle. The side the triangle tip points
corresponds to the opposite border-*
property. For example, a color on border-top
means the arrow points downward.
Experiment with the px
values to change the proportion of the triangle.
✅ No caveats.
Creates a bouncing loader animation.
<div class="bouncing-loader">
<div></div>
<div></div>
<div></div>
</div>
@keyframes bouncing-loader {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0.1;
transform: translateY(-1rem);
}
}
.bouncing-loader {
display: flex;
justify-content: center;
}
.bouncing-loader > div {
width: 1rem;
height: 1rem;
margin: 3rem 0.2rem;
background: #8385aa;
border-radius: 50%;
animation: bouncing-loader 0.6s infinite alternate;
}
.bouncing-loader > div:nth-child(2) {
animation-delay: 0.2s;
}
.bouncing-loader > div:nth-child(3) {
animation-delay: 0.4s;
}
Note: 1rem
is usually 16px
.
-
@keyframes
defines an animation that has two states, where the element changesopacity
and is translated up on the 2D plane usingtransform: translateY()
. -
.bouncing-loader
is the parent container of the bouncing circles and usesdisplay: flex
andjustify-content: center
to position them in the center. -
.bouncing-loader > div
, targets the three childdiv
s of the parent to be styled. Thediv
s are given a width and height of1rem
, usingborder-radius: 50%
to turn them from squares to circles. -
margin: 3rem 0.2rem
specifies that each circle has a top/bottom margin of3rem
and left/right margin of0.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 thirddiv
respectively, so that each element does not start the animation at the same time.
✅ 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);
}
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;
}
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.
Variables that can be reused for transition-timing-function
properties, more
powerful than the built-in ease
, ease-in
, ease-out
and ease-in-out
.
<div class="easing-variables"></div>
:root {
--ease-in-quad: cubic-bezier(0.55, 0.085, 0.68, 0.53);
--ease-in-cubic: cubic-bezier(0.55, 0.055, 0.675, 0.19);
--ease-in-quart: cubic-bezier(0.895, 0.03, 0.685, 0.22);
--ease-in-quint: cubic-bezier(0.755, 0.05, 0.855, 0.06);
--ease-in-expo: cubic-bezier(0.95, 0.05, 0.795, 0.035);
--ease-in-circ: cubic-bezier(0.6, 0.04, 0.98, 0.335);
--ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
--ease-out-cubic: cubic-bezier(0.215, 0.61, 0.355, 1);
--ease-out-quart: cubic-bezier(0.165, 0.84, 0.44, 1);
--ease-out-quint: cubic-bezier(0.23, 1, 0.32, 1);
--ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1);
--ease-out-circ: cubic-bezier(0.075, 0.82, 0.165, 1);
--ease-in-out-quad: cubic-bezier(0.455, 0.03, 0.515, 0.955);
--ease-in-out-cubic: cubic-bezier(0.645, 0.045, 0.355, 1);
--ease-in-out-quart: cubic-bezier(0.77, 0, 0.175, 1);
--ease-in-out-quint: cubic-bezier(0.86, 0, 0.07, 1);
--ease-in-out-expo: cubic-bezier(1, 0, 0, 1);
--ease-in-out-circ: cubic-bezier(0.785, 0.135, 0.15, 0.86);
}
.easing-variables {
width: 50px;
height: 50px;
background: #333;
transition: transform 1s var(--ease-out-quart);
}
.easing-variables:hover {
transform: rotate(45deg);
}
The variables are defined globally 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 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;
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.25s ease-out;
}
.hover-underline-animation:hover::after {
transform: scaleX(1);
transform-origin: bottom left;
}
Hover this text to see the effect!
display: inline-block
makes the blockp
aninline-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
andleft: 0
position it to the bottom left of the block.transition: transform 0.25s ease-out
means changes totransform
will be transitioned over 0.25 seconds with anease-out
timing function.transform-origin: bottom right
means the transform anchor point is positioned at the bottom right of the block.:hover::after
then usesscaleX(1)
to transition the width to 100%, then changes thetransform-origin
tobottom left
so that the anchor point is reversed, allowing it transition out in the other direction when hovered off.
✅ 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.
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 {
position: relative;
background: #7983ff;
padding: 0.5rem 1rem;
font-size: 1.2rem;
border: none;
color: white;
cursor: pointer;
outline: none;
overflow: hidden;
}
.mouse-cursor-gradient-tracking span {
position: relative;
}
.mouse-cursor-gradient-tracking::before {
--size: 0;
content: '';
position: absolute;
left: var(--x);
top: var(--y);
width: var(--size);
height: var(--size);
background: radial-gradient(circle closest-side, pink, transparent);
transform: translate(-50%, -50%);
transition: width 0.2s ease, height 0.2s ease;
}
.mouse-cursor-gradient-tracking:hover::before {
--size: 200px;
}
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
btn.onmousemove = function(e) {
var x = e.pageX - btn.offsetLeft
var y = e.pageY - btn.offsetTop
btn.style.setProperty('--x', x + 'px')
btn.style.setProperty('--y', y + 'px')
}
TODO
Note!
If the element's parent has a positioning context (position: relative
), you will need to subtract
its offsets as well.
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
Reveals an interactive popout menu on hover.
<div class="reference">
<div class="popout-menu">
Popout menu
</div>
</div>
.reference {
position: relative;
}
.popout-menu {
position: absolute;
visibility: hidden;
left: 100%;
}
.reference:hover > .popout-menu {
visibility: visible;
}
position: relative
on the reference parent establishes a Cartesian positioning context for its child.position: absolute
takes the popout menu out of the flow of the document and positions it in relation to the parent.left: 100%
moves the the popout menu 100% of its parent's width from the left.visibility: hidden
hides the popout menu initially and allows for transitions (unlikedisplay: none
)..reference:hover > .popout-menu
means that when.reference
is hovered over, select immediate children with a class of.popout-menu
and change theirvisibility
tovisible
, which shows the popout.
✅ 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>
<span>Item 4</span>
<span>Item 5</span>
<span>Item 6</span>
</div>
span {
padding: 0 1rem;
transition: opacity 0.2s;
}
.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 anyspan
children that are not currently being hovered and change their opacity to0.5
.
✅ No caveats.
CSS variables that contain specific values to be reused throughout a document.
<p class="custom-variables">CSS is awesome!</p>
:root {
--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.