diff --git a/css/snippet-template.md b/css/snippet-template.md new file mode 100644 index 000000000..782c0e7b7 --- /dev/null +++ b/css/snippet-template.md @@ -0,0 +1,31 @@ +--- +title: Snippet Name +type: snippet +tags: [other] +cover: image +dateModified: 2021-06-13T05:00:00-04:00 +--- + +Explain briefly what the snippet does. + +- Explain briefly how the snippet works. +- Use bullet points for your snippet's explanation. +- Try to explain everything briefly but clearly. + +```html +
+``` + +```css +.my-snippet { + background-color: #fff; + border: 1px solid #000; + border-radius: 4px; +} +``` + +```js +console.log( + "This is optional, if your snippet doesn't require JavaScript, be sure to delete this block!" +) +``` diff --git a/css/snippets/aspect-ratio.md b/css/snippets/aspect-ratio.md new file mode 100644 index 000000000..307b0179c --- /dev/null +++ b/css/snippets/aspect-ratio.md @@ -0,0 +1,38 @@ +--- +title: Aspect ratio +type: snippet +tags: [layout] +author: chalarangelo +cover: digital-nomad-12 +dateModified: 2022-08-14T05:00:00-04:00 +--- + +Creates a responsive container with a specified aspect ratio. + +- Use a CSS custom property, `--aspect-ratio` to define the desired aspect ratio. +- Set the container element to `position: relative` and `height: 0`, calculating its height using the `calc()` function and the `--aspect-ratio` custom property. +- Set the direct child of the container element to `position: absolute` and filling it parent, while giving it `object-fit: cover` to maintain the aspect ratio. + +```html +
+ +
+``` + +```css +.container { + --aspect-ratio: 16/9; + position: relative; + height: 0; + padding-bottom: calc(100% / (var(--aspect-ratio))); +} + +.container > * { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + object-fit: cover; +} +``` diff --git a/css/snippets/border-with-top-triangle.md b/css/snippets/border-with-top-triangle.md new file mode 100644 index 000000000..cfd3d7cf4 --- /dev/null +++ b/css/snippets/border-with-top-triangle.md @@ -0,0 +1,44 @@ +--- +title: Border with top triangle +type: snippet +tags: [visual] +cover: greek-coffee +dateModified: 2021-01-07T23:52:15+02:00 +--- + +Creates a content container with a triangle at the top. + +- Use the `::before` and `::after` pseudo-elements to create two triangles. +- The colors of the two triangles should be the same as the container's `border-color` and the container's `background-color` respectively. +- The `border-width` of the one triangle (`::before`) should be `1px` wider than the other one (`::after`), in order to act as the border. +- The smaller triangle (`::after`) should be `1px` to the right of the larger triangle (`::before`) to allow for its left border to be shown. + +```html +
Border with top triangle
+``` + +```css +.container { + position: relative; + background: #ffffff; + padding: 15px; + border: 1px solid #dddddd; + margin-top: 20px; +} + +.container::before, +.container::after { + content: ''; + position: absolute; + bottom: 100%; + left: 19px; + border: 11px solid transparent; + border-bottom-color: #dddddd; +} + +.container::after { + left: 20px; + border: 10px solid transparent; + border-bottom-color: #ffffff; +} +``` diff --git a/css/snippets/bouncing-loader.md b/css/snippets/bouncing-loader.md new file mode 100644 index 000000000..74caefac7 --- /dev/null +++ b/css/snippets/bouncing-loader.md @@ -0,0 +1,54 @@ +--- +title: Bouncing loader +type: snippet +tags: [animation] +cover: digital-nomad-12 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a bouncing loader animation. + +- Use `@keyframes` to define a bouncing animation, using the `opacity` and `transform` properties. Use a single axis translation on `transform: translate3d()` to achieve better animation performance. +- Create a parent container, `.bouncing-loader`, for the bouncing circles. Use `display: flex` and `justify-content: center` to position them in the center. +- Give the three bouncing circle `
` elements the same `width` and `height` and `border-radius: 50%` to make them circular. +- Apply the `bouncing-loader` animation to each of the three bouncing circles. +- Use a different `animation-delay` for each circle and `animation-direction: alternate` to create the appropriate effect. + +```html +
+
+
+
+
+``` + +```css +@keyframes bouncing-loader { + to { + opacity: 0.1; + transform: translate3d(0, -16px, 0); + } +} + +.bouncing-loader { + display: flex; + justify-content: center; +} + +.bouncing-loader > div { + width: 16px; + height: 16px; + 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; +} +``` diff --git a/css/snippets/box-sizing-reset.md b/css/snippets/box-sizing-reset.md new file mode 100644 index 000000000..3c8034f8f --- /dev/null +++ b/css/snippets/box-sizing-reset.md @@ -0,0 +1,45 @@ +--- +title: Box-sizing reset +type: snippet +tags: [layout] +cover: interior +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Resets the box-model so that `width` and `height` are not affected by `border` or `padding`. + +- Use `box-sizing: border-box` to include the width and height of `padding` and `border` when calculating the element's `width` and `height`. +- Use `box-sizing: inherit` to pass down the `box-sizing` property from parent to child elements. + +```html +
border-box
+
content-box
+``` + +```css +div { + box-sizing: border-box; +} + +*, +*::before, +*::after { + box-sizing: inherit; +} + +.box { + display: inline-block; + width: 120px; + height: 120px; + padding: 8px; + margin: 8px; + background: #F24333; + color: white; + border: 1px solid #BA1B1D; + border-radius: 4px; +} + +.content-box { + box-sizing: content-box; +} +``` diff --git a/css/snippets/broken-image-fallback.md b/css/snippets/broken-image-fallback.md new file mode 100644 index 000000000..831217fd6 --- /dev/null +++ b/css/snippets/broken-image-fallback.md @@ -0,0 +1,43 @@ +--- +title: Fallback for images that fail to load +type: snippet +shortTitle: Broken image fallback +tags: [visual] +author: chalarangelo +cover: building-facade +dateModified: 2022-11-04T05:00:00-04:00 +--- + +Displays an error message when an image fails to load. + +- Apply styles to the `img` element as if it was a text container. +- Use the `::before` and `::after` pseudo-elements to display an error message and the image URL. These elements will only be displayed if the image fails to load. + +```html + +``` + +```css +img { + display: block; + font-family: sans-serif; + font-weight: 300; + height: auto; + line-height: 2; + position: relative; + text-align: center; + width: 100%; +} + +img::before { + content: "Sorry, this image is unavailable."; + display: block; + margin-bottom: 8px; +} + +img::after { + content: "(url: " attr(src) ")"; + display: block; + font-size: 12px; +} +``` diff --git a/css/snippets/button-border-animation.md b/css/snippets/button-border-animation.md new file mode 100644 index 000000000..60977819a --- /dev/null +++ b/css/snippets/button-border-animation.md @@ -0,0 +1,54 @@ +--- +title: Button border animation +type: snippet +tags: [animation] +cover: highlands +dateModified: 2021-05-24T15:28:52+03:00 +--- + +Creates a border animation on hover. + +- Use the `::before` and `::after` pseudo-elements to create two boxes `24px` wide opposite each other above and below the box. +- Use the `:hover` pseudo-class to extend the `width` of those elements to `100%` on hover and animate the change using `transition`. + +```html + +``` + +```css +.animated-border-button { + background-color: #263059; + border: none; + color: #ffffff; + outline: none; + padding: 12px 40px 10px; + position: relative; +} + +.animated-border-button::before, +.animated-border-button::after { + border: 0 solid transparent; + transition: all 0.3s; + content: ''; + height: 0; + position: absolute; + width: 24px; +} + +.animated-border-button::before { + border-top: 2px solid #263059; + right: 0; + top: -4px; +} + +.animated-border-button::after { + border-bottom: 2px solid #263059; + bottom: -4px; + left: 0; +} + +.animated-border-button:hover::before, +.animated-border-button:hover::after { + width: 100%; +} +``` diff --git a/css/snippets/button-focus-swing-animation.md b/css/snippets/button-focus-swing-animation.md new file mode 100644 index 000000000..24d0dabfb --- /dev/null +++ b/css/snippets/button-focus-swing-animation.md @@ -0,0 +1,56 @@ +--- +title: Button swing animation +type: snippet +tags: [animation] +author: chalarangelo +cover: painters-desk +dateModified: 2021-05-24T15:28:52+03:00 +--- + +Creates a swing animation on focus. + +- Use an appropriate `transition` to animate changes to the element. +- Use the `:focus` pseudo-class to apply an `animation` that uses `transform` to make the element swing. +- Use `animation-iteration-count` to only play the animation once. + +```html + +``` + +```css +.button-swing { + color: #65b5f6; + background-color: transparent; + border: 1px solid #65b5f6; + border-radius: 4px; + padding: 0 16px; + cursor: pointer; + transition: all 0.2s ease-in-out; +} + +.button-swing:focus { + animation: swing 1s ease; + animation-iteration-count: 1; +} + +@keyframes swing { + 15% { + transform: translateX(5px); + } + 30% { + transform: translateX(-5px); + } + 50% { + transform: translateX(3px); + } + 65% { + transform: translateX(-3px); + } + 80% { + transform: translateX(2px); + } + 100% { + transform: translateX(0); + } +} +``` diff --git a/css/snippets/button-hover-fill-animation.md b/css/snippets/button-hover-fill-animation.md new file mode 100644 index 000000000..231bae2ff --- /dev/null +++ b/css/snippets/button-hover-fill-animation.md @@ -0,0 +1,32 @@ +--- +title: Button fill animation +type: snippet +tags: [animation] +cover: beach-pineapple +dateModified: 2021-04-02T21:34:43+03:00 +--- + +Creates a fill animation on hover. + +- Set a `color` and `background` and use an appropriate `transition` to animate changes to the element. +- Use the `:hover` pseudo-class to change the `background` and `color` of the element when the user hovers over it. + +```html + +``` + +```css +.animated-fill-button { + padding: 20px; + background: #fff; + color: #000; + border: 1px solid #000; + cursor: pointer; + transition: 0.3s all ease-in-out; +} + +.animated-fill-button:hover { + background: #000; + color: #fff; +} +``` diff --git a/css/snippets/button-hover-grow-animation.md b/css/snippets/button-hover-grow-animation.md new file mode 100644 index 000000000..83814fc04 --- /dev/null +++ b/css/snippets/button-hover-grow-animation.md @@ -0,0 +1,33 @@ +--- +title: Button grow animation +type: snippet +tags: [animation] +author: chalarangelo +cover: white-laptop +dateModified: 2021-05-24T15:28:52+03:00 +--- + +Creates a grow animation on hover. + +- Use an appropriate `transition` to animate changes to the element. +- Use the `:hover` pseudo-class to change the `transform` to `scale(1.1)`, growing the element when the user hovers over it. + +```html + +``` + +```css +.button-grow { + color: #65b5f6; + background-color: transparent; + border: 1px solid #65b5f6; + border-radius: 4px; + padding: 0 16px; + cursor: pointer; + transition: all 0.3s ease-in-out; +} + +.button-grow:hover { + transform: scale(1.1); +} +``` diff --git a/css/snippets/button-hover-shrink-animation.md b/css/snippets/button-hover-shrink-animation.md new file mode 100644 index 000000000..2b0f7371f --- /dev/null +++ b/css/snippets/button-hover-shrink-animation.md @@ -0,0 +1,33 @@ +--- +title: Button shrink animation +type: snippet +tags: [animation] +author: chalarangelo +cover: clay-pot-horizon +dateModified: 2021-05-24T15:28:52+03:00 +--- + +Creates a shrink animation on hover. + +- Use an appropriate `transition` to animate changes to the element. +- Use the `:hover` pseudo-class to change the `transform` to `scale(0.8)`, shrinking the element when the user hovers over it. + +```html + +``` + +```css +.button-shrink { + color: #65b5f6; + background-color: transparent; + border: 1px solid #65b5f6; + border-radius: 4px; + padding: 0 16px; + cursor: pointer; + transition: all 0.3s ease-in-out; +} + +.button-shrink:hover { + transform: scale(0.8); +} +``` diff --git a/css/snippets/card-image-cutout.md b/css/snippets/card-image-cutout.md new file mode 100644 index 000000000..7c70d07d9 --- /dev/null +++ b/css/snippets/card-image-cutout.md @@ -0,0 +1,81 @@ +--- +title: Card with image cutout +type: snippet +tags: [visual] +author: chalarangelo +cover: radio-monstera +dateModified: 2022-12-11T05:00:00-04:00 +--- + +Creates a card with an image cutout. + +- Use `background` to add a colored background to a `.container` element. +- Create a `.card` containing a `figure` with the appropriate image for the cutout and any other content you want. +- Use the `::before` pseudo-element to add a `border` around the `figure` element, matching the `.container` element's `background` and creating the illusion of a cutout in the `.card`. + +```html +
+
+
+ +
+

Lorem ipsum dolor sit amet consectetur adipisicing elit.

+
+
+``` + +```css +.container { + display: flex; + padding: 96px 24px 48px; + justify-content: center; + align-items: center; + background: #f3f1fe; +} + +.card { + width: 350px; + display: flex; + flex-direction: column; + align-items: center; + background: #fff; + border-radius: 10px; + margin: 8px; + box-shadow: 0 0 5px -2px rgba(0, 0, 0, 0.1); +} + +.card figure { + width: 120px; + height: 120px; + border-radius: 50%; + margin-top: -60px; + position: relative; +} + +.card figure::before { + content: ""; + border-radius: inherit; + position: absolute; + top: 50%; + left: 50%; + width: 100%; + height: 100%; + transform: translate(-50%, -50%); + border: 1rem solid #f3f1fe; + box-shadow: 0 1px rgba(0, 0, 0, 0.1); +} + +.card figure img { + border-radius: inherit; + width: 100%; + height: 100%; + object-fit: cover; +} + +.card .content { + text-align: center; + margin: 2rem; + line-height: 1.5; + color: #101010; +} +``` diff --git a/css/snippets/checkerboard-pattern.md b/css/snippets/checkerboard-pattern.md new file mode 100644 index 000000000..7a9bbf8af --- /dev/null +++ b/css/snippets/checkerboard-pattern.md @@ -0,0 +1,44 @@ +--- +title: Checkerboard background pattern +type: snippet +tags: [visual] +cover: digital-nomad-2 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a checkerboard background pattern. + +- Use `background-color` to set a white background. +- Use `background-image` with two `linear-gradient()` values. Give each one a different angle to create the checkerboard pattern. +- Use `background-size` to specify the pattern's size. +- **Note:** The fixed `height` and `width` of the element is for demonstration purposes only. + +```html +
+``` + +```css +.checkerboard { + width: 240px; + height: 240px; + background-color: #fff; + background-image: linear-gradient( + 45deg, + #000 25%, + transparent 25%, + transparent 75%, + #000 75%, + #000 + ), + linear-gradient( + -45deg, + #000 25%, + transparent 25%, + transparent 75%, + #000 75%, + #000 + ); + background-size: 60px 60px; + background-repeat: repeat; +} +``` diff --git a/css/snippets/circle.md b/css/snippets/circle.md new file mode 100644 index 000000000..3c5ed7518 --- /dev/null +++ b/css/snippets/circle.md @@ -0,0 +1,25 @@ +--- +title: Circle +type: snippet +tags: [visual] +cover: oven-paddle +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a circular shape with pure CSS. + +- Use `border-radius: 50%` to curve the borders of the 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. + +```html +
+``` + +```css +.circle { + border-radius: 50%; + width: 32px; + height: 32px; + background: #9C27B0; +} +``` diff --git a/css/snippets/clearfix.md b/css/snippets/clearfix.md new file mode 100644 index 000000000..864fe4b63 --- /dev/null +++ b/css/snippets/clearfix.md @@ -0,0 +1,35 @@ +--- +title: Clearfix +type: snippet +tags: [layout] +cover: memories-of-pineapple-3 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Ensures that an element self-clears its children. + +- Use the `::after` pseudo-element and apply `content: ''` to allow it to affect layout. +- Use `clear: both` to make the element clear past both left and right floats. +- For this technique to work properly, make sure there are no non-floating children in the container and that there are no tall floats before the clearfixed container but in the same formatting context (e.g. floated columns). +- **Note:** This is only useful if you are using `float` to build layouts. Consider using a more modern approach, such as the flexbox or grid layout. + +```html +
+
float a
+
float b
+
float c
+
+``` + +```css +.clearfix::after { + content: ''; + display: block; + clear: both; +} + +.floated { + float: left; + padding: 4px; +} +``` diff --git a/css/snippets/constant-width-to-height-ratio.md b/css/snippets/constant-width-to-height-ratio.md new file mode 100644 index 000000000..68a91da6d --- /dev/null +++ b/css/snippets/constant-width-to-height-ratio.md @@ -0,0 +1,35 @@ +--- +title: Constant width to height ratio +type: snippet +tags: [layout] +cover: clutter +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Ensures that an element with variable `width` will retain a proportionate `height` value. + +- Apply `padding-top` on the `::before` pseudo-element, making the `height` of the element equal to a percentage of its `width`. +- The proportion of `height` to `width` can be altered as necessary. For example a `padding-top` of `100%` will create a responsive square (1:1 ratio). + +```html +
+``` + +```css +.constant-width-to-height-ratio { + background: #9C27B0; + width: 50%; +} + +.constant-width-to-height-ratio::before { + content: ''; + padding-top: 100%; + float: left; +} + +.constant-width-to-height-ratio::after { + content: ''; + display: block; + clear: both; +} +``` diff --git a/css/snippets/counter.md b/css/snippets/counter.md new file mode 100644 index 000000000..491cc9cd1 --- /dev/null +++ b/css/snippets/counter.md @@ -0,0 +1,40 @@ +--- +title: Counter +type: snippet +tags: [visual] +cover: laptop-plants +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a custom list counter that accounts for nested list elements. + +- Use `counter-reset` to initialize a variable counter (default `0`), the name of which is the value of the attribute (i.e. `counter`). +- Use `counter-increment` on the variable counter for each countable element (i.e. each `
  • `). +- Use `counters()` to display the value of each variable counter as part of the `content` of the `::before` pseudo-element for each countable element (i.e. each `
  • `). The second value passed to it (`'.'`) acts as the delimiter for nested counters. + +```html + +``` + +```css +ul { + counter-reset: counter; + list-style: none; +} + +li::before { + counter-increment: counter; + content: counters(counter, '.') ' '; +} +``` diff --git a/css/snippets/custom-checkbox.md b/css/snippets/custom-checkbox.md new file mode 100644 index 000000000..b5ad2a36f --- /dev/null +++ b/css/snippets/custom-checkbox.md @@ -0,0 +1,151 @@ +--- +title: Custom checkbox +type: snippet +tags: [visual,animation] +author: chalarangelo +cover: interior-8 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a styled checkbox with animation on state change. + +- Use an `` element to create the check `` and insert it via the `` element to create a reusable SVG icon. +- Create a `.checkbox-container` and use flexbox to create the appropriate layout for the checkboxes. +- Hide the `
    + + + + +
    +``` + +```css +.checkbox-symbol { + position: absolute; + width: 0; + height: 0; + pointer-events: none; + user-select: none; +} + +.checkbox-container { + box-sizing: border-box; + background: #ffffff; + color: #222; + height: 64px; + display: flex; + justify-content: center; + align-items: center; + flex-flow: row wrap; +} + +.checkbox-container * { + box-sizing: border-box; +} + +.checkbox-input { + position: absolute; + visibility: hidden; +} + +.checkbox { + user-select: none; + cursor: pointer; + padding: 6px 8px; + border-radius: 6px; + overflow: hidden; + transition: all 0.3s ease; + display: flex; +} + +.checkbox:not(:last-child) { + margin-right: 6px; +} + +.checkbox:hover { + background: rgba(0, 119, 255, 0.06); +} + +.checkbox span { + vertical-align: middle; + transform: translate3d(0, 0, 0); +} + +.checkbox span:first-child { + position: relative; + flex: 0 0 18px; + width: 18px; + height: 18px; + border-radius: 4px; + transform: scale(1); + border: 1px solid #cccfdb; + transition: all 0.3s ease; +} + +.checkbox span:first-child svg { + position: absolute; + top: 3px; + left: 2px; + fill: none; + stroke: #fff; + stroke-dasharray: 16px; + stroke-dashoffset: 16px; + transition: all 0.3s ease; + transform: translate3d(0, 0, 0); +} + +.checkbox span:last-child { + padding-left: 8px; + line-height: 18px; +} + +.checkbox:hover span:first-child { + border-color: #0077ff; +} + +.checkbox-input:checked + .checkbox span:first-child { + background: #0077ff; + border-color: #0077ff; + animation: zoom-in-out 0.3s ease; +} + +.checkbox-input:checked + .checkbox span:first-child svg { + stroke-dashoffset: 0; +} + +@keyframes zoom-in-out { + 50% { + transform: scale(0.9); + } +} +``` diff --git a/css/snippets/custom-radio.md b/css/snippets/custom-radio.md new file mode 100644 index 000000000..ccf21ce25 --- /dev/null +++ b/css/snippets/custom-radio.md @@ -0,0 +1,83 @@ +--- +title: Custom radio button +type: snippet +tags: [visual,animation] +author: chalarangelo +cover: messy-computer +dateModified: 2022-11-16T05:00:00-04:00 +--- + +Creates a styled radio button with animation on state change. + +- Create a `.radio-container` and use flexbox to create the appropriate layout for the radio buttons. +- Reset the styles on the `` and use it to create the outline and background of the radio button. +- Use the `::before` element to create the inner circle of the radio button. +- Use `transform: scale(1)` and a CSS transition to create an animation effect on state change. + +```html +
    + + + + +
    +``` + +```css +.radio-container { + box-sizing: border-box; + background: #ffffff; + color: #222; + height: 64px; + display: flex; + justify-content: center; + align-items: center; + flex-flow: row wrap; +} + +.radio-container * { + box-sizing: border-box; +} + +.radio-input { + appearance: none; + background-color: #ffffff; + width: 16px; + height: 16px; + border: 1px solid #cccfdb; + margin: 0; + border-radius: 50%; + display: grid; + align-items: center; + justify-content: center; + transition: all 0.3s ease; +} + +.radio-input::before { + content: ""; + width: 6px; + height: 6px; + border-radius: 50%; + transform: scale(0); + transition: 0.3s transform ease-in-out; + box-shadow: inset 6px 6px #ffffff; +} + +.radio-input:checked { + background: #0077ff; + border-color: #0077ff; +} + +.radio-input:checked::before { + transform: scale(1); +} + +.radio { + cursor: pointer; + padding: 6px 8px; +} + +.radio:not(:last-child) { + margin-right: 6px; +} +``` diff --git a/css/snippets/custom-scrollbar.md b/css/snippets/custom-scrollbar.md new file mode 100644 index 000000000..f0f3f12b5 --- /dev/null +++ b/css/snippets/custom-scrollbar.md @@ -0,0 +1,46 @@ +--- +title: Custom scrollbar +type: snippet +tags: [visual] +cover: sea-view +dateModified: 2021-05-16T13:09:15+03:00 +--- + +Customizes the scrollbar style for elements with scrollable overflow. + +- Use `::-webkit-scrollbar` to style the scrollbar element. +- Use `::-webkit-scrollbar-track` to style the scrollbar track (the background of the scrollbar). +- Use `::-webkit-scrollbar-thumb` to style the scrollbar thumb (the draggable element). +- **Note:** Scrollbar styling doesn't appear to be on any standards track. This technique only works on WebKit-based browsers. + +```html +
    +

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

    +
    +``` + +```css +.custom-scrollbar { + height: 70px; + overflow-y: scroll; +} + +.custom-scrollbar::-webkit-scrollbar { + width: 8px; +} + +.custom-scrollbar::-webkit-scrollbar-track { + background: #1E3F20; + border-radius: 12px; +} + +.custom-scrollbar::-webkit-scrollbar-thumb { + background: #4A7856; + border-radius: 12px; +} +``` diff --git a/css/snippets/custom-text-selection.md b/css/snippets/custom-text-selection.md new file mode 100644 index 000000000..67d7440bb --- /dev/null +++ b/css/snippets/custom-text-selection.md @@ -0,0 +1,27 @@ +--- +title: Custom text selection +type: snippet +tags: [visual] +cover: digital-nomad +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Changes the styling of text selection. + +- Use the `::selection` pseudo-selector to style text within it when selected. + +```html +

    Select some of this text.

    +``` + +```css +::selection { + background: aquamarine; + color: black; +} + +.custom-text-selection::selection { + background: deeppink; + color: white; +} +``` diff --git a/css/snippets/disable-selection.md b/css/snippets/disable-selection.md new file mode 100644 index 000000000..02084424b --- /dev/null +++ b/css/snippets/disable-selection.md @@ -0,0 +1,23 @@ +--- +title: Disable selection +type: snippet +tags: [interactivity] +cover: interior-9 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Makes the content unselectable. + +- Use `user-select: none` to make the content of the element not selectable. +- **Note:** This is not a secure method to prevent users from copying content. + +```html +

    You can select me.

    +

    You can't select me!

    +``` + +```css +.unselectable { + user-select: none; +} +``` diff --git a/css/snippets/display-empty-links.md b/css/snippets/display-empty-links.md new file mode 100644 index 000000000..96a0be7dc --- /dev/null +++ b/css/snippets/display-empty-links.md @@ -0,0 +1,24 @@ +--- +title: Style links with no text +type: snippet +tags: [visual] +author: chalarangelo +cover: metro-tunnel +dateModified: 2022-11-11T05:00:00-04:00 +--- + +Displays the link URL for links with no text. + +- Use the `:empty` pseudo-class to select links with no text. +- Use the `:not` pseudo-class to exclude links with text. +- Use the `content` property and the `attr()` function to display the link URL in the `::before` pseudo-element. + +```html + +``` + +```css +a[href^="http"]:empty::before { + content: attr(href); +} +``` diff --git a/css/snippets/display-table-centering.md b/css/snippets/display-table-centering.md new file mode 100644 index 000000000..45275d4df --- /dev/null +++ b/css/snippets/display-table-centering.md @@ -0,0 +1,41 @@ +--- +title: Display table centering +type: snippet +tags: [layout] +cover: malibu +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Vertically and horizontally centers a child element within its parent element, using `display: table`. + +- Use `display: table` to make the `.center` element behave like a `` element. +- Set `height` and `width` to `100%` to make the element fill the available space within its parent element. +- Use `display: table-cell` on the child element to make it behave like a ``, `

    `, `

      `, etc. + +```html +
        +
      • Item 01
      • +
      • Item 02
      • +
      • Item 03
      • +
      • Item 04
      • +
      • Item 05
      • +
      +``` + +```css +li:nth-child(odd) { + background-color: #999; +} +``` diff --git a/css/snippets/zig-zag-pattern.md b/css/snippets/zig-zag-pattern.md new file mode 100644 index 000000000..b95eb0b23 --- /dev/null +++ b/css/snippets/zig-zag-pattern.md @@ -0,0 +1,33 @@ +--- +title: Zig zag background pattern +type: snippet +tags: [visual] +cover: blue-lake +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a zig zag background pattern. + +- Use `background-color` to set a white background. +- Use `background-image` with four `linear-gradient()` values to create the parts of a zig zag pattern. +- Use `background-size` to specify the pattern's size. Use `background-position` to place the parts of the pattern in the correct locations. +- **Note:** The fixed `height` and `width` of the element is for demonstration purposes only. + +```html +
      +``` + +```css +.zig-zag { + width: 240px; + height: 240px; + background-color: #fff; + background-image: linear-gradient(135deg, #000 25%, transparent 25%), + linear-gradient(225deg, #000 25%, transparent 25%), + linear-gradient(315deg, #000 25%, transparent 25%), + linear-gradient(45deg, #000 25%, transparent 25%); + background-position: -30px 0, -30px 0, 0 0, 0 0; + background-size: 60px 60px; + background-repeat: repeat; +} +``` diff --git a/css/snippets/zoomin-zoomout-animation.md b/css/snippets/zoomin-zoomout-animation.md new file mode 100644 index 000000000..27b204732 --- /dev/null +++ b/css/snippets/zoomin-zoomout-animation.md @@ -0,0 +1,39 @@ +--- +title: Zoom in zoom out animation +type: snippet +tags: [animation] +cover: travel-mug-2 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a zoom in zoom out animation. + +- Use `@keyframes` to define a three-step animation. At the start (`0%`) and end (`100%`), the element is its original size (`scale(1 ,1)`). Halfway through (`50%`) it's scaled up to 1.5 times its original size (`scale(1.5, 1.5)`). +- Use `width` and `height` to give the element a specific size. +- Use `animation` to set the appropriate values for the element to make it animated. + +```html +
      +``` + +```css +.zoom-in-out-box { + margin: 24px; + width: 50px; + height: 50px; + background: #f50057; + animation: zoom-in-zoom-out 1s ease infinite; +} + +@keyframes zoom-in-zoom-out { + 0% { + transform: scale(1, 1); + } + 50% { + transform: scale(1.5, 1.5); + } + 100% { + transform: scale(1, 1); + } +} +```
    ` elements. +- Use `text-align: center` and `vertical-align: middle` on the child element to center it horizontally and vertically. +- The outer parent (`.container`) must have a fixed `width` and `height`. + +```html +
    +
    Centered content
    +
    +``` + +```css +.container { + border: 1px solid #9C27B0; + height: 250px; + width: 250px; +} + +.center { + display: table; + height: 100%; + width: 100%; +} + +.center > span { + display: table-cell; + text-align: center; + vertical-align: middle; +} +``` diff --git a/css/snippets/donut-spinner.md b/css/snippets/donut-spinner.md new file mode 100644 index 000000000..175fe3b6e --- /dev/null +++ b/css/snippets/donut-spinner.md @@ -0,0 +1,37 @@ +--- +title: Donut spinner +type: snippet +tags: [animation] +cover: digital-nomad-3 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a donut spinner that can be used to indicate the loading of content. + +- Use a semi-transparent `border` for the whole element. Exclude one side that will serve as the loading indicator for the donut. +- Define and use an appropriate animation, using `transform: rotate()` to rotate the element. + +```html +
    +``` + +```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; +} +``` diff --git a/css/snippets/drop-cap.md b/css/snippets/drop-cap.md new file mode 100644 index 000000000..a5abaa947 --- /dev/null +++ b/css/snippets/drop-cap.md @@ -0,0 +1,28 @@ +--- +title: Drop cap +type: snippet +tags: [visual] +cover: bamboo-lamp +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Makes the first letter of the first paragraph bigger than the rest of the text. + +- Use the `:first-child` selector to select only the first paragraph. +- Use the `::first-letter` pseudo-element to style the first element of the paragraph. + +```html +

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam commodo ligula quis tincidunt cursus. Integer consectetur tempor ex eget hendrerit. Cras facilisis sodales odio nec maximus. Pellentesque lacinia convallis libero, rhoncus tincidunt ante dictum at. Nullam facilisis lectus tellus, sit amet congue erat sodales commodo.

    +

    Donec magna erat, imperdiet non odio sed, sodales tempus magna. Integer vitae orci lectus. Nullam consectetur orci at pellentesque efficitur.

    +``` + +```css +p:first-child::first-letter { + color: #5f79ff; + float: left; + margin: 0 8px 0 4px; + font-size: 3rem; + font-weight: bold; + line-height: 1; +} +``` diff --git a/css/snippets/dynamic-shadow.md b/css/snippets/dynamic-shadow.md new file mode 100644 index 000000000..a4ade87f7 --- /dev/null +++ b/css/snippets/dynamic-shadow.md @@ -0,0 +1,40 @@ +--- +title: Dynamic shadow +type: snippet +tags: [visual] +cover: couch-laptop +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a shadow similar to `box-shadow` but based on the colors of the element itself. + +- Use the `::after` pseudo-element with `position: absolute` and `width` and `height` equal to `100%` to fill the available space in the parent element. +- Use `background: inherit` to inherit the `background` of the parent element. +- Use `top` to slightly offset the pseudo-element, `filter: blur()` to create a shadow and `opacity` to make it semi-transparent. +- Use `z-index: 1` on the parent and `z-index: -1` on the pseudo-element to position it behind its parent. + +```html +
    +``` + +```css +.dynamic-shadow { + position: relative; + width: 10rem; + height: 10rem; + background: linear-gradient(75deg, #6d78ff, #00ffb8); + z-index: 1; +} + +.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; +} +``` diff --git a/css/snippets/etched-text.md b/css/snippets/etched-text.md new file mode 100644 index 000000000..639766083 --- /dev/null +++ b/css/snippets/etched-text.md @@ -0,0 +1,26 @@ +--- +title: Etched text +type: snippet +tags: [visual] +cover: memories-of-pineapple-2 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates an effect where text appears to be "etched" or engraved into the background. + +- Use `text-shadow` to create 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. + +```html +

    I appear etched into the background.

    +``` + +```css +.etched-text { + text-shadow: 0 2px white; + font-size: 1.5rem; + font-weight: bold; + color: #b8bec5; +} +``` diff --git a/css/snippets/evenly-distributed-children.md b/css/snippets/evenly-distributed-children.md new file mode 100644 index 000000000..41852ad62 --- /dev/null +++ b/css/snippets/evenly-distributed-children.md @@ -0,0 +1,28 @@ +--- +title: Evenly distributed children +type: snippet +tags: [layout] +cover: little-bird +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Evenly distributes child elements within a parent element. + +- Use `display: flex` to use the flexbox layout. +- Use `justify-content: space-between` to 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, instead of between them. + +```html +
    +

    Item1

    +

    Item2

    +

    Item3

    +
    +``` + +```css +.evenly-distributed-children { + display: flex; + justify-content: space-between; +} +``` diff --git a/css/snippets/fit-image-in-container.md b/css/snippets/fit-image-in-container.md new file mode 100644 index 000000000..ce81c131b --- /dev/null +++ b/css/snippets/fit-image-in-container.md @@ -0,0 +1,37 @@ +--- +title: Fit image in container +type: snippet +tags: [layout,visual] +cover: succulent-3 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Fits an positions an image appropriately inside its container while preserving its aspect ratio. + +- Use `object-fit: contain` to fit the entire image within the container while preserving its aspect ratio. +- Use `object-fit: cover` to fill the container with the image while preserving its aspect ratio. +- Use `object-position: center` to position the image at the center of the container. + +```html + + +``` + +```css +.image { + background: #34495e; + border: 1px solid #34495e; + width: 200px; + height: 200px; +} + +.image-contain { + object-fit: contain; + object-position: center; +} + +.image-cover { + object-fit: cover; + object-position: right top; +} +``` diff --git a/css/snippets/flexbox-centering.md b/css/snippets/flexbox-centering.md new file mode 100644 index 000000000..d5c140cad --- /dev/null +++ b/css/snippets/flexbox-centering.md @@ -0,0 +1,28 @@ +--- +title: Flexbox centering +type: snippet +tags: [layout] +cover: basket-paper +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Horizontally and vertically centers a child element within a parent element using flexbox. + +- Use `display: flex` to create a flexbox layout. +- Use `justify-content: center` to center the child horizontally. +- Use `align-items: center` to center the child vertically. + +```html +
    +
    Centered content.
    +
    +``` + +```css +.flexbox-centering { + display: flex; + justify-content: center; + align-items: center; + height: 100px; +} +``` diff --git a/css/snippets/floating-list-titles.md b/css/snippets/floating-list-titles.md new file mode 100644 index 000000000..4ff22ff78 --- /dev/null +++ b/css/snippets/floating-list-titles.md @@ -0,0 +1,105 @@ +--- +title: List with floating section headings +type: snippet +tags: [visual] +author: chalarangelo +cover: pop-of-green +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a list with floating headings for each section. + +- Use `overflow-y: auto` to allow the list container to overflow vertically. +- Use `display: grid` on the inner container (`
    `) to create a layout with two columns. +- Set headings (`
    `) to `grid-column: 1` and content (`
    `) to `grid-column: 2`. +- Finally, apply `position: sticky` and `top: 0.5rem` to headings to create a floating effect. + +```html +
    +
    +
    +
    A
    +
    Algeria
    +
    Angola
    + +
    B
    +
    Benin
    +
    Botswana
    +
    Burkina Faso
    +
    Burundi
    + +
    C
    +
    Cabo Verde
    +
    Cameroon
    +
    Central African Republic
    +
    Chad
    +
    Comoros
    +
    Congo, Democratic Republic of the
    +
    Congo, Republic of the
    +
    Cote d'Ivoire
    + +
    D
    +
    Djibouti
    + +
    E
    +
    Egypt
    +
    Equatorial Guinea
    +
    Eritrea
    +
    Eswatini (formerly Swaziland)
    +
    Ethiopia
    +
    +
    +
    +``` + +```css +.container { + display: grid; + place-items: center; + min-height: 400px; +} + +.floating-stack { + background: #455A64; + color: #fff; + height: 80vh; + width: 320px; + border-radius: 1rem; + overflow-y: auto; +} + +.floating-stack > dl { + margin: 0 0 1rem; + display: grid; + grid-template-columns: 2.5rem 1fr; + align-items: center; +} + +.floating-stack dt { + position: sticky; + top: 0.5rem; + left: 0.5rem; + font-weight: bold; + background: #263238; + color: #cfd8dc; + height: 2rem; + width: 2rem; + border-radius: 50%; + padding: 0.25rem 1rem; + grid-column: 1; + display: inline-flex; + align-items: center; + justify-content: center; + box-sizing: border-box; +} + +.floating-stack dd { + grid-column: 2; + margin: 0; + padding: 0.75rem; +} + +.floating-stack > dl:first-of-type > dd:first-of-type { + margin-top: 0.25rem; +} +``` diff --git a/css/snippets/fluid-typography.md b/css/snippets/fluid-typography.md new file mode 100644 index 000000000..0eb94e936 --- /dev/null +++ b/css/snippets/fluid-typography.md @@ -0,0 +1,23 @@ +--- +title: Fluid typography +type: snippet +tags: [visual] +author: chalarangelo +cover: shell-focus +dateModified: 2021-05-16T11:23:05+03:00 +--- + +Creates text that scales according to the viewport width. + +- Use the `clamp()` CSS function to clamp the value of `font-size` between `1rem` and `3rem`. +- Use the formula `8vw - 2rem` to calculate a responsive value for `font-size` (`1rem` at `600px`, `3rem` at `1000px`). + +```html +

    Hello World!

    +``` + +```css +.fluid-type { + font-size: clamp(1rem, 8vw - 2rem, 3rem); +} +``` diff --git a/css/snippets/focus-within.md b/css/snippets/focus-within.md new file mode 100644 index 000000000..a8d9fcc82 --- /dev/null +++ b/css/snippets/focus-within.md @@ -0,0 +1,42 @@ +--- +title: Focus Within +type: snippet +tags: [visual,interactivity] +cover: boats +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Changes the appearance of a form if any of its children are focused. + +- Use the pseudo-class `:focus-within` to apply styles to a parent element if any child element gets focused. + +```html +
    + + +
    + + +
    +``` + +```css +form { + border: 2px solid #52B882; + padding: 8px; + border-radius: 2px; +} + +form:focus-within { + background: #7CF0BD; +} + +label { + display: inline-block; + width: 72px; +} + +input { + margin: 4px 12px; +} +``` diff --git a/css/snippets/full-width.md b/css/snippets/full-width.md new file mode 100644 index 000000000..a1a3aaf82 --- /dev/null +++ b/css/snippets/full-width.md @@ -0,0 +1,58 @@ +--- +title: Full-width image +type: snippet +tags: [layout] +author: chalarangelo +cover: yellow-white-mug-2 +dateModified: 2021-01-07T10:14:46+02:00 +--- + +Creates a full-width image. + +- Use `left: 50%` and `right: 50%` to position the image in the middle of the parent element. +- Use `margin-left: -50vw` and `margin-right: -50vw` to offset the image on both sides relative to the size of the viewport. +- Use `width: 100vw` and `max-width: 100vw` to size the image relative to the viewport. + +```html +
    +

    Lorem ipsum dolor sit amet

    +

    + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris molestie + lobortis sapien, sit amet iaculis est interdum tincidunt. Nunc egestas nibh + ut metus elementum consequat. Integer elit orci, rhoncus efficitur lectus + eu, faucibus interdum felis. +

    +

    + +

    +

    + Orci varius natoque penatibus et magnis dis parturient montes, nascetur + ridiculus mus. Nullam pretium lectus sed ex efficitur, ac varius sapien + gravida. Sed facilisis elit quis sem sollicitudin, ut aliquam neque + eleifend. Maecenas sagittis neque sapien, ac tempus nulla tempus nec. + Curabitur tellus est, convallis id dolor ut, porta hendrerit quam. +

    +
    +``` + +```css +main { + margin: 0 auto; + max-width: 640px; +} + +img { + height: auto; + max-width: 100%; +} + +.full-width { + position: relative; + left: 50%; + right: 50%; + margin-left: -50vw; + margin-right: -50vw; + max-width: 100vw; + width: 100vw; +} +``` diff --git a/css/snippets/fullscreen.md b/css/snippets/fullscreen.md new file mode 100644 index 000000000..3538ebac5 --- /dev/null +++ b/css/snippets/fullscreen.md @@ -0,0 +1,54 @@ +--- +title: Fullscreen +type: snippet +tags: [visual] +cover: flower-portrait-3 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Applies styles to an element when in fullscreen mode. + +- Use the `:fullscreen` CSS pseudo-element selector to select and style an element that is displayed in fullscreen mode. +- Use a ` + +``` + +```css +.container { + margin: 40px auto; + max-width: 700px; +} + +.element { + padding: 20px; + height: 300px; + width: 100%; + background-color: skyblue; + box-sizing: border-box; +} + +.element p { + text-align: center; + color: white; + font-size: 3em; +} + +.element:-ms-fullscreen p { + visibility: visible; +} + +.element:fullscreen { + background-color: #e4708a; + width: 100vw; + height: 100vh; +} +``` diff --git a/css/snippets/gradient-text.md b/css/snippets/gradient-text.md new file mode 100644 index 000000000..a87ac5706 --- /dev/null +++ b/css/snippets/gradient-text.md @@ -0,0 +1,26 @@ +--- +title: Gradient text +type: snippet +tags: [visual] +cover: red-berries +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Gives text a gradient color. + +- Use `background` with a `linear-gradient()` value to give the text element a gradient background. +- Use `webkit-text-fill-color: transparent` to fill the text with a transparent color. +- Use `webkit-background-clip: text` to clip the background with the text, filling the text with the gradient background as the color. + +```html +

    Gradient text

    +``` + +```css +.gradient-text { + background: linear-gradient(#70D6FF, #00072D); + -webkit-text-fill-color: transparent; + -webkit-background-clip: text; + font-size: 32px; +} +``` diff --git a/css/snippets/grid-centering.md b/css/snippets/grid-centering.md new file mode 100644 index 000000000..5e18ea077 --- /dev/null +++ b/css/snippets/grid-centering.md @@ -0,0 +1,28 @@ +--- +title: Grid centering +type: snippet +tags: [layout] +cover: work-hard-computer +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Horizontally and vertically centers a child element within a parent element using `grid`. + +- Use `display: grid` to create a grid layout. +- Use `justify-content: center` to center the child horizontally. +- Use `align-items: center` to center the child vertically. + +```html +
    +
    Centered content.
    +
    +``` + +```css +.grid-centering { + display: grid; + justify-content: center; + align-items: center; + height: 100px; +} +``` diff --git a/css/snippets/hamburger-button.md b/css/snippets/hamburger-button.md new file mode 100644 index 000000000..16734bf65 --- /dev/null +++ b/css/snippets/hamburger-button.md @@ -0,0 +1,55 @@ +--- +title: Hamburger Button +type: snippet +tags: [interactivity] +cover: volcano-sunset +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Displays a hamburger menu which transitions to a cross button on hover. + +- Use a `.hamburger-menu` container `div` which contains the top, bottom, and middle bars. +- Set the container to `display: flex` with `flex-flow: column wrap`. +- Add distance between the bars using `justify-content: space-between`. +- Use `transform: rotate()` to rotate the top and bottom bars by 45 degrees and `opacity: 0` to fade the middle bar on hover. +- Use `transform-origin: left` so that the bars rotate around the left point. + +```html +
    +
    +
    +
    +
    +``` + +```css +.hamburger-menu { + display: flex; + flex-flow: column wrap; + justify-content: space-between; + height: 2.5rem; + width: 2.5rem; + cursor: pointer; +} + +.hamburger-menu .bar { + height: 5px; + background: black; + border-radius: 5px; + margin: 3px 0px; + transform-origin: left; + transition: all 0.5s; +} + +.hamburger-menu:hover .top { + transform: rotate(45deg); +} + +.hamburger-menu:hover .middle { + opacity: 0; +} + +.hamburger-menu:hover .bottom { + transform: rotate(-45deg); +} +``` diff --git a/css/snippets/height-transition.md b/css/snippets/height-transition.md new file mode 100644 index 000000000..e06ff5bb8 --- /dev/null +++ b/css/snippets/height-transition.md @@ -0,0 +1,41 @@ +--- +title: Height transition +type: snippet +tags: [animation] +cover: washed-ashore +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Transitions an element's height from `0` to `auto` when its height is unknown. + +- Use `transition` to specify that changes to `max-height` should be transitioned over. +- Use `overflow: hidden` to prevent the contents of the hidden element from overflowing its container. +- Use `max-height` to specify an initial height of `0`. +- Use the `:hover` pseudo-class to change the `max-height` to the value of the `--max-height` variable set by JavaScript. +- Use `Element.scrollHeight` and `CSSStyleDeclaration.setProperty()` to set the value of `--max-height` to the current height of the element. +- **Note:** Causes reflow on each animation frame, which will be laggy if there are a large number of elements beneath the element that is transitioning in height. + +```html +
    + Hover me to see a height transition. +
    Additional content
    +
    +``` + +```css +.el { + transition: max-height 0.3s; + overflow: hidden; + max-height: 0; +} + +.trigger:hover > .el { + max-height: var(--max-height); +} +``` + +```js +let el = document.querySelector('.el'); +let height = el.scrollHeight; +el.style.setProperty('--max-height', height + 'px'); +``` diff --git a/css/snippets/hide-empty-elements.md b/css/snippets/hide-empty-elements.md new file mode 100644 index 000000000..124dfae10 --- /dev/null +++ b/css/snippets/hide-empty-elements.md @@ -0,0 +1,22 @@ +--- +title: Hide empty elements +type: snippet +tags: [visual] +author: chalarangelo +cover: metro-arrival +dateModified: 2022-11-18T05:00:00-04:00 +--- + +Hides elements with no content. + +- Use the `:empty` pseudo-class to select elements with no content. + +```html +

    Lorem ipsum dolor sit amet.

    +``` + +```css +:empty { + display: none; +} +``` diff --git a/css/snippets/hide-scrollbars.md b/css/snippets/hide-scrollbars.md new file mode 100644 index 000000000..3a7a35cc3 --- /dev/null +++ b/css/snippets/hide-scrollbars.md @@ -0,0 +1,36 @@ +--- +title: Hide scroll bars +type: snippet +tags: [visual] +author: chalarangelo +cover: by-the-lighthouse +dateModified: 2022-05-13T05:00:00-04:00 +--- + +Hides scrollbars on an element, while still allowing it to be scrollable. + +- Use `overflow: auto` to allow the element to be scrollable. +- Use `scrollbar-width: none` to hide scrollbars on Firefox. +- Use `display: none` on the `::-webkit-scrollbar` pseudo-element to hide scrollbars on WebKit browsers (Chrome, Edge, Safari). + +```html +
    +

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean interdum id leo a consectetur. Integer justo magna, ultricies vel enim vitae, egestas efficitur leo. Ut nulla orci, rutrum eu augue sed, tempus pellentesque quam.

    +
    +``` + +```css +div { + width: 200px; + height: 100px; +} + +.no-scrollbars { + overflow: auto; + scrollbar-width: none; +} + +.no-scrollbars::-webkit-scrollbar { + display: none; +} +``` diff --git a/css/snippets/horizontal-gallery.md b/css/snippets/horizontal-gallery.md new file mode 100644 index 000000000..33a94bd27 --- /dev/null +++ b/css/snippets/horizontal-gallery.md @@ -0,0 +1,124 @@ +--- +title: Image gallery with horizontal scroll +type: snippet +tags: [visual,interactivity] +cover: flower-portrait-5 +dateModified: 2022-05-01T05:00:00-04:00 +--- + +Creates a horizontally scrollable image gallery. + +- Position the `.thumbnails` at the bottom of the container using `position: absolute`. +- Use `scroll-snap-type: x mandatory` and `overscroll-behavior-x: contain` to create a snap effect on horizontal scroll. Snap elements to the start of the container using `scroll-snap-align: start`. +- Hide scrollbars using `scrollbar-width: none` and styling the pseudo-element `::-webkit-scrollbar` to `display: none`. +- Use `Element.scrollTo()` to define a `scrollToElement` function, that scrolls the gallery to the given item. +- Use `Array.prototype.map()` and `Array.prototype.join()` to populate the `.thumbnails` element. Give each thumbnail a `data-id` attribute with the index of the image. +- Use `Document.querySelectorAll()` to get all the thumbnail elements. Use `Array.prototype.forEach()` to register a handler for the `'click'` event on each thumbnail, using `EventTarget.addEventListener()` and the `scrollToElement` function. +- Use `Document.querySelector()` and `EventTarget.addEventListener()` to register a handler for the `'scroll'` event. Update the `.thumbnails` element to match the current scroll position, using the `highlightThumbnail` function. + +```html + +``` + +```css +.gallery-container { + position: relative; + display: flex; + justify-content: center; +} + +.thumbnails { + position: absolute; + bottom: 8px; + display: flex; + flex-direction: row; + gap: 6px; +} + +.thumbnails div { + width: 8px; + height: 8px; + cursor: pointer; + background: #aaa; + border-radius: 100%; +} + +.thumbnails div.highlighted { + background-color: #777; +} + +.slides { + margin: 0 16px; + display: grid; + grid-auto-flow: column; + gap: 1rem; + width: 540px; + padding: 0 0.25rem; + height: 720px; + overflow-y: auto; + overscroll-behavior-x: contain; + scroll-snap-type: x mandatory; + scrollbar-width: none; +} + +.slides > div { + scroll-snap-align: start; +} + +.slides img { + width: 540px; + object-fit: contain; +} + +.slides::-webkit-scrollbar { + display: none; +} +``` + +```js +const slideGallery = document.querySelector('.slides'); +const slides = slideGallery.querySelectorAll('div'); +const thumbnailContainer = document.querySelector('.thumbnails'); +const slideCount = slides.length; +const slideWidth = 540; + +const highlightThumbnail = () => { + thumbnailContainer + .querySelectorAll('div.highlighted') + .forEach(el => el.classList.remove('highlighted')); + const index = Math.floor(slideGallery.scrollLeft / slideWidth); + thumbnailContainer + .querySelector(`div[data-id="${index}"]`) + .classList.add('highlighted'); +}; + +const scrollToElement = el => { + const index = parseInt(el.dataset.id, 10); + slideGallery.scrollTo(index * slideWidth, 0); +}; + +thumbnailContainer.innerHTML += [...slides] + .map((slide, i) => `
    `) + .join(''); + +thumbnailContainer.querySelectorAll('div').forEach(el => { + el.addEventListener('click', () => scrollToElement(el)); +}); + +slideGallery.addEventListener('scroll', e => highlightThumbnail()); + +highlightThumbnail(); +``` diff --git a/css/snippets/horizontal-scroll-snap.md b/css/snippets/horizontal-scroll-snap.md new file mode 100644 index 000000000..a98d54509 --- /dev/null +++ b/css/snippets/horizontal-scroll-snap.md @@ -0,0 +1,53 @@ +--- +title: Horizontal scroll snap +type: snippet +tags: [interactivity] +cover: waves-from-above +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a horizontally scrollable container that will snap on elements when scrolling. + +- Use `display: grid` and `grid-auto-flow: column` to create a horizontal layout. +- Use `scroll-snap-type: x mandatory` and `overscroll-behavior-x: contain` to create a snap effect on horizontal scroll. +- Change `scroll-snap-align` to either `start`, `stop` or `center` to change the snap alignment. + +```html +
    + + + + + + + + + +
    +``` + +```css +.horizontal-snap { + margin: 0 auto; + display: grid; + grid-auto-flow: column; + gap: 1rem; + height: calc(180px + 1rem); + padding: 1rem; + max-width: 480px; + overflow-y: auto; + overscroll-behavior-x: contain; + scroll-snap-type: x mandatory; +} + +.horizontal-snap > a { + scroll-snap-align: center; +} + +.horizontal-snap img { + width: 180px; + max-width: none; + object-fit: contain; + border-radius: 1rem; +} +``` diff --git a/css/snippets/hover-additional-content.md b/css/snippets/hover-additional-content.md new file mode 100644 index 000000000..b9d76e1b1 --- /dev/null +++ b/css/snippets/hover-additional-content.md @@ -0,0 +1,75 @@ +--- +title: Show additional content on hover +type: snippet +tags: [visual] +cover: dark-leaves-5 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a card that displays additional content on hover. + +- Use `overflow: hidden` on the card to hide elements that overflow vertically. +- Use the `:hover` and `:focus-within` pseudo-class selectors to change the card's styling if the element is hovered, focused or any of its descendants are focused. +- Set `transition: 0.3s ease all` to create a transition effect on hover/focus. + + +```html +
    + +

    Lorem ipsum

    +
    +

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Link to source +

    +
    +
    +``` + +```css +.card { + width: 300px; + height: 280px; + padding: 0; + box-shadow: 0 2px 4px 0 rgba(0,0,0,0.1); + border-radius: 8px; + box-sizing: border-box; + overflow: hidden; +} + +.card * { + transition: 0.3s ease all; +} + +.card img { + margin: 0; + width: 300px; + height: 224px; + object-fit: cover; + display: block; +} + +.card h3 { + margin: 0; + padding: 12px 12px 48px; + line-height: 32px; + font-weight: 500; + font-size: 20px; +} + +.card .focus-content { + display: block; + padding: 8px 12px; +} + +.card p { + margin: 0; + line-height: 1.5; +} + +.card:hover img, .card:focus-within img { + margin-top: -80px; +} + +.card:hover h3, .card:focus-within h3 { + padding: 8px 12px 0; +} +``` diff --git a/css/snippets/hover-perspective.md b/css/snippets/hover-perspective.md new file mode 100644 index 000000000..e79c86014 --- /dev/null +++ b/css/snippets/hover-perspective.md @@ -0,0 +1,53 @@ +--- +title: Perspective transform on hover +type: snippet +tags: [animation] +author: chalarangelo +cover: tropical-bike +dateModified: 2021-05-17T13:58:04+03:00 +--- + +Applies a perspective transform with a hover animation to an element. + +- Use `transform` with the `perspective()` and `rotateY()` functions to create a perspective for the element. +- Use a `transition` to update the `transform` attribute's value on hover. +- Change the `rotateY()` value to negative to mirror the perspective effect from left to right. + +```html +
    +
    +
    +
    +``` + +```css +.image-card { + display: inline-block; + box-sizing: border-box; + margin: 1rem; + width: 240px; + height: 320px; + padding: 8px; + border-radius: 1rem; + background: url("https://picsum.photos/id/1049/240/320"); + box-shadow: rgba(0, 0, 0, 0.25) 0px 25px 50px -12px; +} + +.perspective-left { + transform: perspective(1500px) rotateY(15deg); + transition: transform 1s ease 0s; +} + +.perspective-left:hover { + transform: perspective(3000px) rotateY(5deg); +} + +.perspective-right { + transform: perspective(1500px) rotateY(-15deg); + transition: transform 1s ease 0s; +} + +.perspective-right:hover { + transform: perspective(3000px) rotateY(-5deg); +} +``` diff --git a/css/snippets/hover-shadow-box-animation.md b/css/snippets/hover-shadow-box-animation.md new file mode 100644 index 000000000..28461ec3b --- /dev/null +++ b/css/snippets/hover-shadow-box-animation.md @@ -0,0 +1,38 @@ +--- +title: Hover shadow box animation +type: snippet +tags: [animation] +unlisted: true +cover: dark-cloud +dateModified: 2021-01-04T12:30:40+02:00 +--- + +Creates a shadow box around the text when it is hovered. + +- 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. +- Use `box-shadow` to make the box transparent. +- Use `transition-property` to enable transitions for both `box-shadow` and `transform`. +- Use the `:hover`, `:active` and `:focus` pseudo-class selectors to apply a new `box-shadow` and `transform: scale(1.2)` to change the scale of the text. + +```html +

    Box it!

    +``` + +```css +.hover-shadow-box-animation { + display: inline-block; + vertical-align: middle; + transform: perspective(1px) translateZ(0); + box-shadow: 0 0 1px transparent; + margin: 10px; + transition-duration: 0.3s; + transition-property: box-shadow, transform; +} + +.hover-shadow-box-animation:hover, +.hover-shadow-box-animation:focus, +.hover-shadow-box-animation:active { + box-shadow: 1px 10px 10px -10px rgba(0, 0, 24, 0.5); + transform: scale(1.2); +} +``` diff --git a/css/snippets/hover-underline-animation.md b/css/snippets/hover-underline-animation.md new file mode 100644 index 000000000..ce1c07671 --- /dev/null +++ b/css/snippets/hover-underline-animation.md @@ -0,0 +1,46 @@ +--- +title: Hover underline animation +type: snippet +tags: [animation] +cover: coffee-phone-tray-2 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates an animated underline effect when the user hovers over the text. + +- Use `display: inline-block` to make the underline span just the width of the text content. +- Use the `::after` pseudo-element with `width: 100%` and `position: absolute` to place it below the content. +- Use `transform: scaleX(0)` to initially hide the pseudo-element. +- Use the `:hover` pseudo-class selector to apply `transform: scaleX(1)` and display the pseudo-element on hover. +- Animate `transform` using `transform-origin: left` and an appropriate `transition`. +- Remove the `transform-origin` property to make the transform originate from the center of the element. + +```html +

    Hover this text to see the effect!

    +``` + +```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.25s ease-out; +} + +.hover-underline-animation:hover::after { + transform: scaleX(1); + transform-origin: bottom left; +} +``` diff --git a/css/snippets/image-hover-menu.md b/css/snippets/image-hover-menu.md new file mode 100644 index 000000000..e550c0468 --- /dev/null +++ b/css/snippets/image-hover-menu.md @@ -0,0 +1,92 @@ +--- +title: Menu on image hover +type: snippet +tags: [layout,animation] +cover: compass +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Displays a menu overlay when the user hovers over the image. + +- Use a `
    ` to wrap the `` element and a `
    ` element that will contain the menu links. +- Use the `opacity` and `right` attributes to animate the image on hover, creating a sliding effect. +- Set the `left` attribute of the `
    ` to the negative of the element's `width`. Reset it to `0` when hovering over the parent element to slide in the menu. +- Use `display: flex`, `flex-direction: column` and `justify-content: center` on the `
    ` to vertically center the menu items. + +```html +
    + +
    + Home + Pricing + About +
    +
    +``` + +```css +.hover-menu { + position: relative; + overflow: hidden; + margin: 8px; + min-width: 340px; + max-width: 480px; + max-height: 290px; + width: 100%; + background: #000; + text-align: center; + box-sizing: border-box; +} + +.hover-menu * { + box-sizing: border-box; +} + +.hover-menu img { + position: relative; + max-width: 100%; + top: 0; + right: 0; + opacity: 1; + transition: 0.3s ease-in-out; +} + +.hover-menu div { + position: absolute; + top: 0; + left: -120px; + width: 120px; + height: 100%; + padding: 8px 4px; + background: #000; + transition: 0.3s ease-in-out; + display: flex; + flex-direction: column; + justify-content: center; +} + +.hover-menu div a { + display: block; + line-height: 2; + color: #fff; + text-decoration: none; + opacity: 0.8; + padding: 5px 15px; + position: relative; + transition: 0.3s ease-in-out; +} + +.hover-menu div a:hover { + text-decoration: underline; +} + +.hover-menu:hover img { + opacity: 0.5; + right: -120px; +} + +.hover-menu:hover div { + left: 0; + opacity: 1; +} +``` diff --git a/css/snippets/image-hover-rotate.md b/css/snippets/image-hover-rotate.md new file mode 100644 index 000000000..904414c34 --- /dev/null +++ b/css/snippets/image-hover-rotate.md @@ -0,0 +1,38 @@ +--- +title: Image rotate on hover +type: snippet +tags: [animation,visual] +cover: succulent-1 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a rotate effect for the image on hover. + +- Use the `scale()`, `rotate()` and `transition` properties when hovering over the parent element (a `
    `) to animate the image. +- Use `overflow: hidden` on the parent element to hide the excess from the image transformation. + +```html +
    + +
    +``` + +```css +.hover-rotate { + overflow: hidden; + margin: 8px; + min-width: 240px; + max-width: 320px; + width: 100%; +} + +.hover-rotate img { + transition: all 0.3s; + box-sizing: border-box; + max-width: 100%; +} + +.hover-rotate:hover img { + transform: scale(1.3) rotate(5deg); +} +``` diff --git a/css/snippets/image-mosaic.md b/css/snippets/image-mosaic.md new file mode 100644 index 000000000..a12f8676d --- /dev/null +++ b/css/snippets/image-mosaic.md @@ -0,0 +1,107 @@ +--- +title: Responsive image mosaic +type: snippet +tags: [layout] +author: chalarangelo +cover: beach-riders +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a responsive image mosaic. + +- Use `display: grid` to create an appropriate responsive grid layout. +- Use `grid-row: span 2 / auto` and `grid-column: span 2 / auto` to create items that span two rows or two columns respectively. +- Wrap the previous styles into a media query to avoid applying on small screen sizes. + +```html +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +``` + +```css +.image-mosaic { + display: grid; + gap: 1rem; + grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); + grid-auto-rows: 240px; +} + +.card { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + background: #353535; + font-size: 3rem; + color: #fff; + box-shadow: rgba(3, 8, 20, 0.1) 0px 0.15rem 0.5rem, rgba(2, 8, 20, 0.1) 0px 0.075rem 0.175rem; + height: 100%; + width: 100%; + border-radius: 4px; + transition: all 500ms; + overflow: hidden; + background-size: cover; + background-position: center; + background-repeat: no-repeat; + padding: 0; + margin: 0; +} + +@media screen and (min-width: 600px) { + .card-tall { + grid-row: span 2 / auto; + } + + .card-wide { + grid-column: span 2 / auto; + } +} +``` diff --git a/css/snippets/image-overlay-hover.md b/css/snippets/image-overlay-hover.md new file mode 100644 index 000000000..4d1eaa0e3 --- /dev/null +++ b/css/snippets/image-overlay-hover.md @@ -0,0 +1,101 @@ +--- +title: Image overlay on hover +type: snippet +tags: [visual,animation] +cover: architectural +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Displays an image overlay effect on hover. + +- Use the `::before` and `::after` pseudo-elements for the top and bottom bars of the overlay respectively. Set their `opacity`, `transform` and `transition` to produce the desired effect. +- Use the `
    ` for the text of the overlay. Set `display: flex`, `flex-direction: column` and `justify-content: center` to center the text into the image. +- Use the `:hover` pseudo-selector to update the `opacity` and `transform` of all the elements and display the overlay. + +```html +
    + +
    +

    Lorem
    Ipsum

    +
    +
    +``` + +```css +.hover-img { + background-color: #000; + color: #fff; + display: inline-block; + margin: 8px; + max-width: 320px; + min-width: 240px; + overflow: hidden; + position: relative; + text-align: center; + width: 100%; +} + +.hover-img * { + box-sizing: border-box; + transition: all 0.45s ease; +} + +.hover-img::before, +.hover-img::after { + background-color: rgba(0, 0, 0, 0.5); + border-top: 32px solid rgba(0, 0, 0, 0.5); + border-bottom: 32px solid rgba(0, 0, 0, 0.5); + position: absolute; + top: 0; + bottom: 0; + left: 0; + right: 0; + content: ''; + transition: all 0.3s ease; + z-index: 1; + opacity: 0; + transform: scaleY(2); +} + +.hover-img img { + vertical-align: top; + max-width: 100%; + backface-visibility: hidden; +} + +.hover-img figcaption { + position: absolute; + top: 0; + bottom: 0; + left: 0; + right: 0; + align-items: center; + z-index: 1; + display: flex; + flex-direction: column; + justify-content: center; + line-height: 1.1em; + opacity: 0; + z-index: 2; + transition-delay: 0.1s; + font-size: 24px; + font-family: sans-serif; + font-weight: 400; + letter-spacing: 1px; + text-transform: uppercase; +} + +.hover-img:hover::before, +.hover-img:hover::after { + transform: scale(1); + opacity: 1; +} + +.hover-img:hover > img { + opacity: 0.7; +} + +.hover-img:hover figcaption { + opacity: 1; +} +``` diff --git a/css/snippets/image-text-overlay.md b/css/snippets/image-text-overlay.md new file mode 100644 index 000000000..e48f3cce1 --- /dev/null +++ b/css/snippets/image-text-overlay.md @@ -0,0 +1,56 @@ +--- +title: Image with text overlay +type: snippet +tags: [visual] +author: chalarangelo +cover: icebreaker +dateModified: 2023-01-29T05:00:00-04:00 +--- + +Displays an image with a text overlay. + +- Use the `
    ` and `
    ` elements to display the image and the text overlay respectively. +- Use a `linear-gradient` to create the overlay effect over the image. + +```html +
    + +
    +

    Business
    Pricing

    +
    +
    +``` + +```css +.text-overlay-image { + box-sizing: border-box; + position: relative; + margin: 8px; + max-width: 400px; + max-height: 400px; + width: 100%; +} + +.text-overlay-image figcaption { + box-sizing: border-box; + position: absolute; + top: 0; + bottom: 0; + left: 0; + right: 0; + display: flex; + flex-direction: column; + justify-content: flex-end; + background: linear-gradient(0deg, #00000088 30%, #ffffff44 100%); + color: #fff; + padding: 16px; + font-family: sans-serif; + font-weight: 700; + line-height: 1.2; + font-size: 28px; +} + +.text-overlay-image figcaption h3 { + margin: 0; +} +``` diff --git a/css/snippets/input-with-prefix.md b/css/snippets/input-with-prefix.md new file mode 100644 index 000000000..9d865f149 --- /dev/null +++ b/css/snippets/input-with-prefix.md @@ -0,0 +1,53 @@ +--- +title: Input with prefix +type: snippet +tags: [interactivity,visual] +cover: flower-portrait-4 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates an input with a visual, non-editable prefix. + +- Use `display: flex` to create a container element. +- Remove the border and outline from the `` field. Apply them to the parent element instead to make it look like an input box. +- Use the `:focus-within` pseudo-class selector to style the parent element accordingly, when the user interacts with the `` field. + +```html +
    + +30 + +
    +``` + +```css +.input-box { + display: flex; + align-items: center; + max-width: 300px; + background: #fff; + border: 1px solid #a0a0a0; + border-radius: 4px; + padding-left: 0.5rem; + overflow: hidden; + font-family: sans-serif; +} + +.input-box .prefix { + font-weight: 300; + font-size: 14px; + color: #999; +} + +.input-box input { + flex-grow: 1; + font-size: 14px; + background: #fff; + border: none; + outline: none; + padding: 0.5rem; +} + +.input-box:focus-within { + border-color: #777; +} +``` diff --git a/css/snippets/isometric-card.md b/css/snippets/isometric-card.md new file mode 100644 index 000000000..e98973b4c --- /dev/null +++ b/css/snippets/isometric-card.md @@ -0,0 +1,39 @@ +--- +title: Isometric card +type: snippet +tags: [visual] +author: chalarangelo +cover: guitar-living-room +dateModified: 2021-05-17T14:04:52+03:00 +--- + +Creates an isometric card. + +- Use `transform` with `rotateX()` and `rotateY()` as well as a `box-shadow` to create an isometric card. +- Use `transition` to animate the card, creating a lift effect when the user hovers over it. + +```html +
    +``` + +```css +.isometric-card { + margin: 0 auto; + transform: rotateX(51deg) rotateZ(43deg); + transform-style: preserve-3d; + background-color: #fcfcfc; + will-change: transform; + width: 240px; + height: 320px; + border-radius: 2rem; + box-shadow: 1px 1px 0 1px #f9f9fb, -1px 0 28px 0 rgba(34, 33, 81, 0.01), + 28px 28px 28px 0 rgba(34, 33, 81, 0.25); + transition: 0.4s ease-in-out transform, 0.3s ease-in-out box-shadow; +} + +.isometric-card:hover { + transform: translate3d(0px, -16px, 0px) rotateX(51deg) rotateZ(43deg); + box-shadow: 1px 1px 0 1px #f9f9fb, -1px 0 28px 0 rgba(34, 33, 81, 0.01), + 54px 54px 28px -10px rgba(34, 33, 81, 0.15); +} +``` diff --git a/css/snippets/line-clamp.md b/css/snippets/line-clamp.md new file mode 100644 index 000000000..cd05c6f93 --- /dev/null +++ b/css/snippets/line-clamp.md @@ -0,0 +1,29 @@ +--- +title: Trim multiline text +type: snippet +tags: [layout,visual] +author: chalarangelo +cover: pink-flower-tree +dateModified: 2021-05-16T20:19:13+03:00 +--- + +Limit multiline text to a given number of lines. + +- Use `-webkit-line-clamp` to set the maximum number of lines to be displayed. +- Set `display` to `-webkit-box` and `-webkit-box-orient` to `vertical`, as they are required for `-webkit-line-clamp` to be applied. +- Apply `overflow: hidden` to hide any overflow after the text is trimmed. + +```html +

    + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod enim eget ultricies sollicitudin. Nunc aliquam arcu arcu, non suscipit metus luctus id. Aliquam sodales turpis ipsum, in vehicula dui tempor sit amet. Nullam quis urna erat. Pellentesque mattis dolor purus. Aliquam nisl urna, tempor a euismod a, placerat in mauris. Phasellus neque quam, dapibus quis nunc at, feugiat suscipit tortor. Duis vel posuere dolor. Phasellus risus erat, lobortis et mi vel, viverra faucibus lectus. Etiam ut posuere sapien. Nulla ultrices dui turpis, interdum consectetur urna tempus at. +

    +``` + +```css +.excerpt { + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; +} +``` diff --git a/css/snippets/masonry-layout.md b/css/snippets/masonry-layout.md new file mode 100644 index 000000000..d503978a1 --- /dev/null +++ b/css/snippets/masonry-layout.md @@ -0,0 +1,96 @@ +--- +title: Masonry Layout +type: snippet +tags: [layout] +cover: interior-2 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a masonry-style layout that is especially useful when working with images. + +- Create a masonry-style layout that consists of "bricks" that fall into each other with either a fixed `width` (vertical layout) or a fixed `height` (horizontal layout), forming a perfect fit. Especially useful when working with images. +- Define `.masonry-container` This is the container for the masonry layout and `.masonry-columns`, an inner container in which `.masonry-brick` elements will be placed. +- Apply `display: block` to `.masonry-brick` elements to allow the layout to flow properly. +- Use the `:first-child` pseudo-element selector to apply a different `margin` for the first element to account for its positioning. +- Use CSS variables and media queries for greater flexibility and responsiveness. + +```html +
    +
    + An image + Another image + Another image + One more image + And another one + Last one +
    +
    +``` + +```css +/* Container */ +.masonry-container { + --column-count-small: 1; + --column-count-medium: 2; + --column-count-large: 3; + --column-gap: 0.125rem; + padding: var(--column-gap); +} + +/* Columns */ +.masonry-columns { + column-gap: var(--column-gap); + column-count: var(--column-count-small); + column-width: calc(1 / var(--column-count-small) * 100%); +} + +@media only screen and (min-width: 640px) { + .masonry-columns { + column-count: var(--column-count-medium); + column-width: calc(1 / var(--column-count-medium) * 100%); + } +} + +@media only screen and (min-width: 800px) { + .masonry-columns { + column-count: var(--column-count-large); + column-width: calc(1 / var(--column-count-large) * 100%); + } +} + +/* Bricks */ +.masonry-brick { + width: 100%; + height: auto; + margin: var(--column-gap) 0; + display: block; +} + +.masonry-brick:first-child { + margin: 0 0 var(--column-gap); +} +``` diff --git a/css/snippets/mouse-cursor-gradient-tracking.md b/css/snippets/mouse-cursor-gradient-tracking.md new file mode 100644 index 000000000..fe4faf614 --- /dev/null +++ b/css/snippets/mouse-cursor-gradient-tracking.md @@ -0,0 +1,67 @@ +--- +title: Mouse cursor gradient tracking +type: snippet +tags: [visual,interactivity] +cover: tram-car +dateModified: 2021-01-07T23:52:15+02:00 +--- + +A hover effect where the gradient follows the mouse cursor. + +- Declare two CSS variables, `--x` and `--y`, used to track the position of the mouse on the button. +- Declare a CSS variable, `--size`, used to modify the gradient's dimensions. +- Use `background: radial-gradient(circle closest-side, pink, transparent)` to create the gradient at the correct position. +- Use `Document.querySelector()` and `EventTarget.addEventListener()` to register a handler for the `'mousemove'` event. +- Use `Element.getBoundingClientRect()` and `CSSStyleDeclaration.setProperty()` to update the values of the `--x` and `--y` CSS variables. + +```html + +``` + +```css +.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; +} +``` + +```js +let btn = document.querySelector('.mouse-cursor-gradient-tracking'); +btn.addEventListener('mousemove', e => { + 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'); +}); +``` diff --git a/css/snippets/navigation-list-item-hover-and-focus-effect.md b/css/snippets/navigation-list-item-hover-and-focus-effect.md new file mode 100644 index 000000000..8a429134b --- /dev/null +++ b/css/snippets/navigation-list-item-hover-and-focus-effect.md @@ -0,0 +1,64 @@ +--- +title: Navigation list item hover and focus effect +type: snippet +tags: [visual] +cover: cloudy-rock-formation +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a custom hover and focus effect for navigation items, using CSS transformations. + +- 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-class selectors to transition the pseudo-element to `transform: scale(1)` and show its colored background. +- Prevent the pseudo-element from covering the anchor element by using `z-index`. + +```html + +``` + +```css +.hover-nav ul { + list-style: none; + margin: 0; + padding: 0; + overflow: hidden; +} + +.hover-nav li { + float: left; +} + +.hover-nav li a { + position: relative; + display: block; + color: #fff; + text-align: center; + padding: 8px 12px; + text-decoration: none; + z-index: 0; +} + +li a::before { + position: absolute; + content: ""; + width: 100%; + height: 100%; + bottom: 0; + left: 0; + background-color: #2683f6; + z-index: -1; + transform: scale(0); + transition: transform 0.5s ease-in-out; +} + +li a:hover::before, +li a:focus::before { + transform: scale(1); +} +``` diff --git a/css/snippets/offscreen.md b/css/snippets/offscreen.md new file mode 100644 index 000000000..d79379856 --- /dev/null +++ b/css/snippets/offscreen.md @@ -0,0 +1,34 @@ +--- +title: Offscreen +type: snippet +tags: [layout,visual] +cover: succulent-2 +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Hides an element completely (visually and positionally) in the DOM while still allowing it to be accessible. + +- Remove all borders and padding and hide the element's overflow. +- Use `clip` to define that no part of the element is shown. +- Make the `height` and `width` of the element `1px` and negate them using `margin: -1px`. +- Use `position: absolute` so that the element does not take up space in the DOM. +- **Note:** This technique provides an accessible and layout-friendly alternative to `display: none` (not readable by screen readers) and `visibility: hidden` (takes up physical space in the DOM). + +```html + + Learn More about pants + +``` + +```css +.offscreen { + border: 0; + clip: rect(0 0 0 0); + height: 1px; + margin: -1px; + overflow: hidden; + padding: 0; + position: absolute; + width: 1px; +} +``` diff --git a/css/snippets/overflow-scroll-gradient.md b/css/snippets/overflow-scroll-gradient.md new file mode 100644 index 000000000..5d4e2b4ad --- /dev/null +++ b/css/snippets/overflow-scroll-gradient.md @@ -0,0 +1,53 @@ +--- +title: Overflow scroll gradient +type: snippet +tags: [visual] +cover: memories-of-pineapple-1 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled. + +- Use the `::after` pseudo-element to create a `linear-gradient()` that fades from `transparent` to `white` (top to bottom). +- Use `position: absolute`, `width` and `height` to place and size the pseudo-element in its parent. +- Use `pointer-events: none` to exclude the pseudo-element from mouse events, allowing text behind it to still be selectable/interactive. + +```html +
    +
    + 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? +
    +
    +``` + +```css +.overflow-scroll-gradient { + position: relative; +} + +.overflow-scroll-gradient::after { + content: ''; + position: absolute; + bottom: 0; + width: 250px; + height: 25px; + background: linear-gradient(transparent, white); + pointer-events: none; +} + +.overflow-scroll-gradient-scroller { + overflow-y: scroll; + background: white; + width: 240px; + height: 200px; + padding: 15px; + line-height: 1.2; +} +``` diff --git a/css/snippets/polka-dot-pattern.md b/css/snippets/polka-dot-pattern.md new file mode 100644 index 000000000..723dc5532 --- /dev/null +++ b/css/snippets/polka-dot-pattern.md @@ -0,0 +1,31 @@ +--- +title: Polka dot background pattern +type: snippet +tags: [visual] +cover: bag-waiting +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a polka dot background pattern. + +- Use `background-color` to set a black background. +- Use `background-image` with two `radial-gradient()` values to create two dots. +- Use `background-size` to specify the pattern's size. Use `background-position` to appropriately place the two gradients. +- **Note:** The fixed `height` and `width` of the element is for demonstration purposes only. + +```html +
    +``` + +```css +.polka-dot { + width: 240px; + height: 240px; + background-color: #000; + background-image: radial-gradient(#fff 10%, transparent 11%), + radial-gradient(#fff 10%, transparent 11%); + background-size: 60px 60px; + background-position: 0 0, 30px 30px; + background-repeat: repeat; +} +``` diff --git a/css/snippets/popout-menu.md b/css/snippets/popout-menu.md new file mode 100644 index 000000000..8443aa2c4 --- /dev/null +++ b/css/snippets/popout-menu.md @@ -0,0 +1,43 @@ +--- +title: Popout menu +type: snippet +tags: [interactivity] +cover: city-view +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Reveals an interactive popout menu on hover/focus. + +- Use `left: 100%` to move the popout menu to the right of the parent. +- Use `visibility: hidden` to hide the popout menu initially, allowing for transitions to be applied (unlike `display: none`). +- Use the `:hover`, `:focus` and `:focus-within` pseudo-class selectors to apply `visibility: visible` to the popout menu, displaying it when the parent element is hovered/focused. + +```html +
    +
    Popout menu
    +
    +``` + +```css +.reference { + position: relative; + background: tomato; + width: 100px; + height: 80px; +} + +.popout-menu { + position: absolute; + visibility: hidden; + left: 100%; + background: #9C27B0; + color: white; + padding: 16px; +} + +.reference:hover > .popout-menu, +.reference:focus > .popout-menu, +.reference:focus-within > .popout-menu { + visibility: visible; +} +``` diff --git a/css/snippets/pretty-text-underline.md b/css/snippets/pretty-text-underline.md new file mode 100644 index 000000000..cb932f514 --- /dev/null +++ b/css/snippets/pretty-text-underline.md @@ -0,0 +1,44 @@ +--- +title: Pretty text underline +type: snippet +tags: [visual] +cover: flower-portrait-6 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Provides a nicer alternative to `text-decoration: underline` where descenders do not clip the underline. + +- Use `text-shadow` to apply 4 values with offsets covering a 4x4 `px` area. This ensures the underline has a thick shadow that covers the line where descenders clip it. For best results, use a color that matches the `background` and adjust the `px` values for larger fonts. +- Use `background-image` with `linear-gradient()` and `currentColor` to create a gradient that will act as the actual underline. +- Set `background-position`, `background-repeat` and `background-size` to place the gradient in the correct position. +- Use the `::selection` pseudo-class selector to ensure the text shadow does not interfere with text selection. +- **Note:** This is natively implemented as `text-decoration-skip-ink: auto` but it has less control over the underline. + +```html +
    +

    Pretty text underline without clipping descenders.

    +
    +``` + +```css +.container { + background: #f5f6f9; + color: #333; + padding: 8px 0; +} + +.pretty-text-underline { + display: inline; + text-shadow: 1px 1px #f5f6f9, -1px 1px #f5f6f9, -1px -1px #f5f6f9, + 1px -1px #f5f6f9; + background-image: linear-gradient(90deg, currentColor 100%, transparent 100%); + background-position: bottom; + background-repeat: no-repeat; + background-size: 100% 1px; +} + +.pretty-text-underline::selection { + background-color: rgba(0, 150, 255, 0.3); + text-shadow: none; +} +``` diff --git a/css/snippets/pulse-loader.md b/css/snippets/pulse-loader.md new file mode 100644 index 000000000..f60aa52a9 --- /dev/null +++ b/css/snippets/pulse-loader.md @@ -0,0 +1,57 @@ +--- +title: Pulse loader +type: snippet +tags: [animation] +cover: digital-nomad-8 +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a pulse effect loader animation using the `animation-delay` property. + +- Use `@keyframes` to define an animation at two points in the cycle. At the start (`0%`), the two `
    ` elements have no `width` or `height` and are positioned at the center. At the end (`100%`), both `
    ` elements have increased `width` and `height`, but their `position` is reset to `0`. +- Use `opacity` to transition from `1` to `0` when animating to give the `
    ` elements a disappearing effect as they expand. +- Set a predefined `width` and `height` for the parent container, `.ripple-loader` and use `position: relative` to position its children. +- Use `animation-delay` on the second `
    ` element, so that each element starts its animation at a different time. + +```html +
    +
    +
    +
    +``` + +```css +.ripple-loader { + position: relative; + width: 64px; + height: 64px; +} + +.ripple-loader div { + position: absolute; + border: 4px solid #454ADE; + border-radius: 50%; + animation: ripple-loader 1s ease-out infinite; +} + +.ripple-loader div:nth-child(2) { + animation-delay: -0.5s; +} + +@keyframes ripple-loader { + 0% { + top: 32px; + left: 32px; + width: 0; + height: 0; + opacity: 1; + } + 100% { + top: 0; + left: 0; + width: 64px; + height: 64px; + opacity: 0; + } +} +``` diff --git a/css/snippets/reset-all-styles.md b/css/snippets/reset-all-styles.md new file mode 100644 index 000000000..697a619c7 --- /dev/null +++ b/css/snippets/reset-all-styles.md @@ -0,0 +1,29 @@ +--- +title: Reset all styles +type: snippet +tags: [visual] +cover: rocky-beach-2 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Resets all styles to default values using only one property. + +- Use the `all` property to reset all styles (inherited or not) to their default values. +- **Note:** This will not affect `direction` and `unicode-bidi` properties. + +```html +
    +
    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? +

    +
    +``` + +```css +.reset-all-styles { + all: initial; +} +``` diff --git a/css/snippets/responsive-layout-sidebar.md b/css/snippets/responsive-layout-sidebar.md new file mode 100644 index 000000000..6e9530cb3 --- /dev/null +++ b/css/snippets/responsive-layout-sidebar.md @@ -0,0 +1,46 @@ +--- +title: Responsive layout with sidebar +type: snippet +tags: [layout] +author: chalarangelo +cover: red-petals +dateModified: 2020-09-16T18:54:56+03:00 +--- + +Creates a responsive layout with a content area and a sidebar. + +- Use `display: grid` on the parent container, to create a grid layout. +- Use `minmax()` for the second column (sidebar) to allow it to take up between `150px` and `20%`. +- Use `1fr` for the first column (main content) to take up the rest of the remaining space. + +```html +
    +
    + This element is 1fr large. +
    + +
    +``` + +```css +.container { + display: grid; + grid-template-columns: 1fr minmax(150px, 20%); + height: 100px; +} + +main, aside { + padding: 12px; + text-align: center; +} + +main { + background: #d4f2c4; +} + +aside { + background: #81cfd9; +} +``` diff --git a/css/snippets/rotating-card.md b/css/snippets/rotating-card.md new file mode 100644 index 000000000..eede2b63e --- /dev/null +++ b/css/snippets/rotating-card.md @@ -0,0 +1,69 @@ +--- +title: Rotating Card +type: snippet +tags: [animation] +cover: digital-nomad-11 +dateModified: 2021-03-30T15:24:01+03:00 +--- + +Creates a two sided card which rotates on hover. + +- Set the `backface-visibility` of the cards to none. +- Initially, set `rotateY()` for the back side of the card to `-180deg` and the front side to `0deg`. +- Upon hover, set `rotateY()` for the front side to `180deg` and backside to `0deg`. +- Set the appropriate `perspective` value to create the rotate effect. + +```html +
    +
    +
    Front Side
    +
    +
    +
    Back Side
    +
    +
    +``` + +```css +.card { + perspective: 150rem; + position: relative; + height: 40rem; + max-width: 400px; + margin: 2rem; + box-shadow: none; + background: none; +} + +.card-side { + height: 35rem; + border-radius: 15px; + transition: all 0.8s ease; + backface-visibility: hidden; + position: absolute; + top: 0; + left: 0; + width: 80%; + padding: 2rem; + color: white; +} + +.card-side.back { + transform: rotateY(-180deg); + background-color: #4158D0; + background-image: linear-gradient(43deg, #4158D0 0%,#C850C0 46%, #FFCC70 100%); +} + +.card-side.front { + background-color: #0093E9; + background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%); +} + +.card:hover .card-side.front { + transform: rotateY(180deg); +} + +.card:hover .card-side.back { + transform: rotateY(0deg); +} +``` diff --git a/css/snippets/scroll-progress-bar.md b/css/snippets/scroll-progress-bar.md new file mode 100644 index 000000000..8cecdd5c7 --- /dev/null +++ b/css/snippets/scroll-progress-bar.md @@ -0,0 +1,44 @@ +--- +title: Scroll progress bar +type: snippet +tags: [animation,visual] +author: chalarangelo +cover: coworking-space +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Creates a progress bar indicating the scroll percentage of the page. + +- Use `position: fixed` and a large `z-index` value to place the element at the top of the page and above any content. +- Use `EventTarget.addEventListener()` and `Element.scrollTop` to determine the scroll percentage of the document and apply it to the `width` of the element. + +```html +
    +``` + +```css +body { + min-height: 200vh; +} + +#scroll-progress { + position: fixed; + top: 0; + width: 0%; + height: 4px; + background: #7983ff; + z-index: 10000; +} +``` + +```js +const scrollProgress = document.getElementById('scroll-progress'); +const height = + document.documentElement.scrollHeight - document.documentElement.clientHeight; + +window.addEventListener('scroll', () => { + const scrollTop = + document.body.scrollTop || document.documentElement.scrollTop; + scrollProgress.style.width = `${(scrollTop / height) * 100}%`; +}); +``` diff --git a/css/snippets/shake-invalid-input.md b/css/snippets/shake-invalid-input.md new file mode 100644 index 000000000..499048f1f --- /dev/null +++ b/css/snippets/shake-invalid-input.md @@ -0,0 +1,39 @@ +--- +title: Shake on invalid input +type: snippet +tags: [animation] +cover: perfect-timing +dateModified: 2022-07-31T18:30:11+03:00 +--- + +Creates a shake animation on invalid input. + +- Use the `pattern` attribute to define the regular expression which the input's value must match. +- Use `@keyframes` to define a shake animation, using the `margin-left` property. +- Use the `:invalid` pseudo-class to apply an `animation` to make the element shake. + +```html + +``` + +```css +@keyframes shake { + 0% { + margin-left: 0rem; + } + 25% { + margin-left: 0.5rem; + } + 75% { + margin-left: -0.5rem; + } + 100% { + margin-left: 0rem; + } +} + +input:invalid { + animation: shake 0.2s ease-in-out 0s 2; + box-shadow: 0 0 0.6rem #ff0000; +} +``` diff --git a/css/snippets/shape-separator.md b/css/snippets/shape-separator.md new file mode 100644 index 000000000..3ef7e1024 --- /dev/null +++ b/css/snippets/shape-separator.md @@ -0,0 +1,35 @@ +--- +title: Shape separator +type: snippet +tags: [visual] +unlisted: true +cover: shapes +dateModified: 2021-01-04T12:30:40+02:00 +--- + +Uses an SVG shape to create a separator between two different blocks. + +- Use the `::after` pseudo-element to create the separator element. +- Use `background-image` to add the SVG (a 24x12 triangle) shape via a data URI. The background image will repeat by default, covering the whole area of the pseudo-element. +- Use the `background` of the parent element to set the desired color for the separator. + +```html +
    +``` + +```css +.shape-separator { + position: relative; + height: 48px; + background: #9C27B0; +} + +.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='transparent'/%3E%3C/svg%3E"); + position: absolute; + width: 100%; + height: 12px; + bottom: 0; +} +``` diff --git a/css/snippets/shifting-card.md b/css/snippets/shifting-card.md new file mode 100644 index 000000000..b50e997bd --- /dev/null +++ b/css/snippets/shifting-card.md @@ -0,0 +1,78 @@ +--- +title: Shifting Card +type: snippet +tags: [animation] +author: chalarangelo +cover: clouds-n-mountains +dateModified: 2022-12-14T05:00:00-04:00 +--- + +Creates a card that shifts on hover. + +- Set the appropriate `perspective` on the `.container` element to allow for the shifting effect. +- Add a `transition` for the `transform` property of the `.card` element. +- Use `Document.querySelector()` to select the `.card` element and add event listeners for the `mousemove` and `mouseout` events. +- Use `Element.getBoundingClientRect()` to get the `x`, `y`, `width`, and `height` of the `.card` element. +- Calculate the relative distance as a value between `-1` and `1` for the `x` and `y` axes and apply it through the `transform` property. + +```html +
    +
    +
    +

    Card

    +

    Lorem ipsum dolor sit amet consectetur adipisicing elit. Corrupti repellat, consequuntur doloribus voluptate esse iure?

    +
    +
    +
    +``` + +```css +.container { + display: flex; + padding: 24px; + justify-content: center; + align-items: center; + background: #f3f1fe; +} + +.shifting-card { + width: 350px; + display: flex; + flex-direction: column; + align-items: center; + background: #fff; + border-radius: 10px; + margin: 0.5rem; + transition: transform 0.2s ease-out; + box-shadow: 0 0 5px -2px rgba(0, 0, 0, 0.1); +} + +.shifting-card .content { + text-align: center; + margin: 2rem; + line-height: 1.5; + color: #101010; +} +``` + +```js +const card = document.querySelector('.shifting-card'); +const { x, y, width, height } = card.getBoundingClientRect(); +const cx = x + width / 2; +const cy = y + height / 2; + +const handleMove = e => { + const { pageX, pageY } = e; + const dx = (cx - pageX) / (width / 2); + const dy = (cy - pageY) / (height / 2); + e.target.style.transform = + `rotateX(${10 * dy * -1}deg) rotateY(${10 * dx}deg)`; +}; + +const handleOut = e => { + e.target.style.transform = 'initial'; +}; + +card.addEventListener('mousemove', handleMove); +card.addEventListener('mouseout', handleOut); +``` diff --git a/css/snippets/sibling-fade.md b/css/snippets/sibling-fade.md new file mode 100644 index 000000000..08348719c --- /dev/null +++ b/css/snippets/sibling-fade.md @@ -0,0 +1,30 @@ +--- +title: Sibling fade +type: snippet +tags: [interactivity] +cover: messy-papers +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Fades out the siblings of a hovered item. + +- Use a `transition` to animate changes to `opacity`. +- Use the `:hover` and `:not` pseudo-class selectors to change the `opacity` of all elements except for the one the mouse is over to `0.5`. + +```html +
    + Item 1 Item 2 Item 3 + Item 4 Item 5 Item 6 +
    +``` + +```css +span { + padding: 0 16px; + transition: opacity 0.3s; +} + +.sibling-fade:hover span:not(:hover) { + opacity: 0.5; +} +``` diff --git a/css/snippets/squiggle-link-hover-effect.md b/css/snippets/squiggle-link-hover-effect.md new file mode 100644 index 000000000..c53fa8f8b --- /dev/null +++ b/css/snippets/squiggle-link-hover-effect.md @@ -0,0 +1,33 @@ +--- +title: Squiggle link hover effect +type: snippet +tags: [animation,visual] +author: chalarangelo +cover: dreamy-flowers +dateModified: 2023-01-10T05:00:00-04:00 +--- + +Creates a squiggle effect when hovering over a link. + +- Create a repeating background for the link using a `linear-gradient`. +- Create a `:hover` state for the link with a `background-image` of a data URL containing an SVG with a squiggly path and an animation. + +```html +

    The magnificent octopus swam along gracefully.

    +``` + +```css +a.squiggle { + background: linear-gradient(to bottom, #0087ca 0%, #0087ca 100%); + background-position: 0 100%; + background-repeat: repeat-x; + background-size: 2px 2px; + color: inherit; + text-decoration: none; +} + +a.squiggle:hover { + background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 4'%3E%3Cstyle type='text/css'%3E.squiggle{animation:shift .3s linear infinite;}@keyframes shift {from {transform:translateX(0);}to {transform:translateX(-15px);}}%3C/style%3E%3Cpath fill='none' stroke='%230087ca' stroke-width='2' class='squiggle' d='M0,3.5 c 5,0,5,-3,10,-3 s 5,3,10,3 c 5,0,5,-3,10,-3 s 5,3,10,3'/%3E%3C/svg%3E"); + background-size: auto 4px; +} +``` diff --git a/css/snippets/staggered-animation.md b/css/snippets/staggered-animation.md new file mode 100644 index 000000000..f1d3a51ac --- /dev/null +++ b/css/snippets/staggered-animation.md @@ -0,0 +1,67 @@ +--- +title: Staggered animation +type: snippet +tags: [animation] +cover: aerial-view-port +dateModified: 2021-10-11T18:44:51+03:00 +--- + +Creates a staggered animation for the elements of a list. + +- Set `opacity: 0` and `transform: translateX(100%)` to make list elements transparent and move them all the way to the right. +- Specify the same `transition` properties for list elements, except `transition-delay`. +- Use inline styles to specify a value for `--i` for each list element. This will in turn be used for `transition-delay` to create the stagger effect. +- Use the `:checked` pseudo-class selector for the checkbox to style list elements. Set `opacity` to `1` and `transform` to `translateX(0)` to make them appear and slide into view. + +```html +
    + + +
      +
    • Home
    • +
    • Pricing
    • +
    • Account
    • +
    • Support
    • +
    • About
    • +
    +
    +``` + +```css +.container { + overflow-x: hidden; + width: 100%; +} + +.menu-toggler { + display: none; +} + +.menu-toggler-label { + cursor: pointer; + font-size: 20px; + font-weight: bold; +} + +.stagger-menu { + list-style-type: none; + margin: 16px 0; + padding: 0; +} + +.stagger-menu li { + margin-bottom: 8px; + font-size: 18px; + opacity: 0; + transform: translateX(100%); + transition-property: opacity, transform; + transition-duration: 0.3s; + transition-timing-function: cubic-bezier(0.750, -0.015, 0.565, 1.055); +} + +.menu-toggler:checked ~ .stagger-menu li { + opacity: 1; + transform: translateX(0); + transition-delay: calc(0.055s * var(--i)); +} +``` diff --git a/css/snippets/sticky-list-titles.md b/css/snippets/sticky-list-titles.md new file mode 100644 index 000000000..e8ca50f6c --- /dev/null +++ b/css/snippets/sticky-list-titles.md @@ -0,0 +1,84 @@ +--- +title: List with sticky section headings +type: snippet +tags: [visual] +author: chalarangelo +cover: interior-7 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a list with sticky headings for each section. + +- Use `overflow-y: auto` to allow the list container (`
    `) to overflow vertically. +- Set headings (`
    `) `position` to `sticky` and apply `top: 0` to stick to the top of the container. + +```html +
    +
    +
    A
    +
    Algeria
    +
    Angola
    + +
    B
    +
    Benin
    +
    Botswana
    +
    Burkina Faso
    +
    Burundi
    + +
    C
    +
    Cabo Verde
    +
    Cameroon
    +
    Central African Republic
    +
    Chad
    +
    Comoros
    +
    Congo, Democratic Republic of the
    +
    Congo, Republic of the
    +
    Cote d'Ivoire
    + +
    D
    +
    Djibouti
    + +
    E
    +
    Egypt
    +
    Equatorial Guinea
    +
    Eritrea
    +
    Eswatini (formerly Swaziland)
    +
    Ethiopia
    +
    +
    +``` + +```css +.container { + display: grid; + place-items: center; + min-height: 400px; +} + +.sticky-stack { + background: #37474f; + color: #fff; + margin: 0; + height: 320px; + border-radius: 1rem; + overflow-y: auto; +} + +.sticky-stack dt { + position: sticky; + top: 0; + font-weight: bold; + background: #263238; + color: #cfd8dc; + padding: 0.25rem 1rem; +} + +.sticky-stack dd { + margin: 0; + padding: 0.75rem 1rem; +} + +.sticky-stack dd + dt { + margin-top: 1rem; +} +``` diff --git a/css/snippets/stripes-pattern.md b/css/snippets/stripes-pattern.md new file mode 100644 index 000000000..315b0bcf5 --- /dev/null +++ b/css/snippets/stripes-pattern.md @@ -0,0 +1,29 @@ +--- +title: Stripes background pattern +type: snippet +tags: [visual] +cover: jars-on-shelf +dateModified: 2021-01-11T09:51:43+02:00 +--- + +Creates a stripes background pattern. + +- Use `background-color` to set a white background. +- Use `background-image` with a `radial-gradient()` value to create a vertical stripe. +- Use `background-size` to specify the pattern's size. +- **Note:** The fixed `height` and `width` of the element is for demonstration purposes only. + +```html +
    +``` + +```css +.stripes { + width: 240px; + height: 240px; + background-color: #fff; + background-image: linear-gradient(90deg, transparent 50%, #000 50%); + background-size: 60px 60px; + background-repeat: repeat; +} +``` diff --git a/css/snippets/stylized-quotation-marks.md b/css/snippets/stylized-quotation-marks.md new file mode 100644 index 000000000..7a5688974 --- /dev/null +++ b/css/snippets/stylized-quotation-marks.md @@ -0,0 +1,24 @@ +--- +title: Stylized quotation marks +type: snippet +tags: [visual] +author: chalarangelo +cover: coffee-phone-tray +dateModified: 2021-05-16T19:53:02+03:00 +--- + +Customizes the style of inline quotation marks. + +- Use the `quotes` property to customize the characters used for the opening and closing quotes of a `` element. + +```html +

    + Do or do not, there is no try. – Yoda +

    +``` + +```css +q { + quotes: "“" "”"; +} +``` diff --git a/css/snippets/system-font-stack.md b/css/snippets/system-font-stack.md new file mode 100644 index 000000000..14fdc960a --- /dev/null +++ b/css/snippets/system-font-stack.md @@ -0,0 +1,34 @@ +--- +title: System font stack +type: snippet +tags: [visual] +cover: lavender-shelf +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Uses the native font of the operating system to get close to a native app feel. + +- Define a list of fonts using `font-family`. +- 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 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. +- `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. + +```html +

    This text uses the system font.

    +``` + +```css +.system-font-stack { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, + Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', Helvetica, Arial, + sans-serif; +} +``` diff --git a/css/snippets/text-backdrop-overlay.md b/css/snippets/text-backdrop-overlay.md new file mode 100644 index 000000000..8663bde56 --- /dev/null +++ b/css/snippets/text-backdrop-overlay.md @@ -0,0 +1,36 @@ +--- +title: Image text overlay +type: snippet +tags: [visual] +author: chalarangelo +cover: mountain-lake-cottage +dateModified: 2021-10-13T19:29:39+02:00 +--- + +Displays a text on top of an image using an overlay. + +- Use `backdrop-filter` to apply a `blur(14px)` and `brightness(80%)` effect. This makes text readable regardless of background image and color. + +```html +
    +

    Hello, World

    + +
    +``` + +```css +div { + position: relative; +} + +.text-overlay { + position: absolute; + top: 0; + left: 0; + padding: 1rem; + font-size: 2rem; + font-weight: 300; + color: white; + backdrop-filter: blur(14px) brightness(80%); +} +``` diff --git a/css/snippets/tile-layout-using-inline-block.md b/css/snippets/tile-layout-using-inline-block.md new file mode 100644 index 000000000..1f3070082 --- /dev/null +++ b/css/snippets/tile-layout-using-inline-block.md @@ -0,0 +1,48 @@ +--- +title: 3-tile layout +type: snippet +tags: [layout] +cover: godray-computer-mug +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Aligns items horizontally using `display: inline-block` to create a 3-tile layout. + +- Use `display: inline-block` to create a tiled layout, without using `float`, `flex` or `grid`. +- Use `width` in combination with `calc` to divide the width of the container evenly into 3 columns. +- Set `font-size` for `.tiles` to `0` to avoid whitespace and to `20px` for `

    ` elements to display the text. +- **Note:** If you use relative units (e.g. `em`), using `font-size: 0` to fight whitespace between blocks might cause side effects. + +```html +
    +
    + +

    30 Seconds of CSS

    +
    +
    + +

    30 Seconds of CSS

    +
    +
    + +

    30 Seconds of CSS

    +
    +
    +``` + +```css +.tiles { + width: 600px; + font-size: 0; + margin: 0 auto; +} + +.tile { + width: calc(600px / 3); + display: inline-block; +} + +.tile h2 { + font-size: 20px; +} +``` diff --git a/css/snippets/toggle-switch.md b/css/snippets/toggle-switch.md new file mode 100644 index 000000000..363fc6f23 --- /dev/null +++ b/css/snippets/toggle-switch.md @@ -0,0 +1,56 @@ +--- +title: Toggle switch +type: snippet +tags: [visual,interactivity] +cover: interior-5 +dateModified: 2020-12-30T15:37:37+02:00 +--- + +Creates a toggle switch with CSS only. + +- Use the `for` attribute to associate the `