Update snippets

This commit is contained in:
Angelos Chalaris
2020-04-20 13:34:04 +03:00
parent d35dab24c3
commit 3077d4c71f
27 changed files with 141 additions and 386 deletions

View File

@ -14,7 +14,7 @@ Ensures that an element self-clears its children.
```
```css
.clearfix::after {
.clearfix:after {
content: '';
display: block;
clear: both;
@ -27,7 +27,7 @@ Ensures that an element self-clears its children.
#### Explanation
1. `.clearfix::after` defines a pseudo-element.
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.

View File

@ -1,48 +0,0 @@
---
title: Gradient border
tags: visual,intermediate
---
Creates a gradient border.
```html
<div class="gradient-border">
<p>Gradient border!</P>
</div>
```
```css
.gradient-border {
position: relative;
border: solid 1px transparent;
border-radius: 10px;
background-color: #f7f7fe;
background-clip: padding-box;
margin: 8px;
padding: 8px 16px;
}
.gradient-border:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: -1;
margin: -1px;
border-radius: inherit;
background: linear-gradient(to bottom, #d3e7ec, #fff);
}
```
#### Explanation
- Create a block with a transparent border, relative position and a background.
- Absolute position the `:before` pseudo-element with a gradient background and `z-index: -1`.
- Use `top: 0`, `right: 0`, `bottom: 0`, `left: 0` to make the pseudo-element equal size to its parent element, `margin: -1px` to make it larger.
- Use `background-clip: padding-box` to not draw the parent element's background below the border.
#### Browser support
- https://caniuse.com/#feat=mdn-css_properties_background_background-clip

View File

@ -11,17 +11,18 @@ Gives text a gradient color.
```css
.gradient-text {
background: -webkit-linear-gradient(pink, red);
background: linear-gradient(#70D6FF, #00072D);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
font-size: 32px;
}
```
#### 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.
- `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.
#### Browser support

View File

@ -1,50 +0,0 @@
---
title: Hairline border
tags: visual,advanced
---
Gives an element a border equal to 1 native device pixel in width, which can look very sharp and crisp.
```html
<div class="hairline-border">text</div>
```
```css
.hairline-border {
box-shadow: 0 0 0 1px;
}
@media (min-resolution: 2dppx) {
.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;
}
}
```
#### 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
<span class="snippet__support-note">⚠️ Needs alternate syntax and JavaScript user agent checking for full support.</span>
- https://caniuse.com/#feat=css-boxshadow
- https://caniuse.com/#feat=css-media-resolution
<br>
\* Chrome does not support subpixel values on `border`. Safari does not support subpixel values on `box-shadow`. Firefox supports subpixel values on both.

View File

@ -56,6 +56,3 @@ Displays a hamburger menu which transitions to a cross on hover.
- Set `transition all 0.5s` so that both `transform` and `opacity` properties are animated for half a second.
#### Browser support
- https://caniuse.com/#feat=flexbox
- https://caniuse.com/#feat=css-transitions

View File

@ -8,7 +8,7 @@ Transitions an element's height from `0` to `auto` when its height is unknown.
```html
<div class="trigger">
Hover me to see a height transition.
<div class="el">content</div>
<div class="el">Additional content</div>
</div>
```
@ -25,22 +25,19 @@ Transitions an element's height from `0` to `auto` when its height is unknown.
```
```js
var el = document.querySelector('.el')
var height = el.scrollHeight
el.style.setProperty('--max-height', height + 'px')
let el = document.querySelector('.el');
let height = el.scrollHeight;
el.style.setProperty('--max-height', height + 'px');
```
#### Explanation
1. `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.
2. `overflow: hidden` prevents the contents of the hidden element from overflowing its container.
3. `max-height: 0` specifies that the element has no height initially.
4. `.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.
<br>
1. `el.scrollHeight` is the height of the element including overflow, which will change dynamically based on the content of the element.
2. `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.
- `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.
#### Browser Support

View File

@ -30,15 +30,12 @@ Creates a shadow box around the text when it is hovered.
#### Explanation
1. `display: inline-block` to set width and length for `p` element thus making it an `inline-block`.
2. Set `transform: perspective(1px)` to give element a 3D space by affecting the distance between the Z plane and the user and `translate(0)` to reposition the `p` element along z-axis in 3D space.
3. `box-shadow:` to set up the box.
4. `transparent` to make box transparent.
5. `transition-property` to enable transitions for both `box-shadow` and `transform`.
6. `:hover` to activate whole css when hovering is done until `active`.
7. `transform: scale(1.2)` to change the scale, magnifying the text.
- `display: inline-block` to set width and length for `p` element thus making it an `inline-block`.
- Set `transform: perspective(1px)` to give element a 3D space by affecting the distance between the Z plane and the user and `translate(0)` to reposition the `p` element along z-axis in 3D space.
- `box-shadow:` to set up the box.
- `transparent` to make box transparent.
- `transition-property` to enable transitions for both `box-shadow` and `transform`.
- `:hover` to activate whole css when hovering is done until `active`.
- `transform: scale(1.2)` to change the scale, magnifying the text.
#### Browser Support
- https://caniuse.com/#feat=transforms3d
- https://caniuse.com/#feat=css-transitions

View File

@ -16,7 +16,7 @@ Creates an animated underline effect when the text is hovered over.
color: #0087ca;
}
.hover-underline-animation::after {
.hover-underline-animation:after {
content: '';
position: absolute;
width: 100%;
@ -29,7 +29,7 @@ Creates an animated underline effect when the text is hovered over.
transition: transform 0.25s ease-out;
}
.hover-underline-animation:hover::after {
.hover-underline-animation:hover:after {
transform: scaleX(1);
transform-origin: bottom left;
}
@ -37,18 +37,15 @@ Creates an animated underline effect when the text is hovered over.
#### 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. `transition: transform 0.25s ease-out` means changes to `transform` will be transitioned over 0.25 seconds with an `ease-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.
- `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.
#### Browser support
- https://caniuse.com/#feat=transforms2d
- https://caniuse.com/#feat=css-transitions

View File

@ -1,46 +0,0 @@
---
title: Last item with remaining available height
tags: layout,intermediate
---
Take advantage of available viewport space by giving the last element the remaining available space in current viewport, even when resizing the window.
```html
<div class="container">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
</div>
```
```css
html,
body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
display: flex;
flex-direction: column;
}
.container > div:last-child {
background-color: tomato;
flex: 1;
}
```
#### Explanation
1. `height: 100%` sets the height of container as viewport height.
2. `display: flex` creates a flexbox layout.
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 occupy all available space.
#### Browser support
- https://caniuse.com/#feat=flexbox

View File

@ -1,31 +0,0 @@
---
title: Lobotomized Owl Selector
tags: layout,beginner
---
Sets an automatically inherited margin for all elements that follow other elements in the document.
```html
<div>
<div>Parent 01</div>
<div>Parent 02
<div>Child 01</div>
<div>Child 02</div>
</div>
<div>Parent 03</div>
</div>
```
```css
* + * {
margin-top: 1.5em;
}
```
#### Explanation
- In this example, all elements in the flow of the document that follow other elements will receive `margin-top: 1.5em`.
- This example assumes that the paragraphs' `font-size` is 1em and its `line-height` is 1.5.
_Note: You can read [this article](https://alistapart.com/article/axiomatic-css-and-lobotomized-owls/_) for a more detailed explanation._
#### Browser support

View File

@ -70,9 +70,3 @@ Creates a vertical masonry layout using HTML and CSS.
- CSS variables are used to allow for greater flexibility for the layout, while media queries ensure that the layout flows responsively in different viewport sizes.
#### Browser support
- https://www.caniuse.com/#feat=css-variables
- https://www.caniuse.com/#feat=calc
- https://www.caniuse.com/#feat=mdn-css_properties_column-count
- https://www.caniuse.com/#feat=mdn-css_properties_column-width
- https://www.caniuse.com/#feat=mdn-css_properties_column-gap_multicol_context

View File

@ -28,7 +28,7 @@ A hover effect where the gradient follows the mouse cursor.
position: relative;
}
.mouse-cursor-gradient-tracking::before {
.mouse-cursor-gradient-tracking:before {
--size: 0;
content: '';
position: absolute;
@ -41,27 +41,27 @@ A hover effect where the gradient follows the mouse cursor.
transition: width 0.2s ease, height 0.2s ease;
}
.mouse-cursor-gradient-tracking:hover::before {
.mouse-cursor-gradient-tracking:hover:before {
--size: 200px;
}
```
```js
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
let btn = document.querySelector('.mouse-cursor-gradient-tracking');
btn.onmousemove = function(e) {
var rect = e.target.getBoundingClientRect()
var x = e.clientX - rect.left
var y = e.clientY - rect.top
btn.style.setProperty('--x', x + 'px')
btn.style.setProperty('--y', y + 'px')
let rect = e.target.getBoundingClientRect();
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
btn.style.setProperty('--x', x + 'px');
btn.style.setProperty('--y', y + 'px');
}
```
#### Explanation
1. `--x` and `--y` are used to track the position of the mouse on the button.
2. `--size` is used to keep modify of the gradient's dimensions.
3. `background: radial-gradient(circle closest-side, pink, transparent);` creates the gradient at the correct postion.
- `--x` and `--y` are used to track the position of the mouse on the button.
- `--size` is used to keep modify of the gradient's dimensions.
- `background: radial-gradient(circle closest-side, pink, transparent);` creates the gradient at the correct postion.
#### Browser support

View File

@ -6,28 +6,28 @@ tags: visual,beginner
Fancy hover and focus effect at navigation items using transform CSS property.
```html
<nav>
<nav class="hover-nav">
<ul>
<li><a href="#/">Home</a></li>
<li><a href="#/">About</a></li>
<li><a href="#/">Contact</a></li>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
```
```css
nav ul {
.hover-nav ul {
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
}
nav li {
.hover-nav li {
float: left;
}
nav li a {
.hover-nav li a {
position: relative;
display: block;
color: #222;
@ -36,7 +36,7 @@ nav li a {
text-decoration: none;
}
li a::before {
li a:before {
position: absolute;
content: "";
width: 100%;
@ -49,19 +49,16 @@ li a::before {
transition: transform 0.5s ease-in-out;
}
li a:hover::before,
li a:focus::before {
li a:hover:before,
li a:focus:before {
transform: scale(1);
}
```
#### Explanation
- Use the `::before` pseudo-element at the list item anchor to create a hover effect, hide it using `transform: scale(0)`.
- Use the `:before` pseudo-element at the list item anchor to create a hover effect, hide it using `transform: scale(0)`.
- Use the `:hover` and `:focus` pseudo-selectors to transition to `transform: scale(1)` and show the pseudo-element with its colored background.
- Prevent the pseudo-element from covering the anchor element by using `z-index: -1`.
#### Browser support
- https://caniuse.com/#feat=transforms2d
- https://caniuse.com/#feat=css-transitions

View File

@ -27,13 +27,13 @@ Provides an alternative to `display: none` (not readable by screen readers) and
#### 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.
- Remove all borders.
- Use `clip` to indicate that no part of the element should be shown.
- Make the height and width of the element 1px.
- Negate the elements height and width using `margin: -1px`.
- Hide the element's overflow.
- Remove all padding.
- Position the element absolutely so that it does not take up space in the DOM.
#### Browser support

View File

@ -25,7 +25,7 @@ Adds a fading gradient to an overflowing element to better indicate there is mor
position: relative;
}
.overflow-scroll-gradient::after {
.overflow-scroll-gradient:after {
content: '';
position: absolute;
bottom: 0;
@ -47,15 +47,13 @@ Adds a fading gradient to an overflowing element to better indicate there is mor
#### 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.
- `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.
#### Browser support
- https://caniuse.com/#feat=css-gradients

View File

@ -6,7 +6,9 @@ tags: interactivity,intermediate
Reveals an interactive popout menu on hover and focus.
```html
<div class="reference" tabindex="0"><div class="popout-menu">Popout menu</div></div>
<div class="reference" tabindex="0">
<div class="popout-menu">Popout menu</div>
</div>
```
```css
@ -14,7 +16,7 @@ Reveals an interactive popout menu on hover and focus.
position: relative;
background: tomato;
width: 100px;
height: 100px;
height: 80px;
}
.popout-menu {
@ -23,7 +25,7 @@ Reveals an interactive popout menu on hover and focus.
left: 100%;
background: #333;
color: white;
padding: 15px;
padding: 16px;
}
.reference:hover > .popout-menu,
@ -35,12 +37,12 @@ Reveals an interactive popout menu on hover and focus.
#### 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.
- `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.
#### Browser support

View File

@ -7,7 +7,7 @@ A nicer alternative to `text-decoration: underline` where descenders do not clip
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>
<p class="pretty-text-underline">Pretty text underline without clipping descenders.</p>
```
```css
@ -33,12 +33,9 @@ Natively implemented as `text-decoration-skip-ink: auto` but it has less control
#### 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.
- `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`).
- 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.
- The `::selection` pseudo selector rule ensures the text shadow does not interfere with text selection.
#### Browser support
- https://caniuse.com/#feat=css-textshadow
- https://caniuse.com/#feat=css-gradients

View File

@ -21,7 +21,7 @@ Creates a pulse effect loader animation using the `animation-delay` property.
.ripple-loader div {
position: absolute;
border: 4px solid #76ff03;
border: 4px solid #454ADE;
border-radius: 50%;
animation: ripple-loader 1s ease-out infinite;
}

View File

@ -16,7 +16,7 @@ Uses an SVG shape to separate two different blocks to create more a interesting
background: #333;
}
.shape-separator::after {
.shape-separator:after {
content: '';
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;
@ -28,13 +28,13 @@ Uses an SVG shape to separate two different blocks to create more a interesting
#### 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](http://yoksel.github.io/url-encoder/).
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.
- `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](http://yoksel.github.io/url-encoder/).
- `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.
#### Browser support

View File

@ -15,7 +15,7 @@ Fades out the siblings of a hovered item.
```css
span {
padding: 0 1rem;
transition: opacity 0.2s;
transition: opacity 0.3s;
}
.sibling-fade:hover span:not(:hover) {
@ -25,8 +25,8 @@ span {
#### Explanation
1. `transition: opacity 0.2s` specifies that changes to opacity will be transitioned over 0.2 seconds.
2. `.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`.
- `transition: opacity 0.2s` specifies that changes to opacity will be transitioned over 0.3 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`.
#### Browser support

View File

@ -20,15 +20,15 @@ Uses the native font of the operating system to get close to a native app feel.
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 Linux with KDE
6. `Ubuntu` is used on Ubuntu (all variants)
7. `Cantarell` is used on Linux with GNOME Shell
8. `"Helvetica Neue"` and `Helvetica` is used on macOS 10.10 and below (wrapped in quotes because it has a space)
9. `Arial` is a font widely supported by all operating systems
10. `sans-serif` is the fallback sans-serif font if none of the other fonts are supported
- `-apple-system` is San Francisco, used on iOS and macOS (not Chrome however)
- `BlinkMacSystemFont` is San Francisco, used on macOS Chrome
- `Segoe UI` is used on Windows 10
- `Roboto` is used on Android
- `Oxygen-Sans` is used on Linux with KDE
- `Ubuntu` is used on Ubuntu (all variants)
- `Cantarell` is used on Linux with GNOME Shell
- `"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 systems
- `sans-serif` is the fallback sans-serif font if none of the other fonts are supported
#### Browser support

View File

@ -20,7 +20,7 @@ Creates a toggle switch with CSS only.
transition: all 0.3s;
}
.switch::after {
.switch:after {
content: '';
position: absolute;
width: 18px;
@ -32,7 +32,7 @@ Creates a toggle switch with CSS only.
transition: all 0.3s;
}
input[type='checkbox']:checked + .switch::after {
input[type='checkbox']:checked + .switch:after {
transform: translateX(20px);
}
@ -50,14 +50,12 @@ input[type='checkbox']:checked + .switch {
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.
- The `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.
#### Browser support
- https://caniuse.com/#feat=transforms2d

View File

@ -1,12 +1,14 @@
---
title: Transform centering
tags: layout
tags: layout,beginner
---
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>
<div class="parent">
<div class="child">Centered content</div>
</div>
```
```css
@ -28,12 +30,10 @@ Vertically and horizontally centers a child element within its parent element us
#### Explanation
1. `position: absolute` on the child element allows it to be positioned based on its containing block.
2. `left: 50%` and `top: 50%` offsets the child 50% from the left and top edge of its containing block.
3. `transform: translate(-50%, -50%)` allows the height and width of the child element to be negated so that it is vertically and horizontally centered.
- `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: that the fixed height and width on parent element is for the demo only.
#### Browser support
- https://caniuse.com/#feat=transforms2d

View File

@ -1,39 +0,0 @@
---
title: Transform - Detransform
tags: visual,beginner
---
Sets a transform on the parent element and de-transforms the child elements, so they are not affected by the transform.
This allows for some neat effects such as skewed buttons.
```html
<div class="parent"><div class="child">Child content</div></div>
```
```css
:root {
--transform: 10deg;
}
.parent {
transform: skewX(var(--transform));
padding: 1rem;
border: 1px solid;
display: inline-block;
}
.child {
transform: skewX(calc(-1 * var(--transform)));
}
```
#### Explanation
- `--transform: 10deg` sets a CSS variable we can later use to prevent duplicate code.
- `calc(-1 * var(--transform))` on the child element negates the transform from the parent.
_Note: the `display` property of the child element may not be `inline`, otherwise the transform will be ignored - ([see also](https://drafts.csswg.org/css-transforms-1/#terminology))._
#### Browser support
- https://caniuse.com/#feat=transforms2d

View File

@ -37,12 +37,10 @@ If the text is longer than one line, it will be truncated for `n` lines and end
#### Explanation
1. `overflow: hidden` prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
2. `width: 400px` ensures the element has a dimension.
3. `height: 109.2px` calculated value for height, it equals `font-size * line-height * numberOfLines` (in this case `26 * 1.4 * 3 = 109.2`)
4. `height: 36.4px` calculated value for gradient container, it equals `font-size * line-height` (in this case `26 * 1.4 = 36.4`)
5. `background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%)` gradient from `transparent` to `#f5f6f9`
- `overflow: hidden` prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
- `width: 400px` ensures the element has a dimension.
- `height: 109.2px` calculated value for height, it equals `font-size * line-height * numberOfLines` (in this case `26 * 1.4 * 3 = 109.2`)
- `height: 36.4px` calculated value for gradient container, it equals `font-size * line-height` (in this case `26 * 1.4 = 36.4`)
- `background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%)` gradient from `transparent` to `#f5f6f9`
#### Browser support
- https://caniuse.com/#feat=css-gradients

View File

@ -20,13 +20,11 @@ If the text is longer than one line, it will be truncated and end with an ellips
#### 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
- `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
#### Browser support
<span class="snippet__support-note">⚠️ Only works for single line elements.</span>
- https://caniuse.com/#feat=text-overflow

View File

@ -27,5 +27,3 @@ li:nth-child(odd) {
- Note that you can use it to apply different styles to other HTML elements like `div`, `tr`, `p`, `ol`, etc.
#### Browser support
https://caniuse.com/#feat=css-sel3