diff --git a/docs/index.html b/docs/index.html index 0e4339079..3875fc791 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,16 +1,12 @@ - 30 Seconds of CSS

30 Seconds of CSS

A curated collection of useful CSS snippets you can understand in 30 seconds or less.

Bouncing loaderanimation

Creates a bouncing loader animation.

HTML

<div class="bouncing-loader">
+    30 Seconds of CSS         

30 Seconds of CSS

A curated collection of useful CSS snippets you can understand in 30 seconds or less.

Bouncing loaderanimation

Creates a bouncing loader animation.

HTML

<div class="bouncing-loader">
   <div></div>
   <div></div>
   <div></div>
 </div>
 

CSS

@keyframes bouncing-loader {
-  from {
-    opacity: 1;
-    transform: translateY(0);
-  }
   to {
     opacity: 0.1;
-    transform: translateY(-1rem);
+    transform: translate3d(0, -1rem, 0);
   }
 }
 .bouncing-loader {
@@ -31,7 +27,7 @@
 .bouncing-loader > div:nth-child(3) {
   animation-delay: 0.4s;
 }
-

Demo

Explanation

Note: 1rem is usually 16px.

  1. @keyframes defines an animation that has two states, where the element changes opacity and is translated up on the 2D plane using transform: translateY().

  2. .bouncing-loader is the parent container of the bouncing circles and uses display: flex and justify-content: center to position them in the center.

  3. .bouncing-loader > div, targets the three child divs of the parent to be styled. The divs are given a width and height of 1rem, using border-radius: 50% to turn them from squares to circles.

  4. margin: 3rem 0.2rem specifies that each circle has a top/bottom margin of 3rem and left/right margin of 0.2rem so that they do not directly touch each other, giving them some breathing room.

  5. animation is a shorthand property for the various animation properties: animation-name, animation-duration, animation-iteration-count, animation-direction are used.

  6. nth-child(n) targets the element which is the nth child of its parent.

  7. animation-delay is used on the second and third div respectively, so that each element does not start the animation at the same time.

Browser support

95.3%

✅ No caveats.

Box-sizing resetlayout

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

HTML

<div class="box">border-box</div>
+

Demo

Explanation

Note: 1rem is usually 16px.

  1. @keyframes defines an animation that has two states, where the element changes opacity and is translated up on the 2D plane using transform: translate3d(). Using a single axis translation on transform: translate3d() improves the performance of the animation.

  2. .bouncing-loader is the parent container of the bouncing circles and uses display: flex and justify-content: center to position them in the center.

  3. .bouncing-loader > div, targets the three child divs of the parent to be styled. The divs are given a width and height of 1rem, using border-radius: 50% to turn them from squares to circles.

  4. margin: 3rem 0.2rem specifies that each circle has a top/bottom margin of 3rem and left/right margin of 0.2rem so that they do not directly touch each other, giving them some breathing room.

  5. animation is a shorthand property for the various animation properties: animation-name, animation-duration, animation-iteration-count, animation-direction are used.

  6. nth-child(n) targets the element which is the nth child of its parent.

  7. animation-delay is used on the second and third div respectively, so that each element does not start the animation at the same time.

Browser support

95.3%

✅ No caveats.

Box-sizing resetlayout

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

HTML

<div class="box">border-box</div>
 <div class="box content-box">content-box</div>
 

CSS

html {
   box-sizing: border-box;
@@ -274,7 +270,23 @@ li::before {
   display: flex;
   justify-content: space-between;
 }
-

Demo

Item1

Item2

Item3

Explanation

  1. display: flex enables flexbox.
  2. justify-content: space-between evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.

Alternatively, use justify-content: space-around to distribute the children with space around them, rather than between them.

Browser support

98.1%

⚠️ Needs prefixes for full support.

Flexbox centeringlayout

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

HTML

<div class="flexbox-centering">
+

Demo

Item1

Item2

Item3

Explanation

  1. display: flex enables flexbox.
  2. justify-content: space-between evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.

Alternatively, use justify-content: space-around to distribute the children with space around them, rather than between them.

Browser support

98.1%

⚠️ Needs prefixes for full support.

New

Fit image in containerlayoutvisual

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

HTML

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

CSS

.image {
+  background: #34495e;
+  border: 1px solid #34495e;
+  width: 200px;
+  height: 200px;
+}
+.image-contain {
+  object-fit: contain;
+  object-position: center;
+}
+.image-cover {
+  object-fit: cover;
+  object-position: right top;
+}
+

Demo

Explanation

  • object-fit: contain fits the entire image within the container while preserving its aspect ratio.
  • object-fit: cover fills the container with the image while preserving its aspect ratio.
  • object-position: [x] [y] positions the image within the container.

Browser support

92.9%

✅ No caveats.

Flexbox centeringlayout

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

HTML

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

CSS

.flexbox-centering {
@@ -626,4 +638,14 @@ input[type='checkbox']:checked + .switch {
   text-overflow: ellipsis;
   width: 200px;
 }
-

Demo

If I exceed one line's width, I will be truncated.

Explanation

  1. overflow: hidden prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
  2. white-space: nowrap prevents the text from exceeding one line in height.
  3. text-overflow: ellipsis makes it so that if the text exceeds its dimensions, it will end with an ellipsis.
  4. width: 200px; ensures the element has a dimension, to know when to get ellipsis

Browser support

98.4%

⚠️ Only works for single line elements.

\ No newline at end of file +

Demo

If I exceed one line's width, I will be truncated.

Explanation

  1. overflow: hidden prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
  2. white-space: nowrap prevents the text from exceeding one line in height.
  3. text-overflow: ellipsis makes it so that if the text exceeds its dimensions, it will end with an ellipsis.
  4. width: 200px; ensures the element has a dimension, to know when to get ellipsis

Browser support

98.4%

⚠️ Only works for single line elements.

New

Zebra striped listvisual

Creates a striped list with alternating background colors, which is useful for differentiating siblings that have content spread across a wide row.

HTML

<ul>
+  <li>Item 01</li>
+  <li>Item 02</li>
+  <li>Item 03</li>
+  <li>Item 04</li>
+  <li>Item 05</li>
+</ul>
+

CSS

li:nth-child(odd) {
+  background-color: #eee;
+}
+

Demo

  • Item 01
  • Item 02
  • Item 03
  • Item 04
  • Item 05

Explanation

  1. Use the :nth-child(odd) or :nth-child(even) pseudo-class to apply a different background color to elements that match based on their position in a group of siblings.

Note that you can use it to apply different styles to other HTML elements like div, tr, p, ol, etc.

Browser support

98.4%

✅ No caveats.

https://caniuse.com/#feat=css-sel3

\ No newline at end of file diff --git a/index.html b/index.html index 55e18d71d..f6032be0c 100644 --- a/index.html +++ b/index.html @@ -41,6 +41,8 @@ Custom text selection Dynamic shadow Etched text + + NewFit image in container Gradient text Hairline border :not selector @@ -51,6 +53,8 @@ Shape separator System font stack Triangle + + NewZebra striped list