Revamp snippet descriptions
This commit is contained in:
@ -3,18 +3,15 @@ title: Border with top triangle
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Creates a text container with a triangle at the top.
|
||||
Creates a content container with a triangle at the top.
|
||||
|
||||
- Use the `:before` and `:after` pseudo-elements to create two triangles.
|
||||
- The color of the `:before` triangle should be the same as the container's border color.
|
||||
- The color of the `:after` triangle should be the same as the container's background color.
|
||||
- The border width of the `:before` triangle should be `1px` wider than the `:after` triangle, in order to act as the border.
|
||||
- The `:after` triangle should be `1px` to the right of the `:before` triangle to allow for its left border to be shown.
|
||||
- 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 smalle triangle (`:after`) should be `1px` to the right of the larger triangle (`:before`) to allow for its left border to be shown.
|
||||
|
||||
```html
|
||||
<div class="container">
|
||||
Border with top triangle
|
||||
</div>
|
||||
<div class="container">Border with top triangle</div>
|
||||
```
|
||||
|
||||
```css
|
||||
@ -26,7 +23,8 @@ Creates a text container with a triangle at the top.
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.container:before, .container:after {
|
||||
.container:before,
|
||||
.container:after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: 100%;
|
||||
|
||||
@ -5,14 +5,10 @@ tags: animation,intermediate
|
||||
|
||||
Creates a bouncing loader animation.
|
||||
|
||||
- `@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.
|
||||
- `.bouncing-loader` is the parent container of the bouncing circles and uses `display: flex` and `justify-content: center` to position them in the center.
|
||||
- `.bouncing-loader > div`, targets the three child `div`s of the parent to be styled. The `div`s are given a width and height of `1rem`, using `border-radius: 50%` to turn them from squares to circles.
|
||||
- `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.
|
||||
- `animation` is a shorthand property for the various animation properties: `animation-name`, `animation-duration`, `animation-iteration-count`, `animation-direction` are used.
|
||||
- `nth-child(n)` targets the element which is the nth child of its parent.
|
||||
- `animation-delay` is used on the second and third `div` respectively, so that each element does not start the animation at the same time.
|
||||
- Note that `1rem` is usually `16px`.
|
||||
- Use `@keyframes` to define an animation with two states, where the element changes `opacity` and is translated up on the 2D plane using `transform: translate3d()`. Use a single axis translation on `transform: translate3d()` to achieve better animation performance.
|
||||
- Create a parent container, `.bouncing-loader`, for the bouncing circles and use `display: flex` and `justify-content: center` to position them in the center.
|
||||
- Give the three bouncing circle `<div>` elements a `width` and `height` of `16px` and use `border-radius: 50%` to make them circular.
|
||||
- Apply the `bouncing-loader` animation to each of the three bouncing circles, using a different `animation-delay` for each one and `animation-direction: alternate` to create the appropriate effect.
|
||||
|
||||
```html
|
||||
<div class="bouncing-loader">
|
||||
@ -26,7 +22,7 @@ Creates a bouncing loader animation.
|
||||
@keyframes bouncing-loader {
|
||||
to {
|
||||
opacity: 0.1;
|
||||
transform: translate3d(0, -1rem, 0);
|
||||
transform: translate3d(0, -16px, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,8 +32,8 @@ Creates a bouncing loader animation.
|
||||
}
|
||||
|
||||
.bouncing-loader > div {
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin: 3rem 0.2rem;
|
||||
background: #8385aa;
|
||||
border-radius: 50%;
|
||||
|
||||
@ -5,8 +5,8 @@ tags: layout,beginner
|
||||
|
||||
Resets the box-model so that `width` and `height` are not affected by `border` or `padding`.
|
||||
|
||||
- `box-sizing: border-box` makes the addition of `padding` or `border`s not affect an element's `width` or `height`.
|
||||
- `box-sizing: inherit` makes an element respect its parent's `box-sizing` rule.
|
||||
- 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
|
||||
<div class="box">border-box</div>
|
||||
@ -29,6 +29,7 @@ div {
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
padding: 8px;
|
||||
margin: 8px;
|
||||
background: #F24333;
|
||||
color: white;
|
||||
border: 1px solid #BA1B1D;
|
||||
|
||||
@ -5,7 +5,8 @@ tags: animation,intermediate
|
||||
|
||||
Creates a border animation on hover.
|
||||
|
||||
- Use the `:before` and `:after` pseudo-elements as borders that animate 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
|
||||
<button class="animated-border-button">Submit</button>
|
||||
|
||||
@ -3,9 +3,9 @@ title: Circle
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Creates a circle shape with pure CSS.
|
||||
Creates a circular shape with pure CSS.
|
||||
|
||||
- `border-radius: 50%` curves the borders of an element to create a circle.
|
||||
- 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
|
||||
@ -15,8 +15,8 @@ Creates a circle shape with pure CSS.
|
||||
```css
|
||||
.circle {
|
||||
border-radius: 50%;
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
background: #333;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
background: #9C27B0;
|
||||
}
|
||||
```
|
||||
|
||||
@ -5,10 +5,10 @@ tags: layout,beginner
|
||||
|
||||
Ensures that an element self-clears its children.
|
||||
|
||||
- `.clearfix:after` defines a pseudo-element.
|
||||
- `content: ''` allows the pseudo-element to affect layout.
|
||||
- `clear: both` indicates that the left, right or both sides of the element cannot be adjacent to earlier floated elements within the same block formatting context.
|
||||
- This is only useful if you are still using `float` to build layouts. Please consider using a modern approach with flexbox layout or grid layout. For this snippet to work properly you need to ensure that 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).
|
||||
- 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
|
||||
<div class="clearfix">
|
||||
@ -27,5 +27,6 @@ Ensures that an element self-clears its children.
|
||||
|
||||
.floated {
|
||||
float: left;
|
||||
padding: 4px;
|
||||
}
|
||||
```
|
||||
|
||||
@ -3,10 +3,10 @@ title: Constant width to height ratio
|
||||
tags: layout,beginner
|
||||
---
|
||||
|
||||
Given an element of variable width, it will ensure its `height` remains proportionate in a responsive fashion (i.e. its `width` to `height` ratio remains constant).
|
||||
Ensures that an element with variable `width` will retain a proportionate `height` value.
|
||||
|
||||
- `padding-top` on the `:before` pseudo-element causes the height of the element to equal a percentage of its width. `100%` therefore means the element's height will always be `100%` of the width, creating a responsive square.
|
||||
- This method also allows content to be placed inside the element normally.
|
||||
- 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
|
||||
<div class="constant-width-to-height-ratio"></div>
|
||||
@ -14,7 +14,7 @@ Given an element of variable width, it will ensure its `height` remains proporti
|
||||
|
||||
```css
|
||||
.constant-width-to-height-ratio {
|
||||
background: #333;
|
||||
background: #9C27B0;
|
||||
width: 50%;
|
||||
}
|
||||
|
||||
|
||||
@ -3,14 +3,11 @@ title: Counter
|
||||
tags: visual,advanced
|
||||
---
|
||||
|
||||
Counters are, in essence, variables maintained by CSS whose values may be incremented by CSS rules to track how many times they're used.
|
||||
Creates a custom list counter that accounts for nested list elements.
|
||||
|
||||
- `counter-reset` is used to initialize a counter, the name of which is the value of the attribute. By default, the counter starts at `0`. This property can also be used to change its value to any specific number.
|
||||
- `counter-increment` is used for an element that will be countable. Once `counter-reset` is initialized, a counter's value can be increased or decreased.
|
||||
- `counter(name, style)` displays the value of a section counter. Generally used with the `content` property. This function can receive two parameters, the first being the name of the counter and the second one either `decimal` or `upper-roman` (`decimal` by default).
|
||||
- `counters(counter, string, style)` displays the value of a section counter. Generally used with the `content` property. This function can receive three parameters, the first as the name of the counter, the second one you can include a string which comes after the counter and the third one can be `decimal` or `upper-roman` (`decimal` by default).
|
||||
- A CSS counter can be especially useful for making outlined lists, because a new instance of the counter is automatically created in child elements. Using the `counters()` function, separating text can be inserted between different levels of nested counters.
|
||||
- Note that you can create an ordered list using any type of HTML.
|
||||
- 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 `<li>`).
|
||||
- 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 `<li>`). The second value passed to it (`'.'`) acts as the delimiter for nested counters.
|
||||
|
||||
```html
|
||||
<ul>
|
||||
@ -30,6 +27,7 @@ Counters are, in essence, variables maintained by CSS whose values may be increm
|
||||
```css
|
||||
ul {
|
||||
counter-reset: counter;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
li:before {
|
||||
|
||||
@ -3,13 +3,12 @@ title: Custom scrollbar
|
||||
tags: visual,advanced
|
||||
---
|
||||
|
||||
Customizes the scrollbar style for the document and elements with scrollable overflow, on WebKit platforms.
|
||||
Customizes the scrollbar style for the an elements with scrollable overflow.
|
||||
|
||||
- `::-webkit-scrollbar` targets the whole scrollbar element.
|
||||
- `::-webkit-scrollbar-track` targets only the scrollbar track.
|
||||
- `::-webkit-scrollbar-thumb` targets the scrollbar thumb.
|
||||
- Apply the same selectors and styles without `.custom-scrollbar` to style the document scrollbar.
|
||||
- Scrollbar styling doesn't appear to be on any standards track. There are many other pseudo-elements that you can use to style scrollbars. For more info, visit the [WebKit Blog](https://webkit.org/blog/363/styling-scrollbars/).
|
||||
- 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
|
||||
<div class="custom-scrollbar">
|
||||
|
||||
@ -5,8 +5,7 @@ tags: visual,beginner
|
||||
|
||||
Changes the styling of text selection.
|
||||
|
||||
- `::selection` defines a pseudo selector on an element to style text within it when selected. Note that if you don't combine any other selector your style will be applied at document root level, to any selectable element.
|
||||
- Requires prefixes for full support and is not actually in any specification.
|
||||
- Use the `::selection` pseudo-selector to style text within it when selected.
|
||||
|
||||
```html
|
||||
<p class="custom-text-selection">Select some of this text.</p>
|
||||
|
||||
@ -5,8 +5,8 @@ tags: interactivity,beginner
|
||||
|
||||
Makes the content unselectable.
|
||||
|
||||
- `user-select: none` specifies that the text cannot be selected.
|
||||
- This is not a secure method to prevent users from copying content.
|
||||
- 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
|
||||
<p>You can select me.</p>
|
||||
|
||||
@ -3,14 +3,13 @@ title: Display table centering
|
||||
tags: layout,intermediate
|
||||
---
|
||||
|
||||
Vertically and horizontally centers a child element within its parent element using `display: table` (as an alternative to `flexbox`).
|
||||
Vertically and horizontally centers a child element within its parent element, using `display: table`.
|
||||
|
||||
- `display: table` on '.center' allows the element to behave like a `<table>` HTML element.
|
||||
- 100% height and width on '.center' allows the element to fill the available space within its parent element.
|
||||
- `display: table-cell` on '.center > span' allows the element to behave like an <td> HTML element.
|
||||
- `text-align: center` on '.center > span' centers the child element horizontally.
|
||||
- `vertical-align: middle` on '.center > span' centers the child element vertically.
|
||||
- The outer parent ('.container' in this case) must have a fixed height and width.
|
||||
- Use `display: table` to make the `.center` element behave like a `<table>` 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 `<td>` 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
|
||||
<div class="container">
|
||||
@ -20,7 +19,7 @@ Vertically and horizontally centers a child element within its parent element us
|
||||
|
||||
```css
|
||||
.container {
|
||||
border: 1px solid #333;
|
||||
border: 1px solid #9C27B0;
|
||||
height: 250px;
|
||||
width: 250px;
|
||||
}
|
||||
|
||||
@ -5,7 +5,8 @@ tags: animation,intermediate
|
||||
|
||||
Creates a donut spinner that can be used to indicate the loading of content.
|
||||
|
||||
- Use a semi-transparent `border` for the whole element, except one side that will serve as the loading indicator for the donut. Use `animation` to rotate the element.
|
||||
- Use a semi-transparent `border` for the whole element, except 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
|
||||
<div class="donut"></div>
|
||||
|
||||
@ -3,9 +3,10 @@ title: Drop cap
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Makes the first letter in the first paragraph bigger than the rest of the text - often used to signify the beginning of a new section.
|
||||
Makes the first letter of the first paragraph bigger than the rest of the text.
|
||||
|
||||
- Use the `::first-letter` pseudo-element to style the first element of the paragraph, use the `:first-child` selector to select only the first paragraph.
|
||||
- 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
|
||||
<p>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.</p>
|
||||
|
||||
@ -5,16 +5,10 @@ tags: visual,intermediate
|
||||
|
||||
Creates a shadow similar to `box-shadow` but based on the colors of the element itself.
|
||||
|
||||
- `position: relative` on the element establishes a Cartesian positioning context for psuedo-elements.
|
||||
- `z-index: 1` establishes a new stacking context.
|
||||
- `:after` defines a pseudo-element.
|
||||
- `position: absolute` takes the pseudo element out of the flow of the document and positions it in relation to the parent.
|
||||
- `width: 100%` and `height: 100%` sizes the pseudo-element to fill its parent's dimensions, making it equal in size.
|
||||
- `background: inherit` causes the pseudo-element to inherit the linear gradient specified on the element.
|
||||
- `top: 0.5rem` offsets the pseudo-element down slightly from its parent.
|
||||
- `filter: blur(0.4rem)` will blur the pseudo-element to create the appearance of a shadow underneath.
|
||||
- `opacity: 0.7` makes the pseudo-element partially transparent.
|
||||
- `z-index: -1` positions the pseudo-element behind the parent but in front of the background.
|
||||
- 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
|
||||
<div class="dynamic-shadow"></div>
|
||||
|
||||
@ -3,10 +3,10 @@ title: Easing variables
|
||||
tags: animation,beginner
|
||||
---
|
||||
|
||||
Variables that can be reused for `transition-timing-function` properties, more powerful than the built-in `ease`, `ease-in`, `ease-out` and `ease-in-out`.
|
||||
List of variables that can be reused for `transition-timing-function` properties.
|
||||
|
||||
- The variables are defined globally within the `:root` CSS pseudo-class which matches the root element of a tree representing the document.
|
||||
- In HTML, `:root` represents the `<html>` element and is identical to the selector `html`, except that its specificity is higher.
|
||||
- Use the custom property syntax to define appropriate timing functions for transitions.
|
||||
- The variables can be defined globally within the `:root` CSS pseudo-class which matches the root element of a tree representing the document or inside any other selector.
|
||||
|
||||
```html
|
||||
<div class="easing-variables">Hover</div>
|
||||
@ -45,7 +45,7 @@ Variables that can be reused for `transition-timing-function` properties, more p
|
||||
color: white;
|
||||
line-height: 50px;
|
||||
text-align: center;
|
||||
background: #333;
|
||||
background: #9C27B0;
|
||||
transition: transform 1s var(--ease-out-quart);
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ tags: visual,intermediate
|
||||
|
||||
Creates an effect where text appears to be "etched" or engraved into the background.
|
||||
|
||||
- `text-shadow: 0 2px white` creates a white shadow offset `0px` horizontally and `2px` vertically from the origin position.
|
||||
- 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.
|
||||
|
||||
|
||||
@ -5,9 +5,9 @@ tags: layout,intermediate
|
||||
|
||||
Evenly distributes child elements within a parent element.
|
||||
|
||||
- `display: flex` enables flexbox.
|
||||
- `justify-content: space-between` evenly distributes child elements horizontally. The first item is positioned at the left edge, while the last item is positioned at the right edge.
|
||||
- Alternatively, use `justify-content: space-around` to distribute the children with space around them, rather than between them.
|
||||
- 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, you can use `justify-content: space-around` to distribute the children with space around them, rather than between them.
|
||||
|
||||
```html
|
||||
<div class="evenly-distributed-children">
|
||||
|
||||
@ -3,11 +3,11 @@ title: Fit image in container
|
||||
tags: layout,visual,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
Fits an positions an image appropriately inside its container while preserving its aspect ratio.
|
||||
|
||||
- `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.
|
||||
- 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
|
||||
<img class="image image-contain" src="https://picsum.photos/600/200" />
|
||||
|
||||
@ -3,11 +3,11 @@ title: Flexbox centering
|
||||
tags: layout,beginner
|
||||
---
|
||||
|
||||
Horizontally and vertically centers a child element within a parent element using `flexbox`.
|
||||
Horizontally and vertically centers a child element within a parent element using flexbox.
|
||||
|
||||
- `display: flex` creates a flexbox layout.
|
||||
- `justify-content: center` centers the child horizontally.
|
||||
- `align-items: center` centers the child vertically.
|
||||
- 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
|
||||
<div class="flexbox-centering">
|
||||
|
||||
@ -6,8 +6,8 @@ tags: visual,advanced
|
||||
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 (`dl`) to create a layout with two columns.
|
||||
- Set headings (`dt`) to `grid-column: 1` and content (`dd`) to `grid-column: 2`
|
||||
- Use `display: grid` on the inner container (`<dl>`) to create a layout with two columns.
|
||||
- Set headings (`<dt>`) to `grid-column: 1` and content (`<dd>`) to `grid-column: 2`
|
||||
- Finally, apply `position: sticky` and `top: 0.5rem` to headings to create a floating effect.
|
||||
|
||||
```html
|
||||
|
||||
@ -5,7 +5,7 @@ tags: visual,interactivity,intermediate
|
||||
|
||||
Changes the appearance of a form if any of its children are focused.
|
||||
|
||||
- The psuedo class `:focus-within` applies styles to a parent element if any child element gets focused. For example, an `input` element inside a `form` element.
|
||||
- Use the pseudo-class `:focus-within` to apply styles to a parent element if any child element gets focused.
|
||||
|
||||
```html
|
||||
<form>
|
||||
@ -34,6 +34,6 @@ label {
|
||||
}
|
||||
|
||||
input {
|
||||
margin: 4px;
|
||||
margin: 4px 12px;
|
||||
}
|
||||
```
|
||||
|
||||
@ -3,9 +3,13 @@ title: Fullscreen
|
||||
tags: visual,advanced
|
||||
---
|
||||
|
||||
Applies styles to an element when in fullscreen mode.
|
||||
|
||||
|
||||
The `:fullscreen` CSS pseudo-element represents an element that's displayed when the browser is in fullscreen mode.
|
||||
|
||||
- `:fullscreen` CSS pseudo-element selector is used to select and style an element that is being displayed in fullscreen mode.
|
||||
- Use the `:fullscreen` CSS pseudo-element selector to select and style an element that is displayed in fullscreen mode.
|
||||
- Use a `<button>` and `Element.requestFullscreen()` to create a button that makes the element fullscreen for the purposes of previewing the style.
|
||||
|
||||
```html
|
||||
<div class="container">
|
||||
|
||||
@ -5,10 +5,9 @@ tags: visual,intermediate
|
||||
|
||||
Gives text a gradient color.
|
||||
|
||||
- `background: -webkit-linear-gradient(...)` gives the text element a gradient background.
|
||||
- `webkit-text-fill-color: transparent` fills the text with a transparent color.
|
||||
- `webkit-background-clip: text` clips the background with the text, filling the text with the gradient background as the color.
|
||||
- Uses non-standard properties.
|
||||
- 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
|
||||
<p class="gradient-text">Gradient text</p>
|
||||
|
||||
@ -5,9 +5,9 @@ tags: layout,beginner
|
||||
|
||||
Horizontally and vertically centers a child element within a parent element using `grid`.
|
||||
|
||||
- `display: grid` creates a grid layout
|
||||
- `justify-content: center` centers the child horizontally.
|
||||
- `align-items: center` centers the child vertically.
|
||||
- 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
|
||||
<div class="grid-centering">
|
||||
|
||||
@ -1,16 +1,15 @@
|
||||
---
|
||||
title: Hamburger Button
|
||||
tags: interactivity,beginner
|
||||
tags: interactivity,intermediate
|
||||
---
|
||||
|
||||
Displays a hamburger menu which transitions to a cross on hover.
|
||||
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.
|
||||
- The container is set to be a flex container (`display: flex`) with `flex-direction` to be `column` and `flex-wrap` to be `wrap` (alternatively, you can set both properties by a shorthand `flex-flow: column wrap`).
|
||||
- Set the container to `display: flex` with `flex-flow: column wrap`.
|
||||
- Add distance between the bars using `justify-content: space-between`.
|
||||
- The animation has 3 parts: top and bottom bars transforming to 45 degree angles (`rotate(45deg)`), and the middle bar fading away by setting `opacity: 0`.
|
||||
- The `transform-origin` is set to `left` so the bars rotate around the left point.
|
||||
- Set `transition all 0.5s` so that both `transform` and `opacity` properties are animated for half a second.
|
||||
- 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
|
||||
<div class="hamburger-menu">
|
||||
@ -23,8 +22,7 @@ Displays a hamburger menu which transitions to a cross on hover.
|
||||
```css
|
||||
.hamburger-menu {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-wrap: wrap;
|
||||
flex-flow: column wrap;
|
||||
justify-content: space-between;
|
||||
height: 2.5rem;
|
||||
width: 2.5rem;
|
||||
|
||||
@ -5,13 +5,12 @@ tags: animation,intermediate
|
||||
|
||||
Transitions an element's height from `0` to `auto` when its height is unknown.
|
||||
|
||||
- `transition: max-height: 0.5s cubic-bezier(...)` specifies that changes to `max-height` should be transitioned over 0.5 seconds, using an `ease-out-quint` timing function.
|
||||
- `overflow: hidden` prevents the contents of the hidden element from overflowing its container.
|
||||
- `max-height: 0` specifies that the element has no height initially.
|
||||
- `.target:hover > .el` specifies that when the parent is hovered over, target a child `.el` within it and use the `--max-height` variable which was defined by JavaScript.
|
||||
- `el.scrollHeight` is the height of the element including overflow, which will change dynamically based on the content of the element.
|
||||
- `el.style.setProperty(...)` sets the `--max-height` CSS variable which is used to specify the `max-height` of the element the target is hovered over, allowing it to transition smoothly from 0 to auto.
|
||||
- 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.
|
||||
- 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
|
||||
<div class="trigger">
|
||||
@ -22,7 +21,7 @@ Transitions an element's height from `0` to `auto` when its height is unknown.
|
||||
|
||||
```css
|
||||
.el {
|
||||
transition: max-height 0.5s;
|
||||
transition: max-height 0.3s;
|
||||
overflow: hidden;
|
||||
max-height: 0;
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ title: Horizontal scroll snap
|
||||
tags: interactivity,intermediate
|
||||
---
|
||||
|
||||
Creates a scrollable container that will snap on elements when scrolling.
|
||||
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.
|
||||
@ -43,6 +43,7 @@ Creates a scrollable container that will snap on elements when scrolling.
|
||||
|
||||
.horizontal-snap img {
|
||||
width: 180px;
|
||||
max-width: none;
|
||||
object-fit: contain;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ Creates a card that displays additional content on hover.
|
||||
<img src="https://picsum.photos/id/404/367/267"/>
|
||||
<h3>Lorem ipsum</h3>
|
||||
<div class="focus-content">
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br/> <a href="#" class="btn">Link to source</a>
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br/> <a href="#">Link to source</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@ -25,6 +25,7 @@ Creates a card that displays additional content on hover.
|
||||
.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;
|
||||
|
||||
@ -5,13 +5,10 @@ tags: animation,intermediate
|
||||
|
||||
Creates a shadow box around the text when it is hovered.
|
||||
|
||||
- `display: inline-block` to set width and length for `p` element thus making it an `inline-block`.
|
||||
- Set `transform: perspective(1px)` to give element a 3D space by affecting the distance between the Z plane and the user and `translate(0)` to reposition the `p` element along z-axis in 3D space.
|
||||
- `box-shadow:` to set up the box.
|
||||
- `transparent` to make box transparent.
|
||||
- `transition-property` to enable transitions for both `box-shadow` and `transform`.
|
||||
- `:hover` to activate whole css when hovering is done until `active`.
|
||||
- `transform: scale(1.2)` to change the scale, magnifying the text.
|
||||
- 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
|
||||
<p class="hover-shadow-box-animation">Box it!</p>
|
||||
|
||||
@ -5,16 +5,11 @@ tags: animation,advanced
|
||||
|
||||
Creates an animated underline effect when the text is hovered over.
|
||||
|
||||
- `display: inline-block` makes the block `p` an `inline-block` to prevent the underline from spanning the entire parent width rather than just the content (text).
|
||||
- `position: relative` on the element establishes a Cartesian positioning context for pseudo-elements.
|
||||
- `:after` defines a pseudo-element.
|
||||
- `position: absolute` takes the pseudo element out of the flow of the document and positions it in relation to the parent.
|
||||
- `width: 100%` ensures the pseudo-element spans the entire width of the text block.
|
||||
- `transform: scaleX(0)` initially scales the pseudo element to 0 so it has no width and is not visible.
|
||||
- `bottom: 0` and `left: 0` position it to the bottom left of the block.
|
||||
- `transition: transform 0.25s ease-out` means changes to `transform` will be transitioned over 0.25 seconds with an `ease-out` timing function.
|
||||
- `transform-origin: bottom right` means the transform anchor point is positioned at the bottom right of the block.
|
||||
- `:hover:after` then uses `scaleX(1)` to transition the width to 100%, then changes the `transform-origin` to `bottom left` so that the anchor point is reversed, allowing it transition out in the other direction when hovered off.
|
||||
- Use `display: inline-block` to prevent the underline from spanning the entire parent width rather than just the text content.
|
||||
- Use the `:after` pseudo-element with a `width` of `100%` and `position: absolute`, placing 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`.
|
||||
|
||||
```html
|
||||
<p class="hover-underline-animation">Hover this text to see the effect!</p>
|
||||
|
||||
@ -5,10 +5,10 @@ tags: layout,animation,intermediate
|
||||
|
||||
Displays a menu overlay when the image is hovered.
|
||||
|
||||
- Use a `figure` to wrap the `img` element and a `div` element that will contain the menu links.
|
||||
- Use the `opacity` and `right` attributes to animate the image on hover, to create a sliding effect.
|
||||
- Set the `left` attribute of the `div` to the negative of the element's `width` and 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 `div` to vertically center the menu items.
|
||||
- Use a `<figure>` to wrap the `<img>` element and a `<div>` 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 `<div>` to the negative of the element's `width` and 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 `<div>` to vertically center the menu items.
|
||||
|
||||
```html
|
||||
<figure class="hover-menu">
|
||||
|
||||
@ -5,7 +5,7 @@ tags: animation,visual,intermediate
|
||||
|
||||
Creates a rotate effect for the image on hover.
|
||||
|
||||
- Use `scale` and `rotate` when hovering over the parent element (a `figure`) to animate the image, using the `transition` property.
|
||||
- Use `scale` and `rotate` when hovering over the parent element (a `<figure>`) to animate the image, using the `transition` property.
|
||||
- Use `overflow: hidden` on the parent container to hide the excess from the image transformation.
|
||||
|
||||
```html
|
||||
|
||||
@ -5,24 +5,60 @@ tags: layout,intermediate
|
||||
|
||||
Creates a responsive image mosaic.
|
||||
|
||||
- Use `display: grid` to create an appropriate responsive layout.
|
||||
- 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
|
||||
<div class="image-mosaic">
|
||||
<div class="card card-tall card-wide" style="background-image:url('https://picsum.photos/id/564/1200/800')"></div>
|
||||
<div class="card card-tall" style="background-image:url('https://picsum.photos/id/566/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/575/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/626/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/667/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/678/800/530')"></div>
|
||||
<div class="card card-wide" style="background-image:url('https://picsum.photos/id/695/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/683/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/693/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/715/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/610/800/530')"></div>
|
||||
<div class="card" style="background-image:url('https://picsum.photos/id/599/800/530')"></div>
|
||||
<div
|
||||
class="card card-tall card-wide"
|
||||
style="background-image: url('https://picsum.photos/id/564/1200/800')"
|
||||
></div>
|
||||
<div
|
||||
class="card card-tall"
|
||||
style="background-image: url('https://picsum.photos/id/566/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/575/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/626/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/667/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/678/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card card-wide"
|
||||
style="background-image: url('https://picsum.photos/id/695/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/683/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/693/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/715/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/610/800/530')"
|
||||
></div>
|
||||
<div
|
||||
class="card"
|
||||
style="background-image: url('https://picsum.photos/id/599/800/530')"
|
||||
></div>
|
||||
</div>
|
||||
```
|
||||
|
||||
@ -51,7 +87,9 @@ Creates a responsive image mosaic.
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
}
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
@media screen and (min-width: 600px) {
|
||||
.card-tall {
|
||||
|
||||
@ -5,9 +5,9 @@ tags: visual,animation,advanced
|
||||
|
||||
Displays an image overlay effect on hover.
|
||||
|
||||
- Use the `:before` and `:after` elements for the top and bottom bars of the overlay respectively, setting their `opacity`, `transform` and `transition` to produce the desired effect.
|
||||
- Use the `figcaption` for the text of the overlay, setting `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 produce the desired effect.
|
||||
- Use the `:before` and `:after` pseudo-elements for the top and bottom bars of the overlay respectively, setting their `opacity`, `transform` and `transition` to produce the desired effect.
|
||||
- Use the `<figcaption>` for the text of the overlay, setting `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 produce the desired effect.
|
||||
|
||||
```html
|
||||
<figure class="hover-img">
|
||||
|
||||
@ -6,13 +6,13 @@ tags: interactivity,visual,intermediate
|
||||
Creates an input with a visual, non-editable prefix.
|
||||
|
||||
- Use `display: flex` to create a container element.
|
||||
- Remove the border and outline from the `input` field and apply them to the parent element instead to make it look like an input box.
|
||||
- Use the `:focus-within` selector to style the parent element accordingly, when the user interacts with the `input` field.
|
||||
- Remove the border and outline from the `<input>` field and 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 `<input>` field.
|
||||
|
||||
```html
|
||||
<div class="input-box">
|
||||
<span class="prefix">+30</span>
|
||||
<input type="tel" placeholder="210 123 4567"/>
|
||||
<input type="tel" placeholder="210 123 4567"/>
|
||||
</div>
|
||||
```
|
||||
|
||||
@ -20,7 +20,8 @@ Creates an input with a visual, non-editable prefix.
|
||||
.input-box {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
max-width: 300px;
|
||||
max-width: 300px;
|
||||
background: #fff;
|
||||
border: 1px solid #a0a0a0;
|
||||
border-radius: 4px;
|
||||
padding-left: 0.5rem;
|
||||
@ -37,10 +38,11 @@ Creates an input with a visual, non-editable prefix.
|
||||
.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;
|
||||
|
||||
@ -3,22 +3,47 @@ title: Masonry Layout
|
||||
tags: layout,advanced
|
||||
---
|
||||
|
||||
Creates a vertical masonry layout using HTML and CSS.
|
||||
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.
|
||||
- `.masonry-container` is the container for the masonry layout. Within that container, there's a `div.masonry-columns`, which will automatically put each child element, `.masonry-brick`, into the layout.
|
||||
- `.masonry-brick` must be have `display: block` to allow the layout to flow properly, while the `:first-child` of this class should have a different `margin` to account for its positioning.
|
||||
- CSS variables are used to allow for greater flexibility for the layout, while media queries ensure that the layout flows responsively in different viewport sizes.
|
||||
- Define `.masonry-container`, 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 to allow for greater flexibility within the layout in combination with media queries to ensure that the layout is responsive in different viewport sizes.
|
||||
|
||||
```html
|
||||
<div class="masonry-container">
|
||||
<div class="masonry-columns">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1016/384/256" alt="An image">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1025/495/330" alt="Another image">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1024/192/128" alt="Another image">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1028/518/345" alt="One more image">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1035/585/390" alt="And another one">
|
||||
<img class="masonry-brick" src="https://picsum.photos/id/1074/384/216" alt="Last one">
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1016/384/256"
|
||||
alt="An image"
|
||||
/>
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1025/495/330"
|
||||
alt="Another image"
|
||||
/>
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1024/192/128"
|
||||
alt="Another image"
|
||||
/>
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1028/518/345"
|
||||
alt="One more image"
|
||||
/>
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1035/585/390"
|
||||
alt="And another one"
|
||||
/>
|
||||
<img
|
||||
class="masonry-brick"
|
||||
src="https://picsum.photos/id/1074/384/216"
|
||||
alt="Last one"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
@ -5,9 +5,11 @@ tags: visual,interactivity,advanced
|
||||
|
||||
A hover effect where the gradient follows the mouse cursor.
|
||||
|
||||
- `--x` and `--y` are used to track the position of the mouse on the button.
|
||||
- `--size` is used to keep modify of the gradient's dimensions.
|
||||
- `background: radial-gradient(circle closest-side, pink, transparent);` creates the gradient at the correct postion.
|
||||
- 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 postion.
|
||||
- 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
|
||||
<button class="mouse-cursor-gradient-tracking">
|
||||
@ -52,11 +54,11 @@ A hover effect where the gradient follows the mouse cursor.
|
||||
|
||||
```js
|
||||
let btn = document.querySelector('.mouse-cursor-gradient-tracking');
|
||||
btn.onmousemove = function(e) {
|
||||
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');
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
@ -3,11 +3,11 @@ title: Navigation list item hover and focus effect
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Fancy hover and focus effect at navigation items using transform CSS property.
|
||||
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-selectors to transition to `transform: scale(1)` and show the pseudo-element with its colored background.
|
||||
- Prevent the pseudo-element from covering the anchor element by using `z-index: -1`.
|
||||
- Use the `:hover` and `:focus` pseudo-class selectors to transition to `transform: scale(1)` and show the pseudo-element with its colored background.
|
||||
- Prevent the pseudo-element from covering the anchor element by using `z-index`.
|
||||
|
||||
```html
|
||||
<nav class="hover-nav">
|
||||
@ -38,6 +38,7 @@ Fancy hover and focus effect at navigation items using transform CSS property.
|
||||
text-align: center;
|
||||
padding: 8px 12px;
|
||||
text-decoration: none;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
li a:before {
|
||||
|
||||
@ -4,15 +4,12 @@ tags: layout,visual,intermediate
|
||||
---
|
||||
|
||||
Completely hides an element visually and positionally in the DOM while still allowing it to be accessible.
|
||||
Provides an alternative to `display: none` (not readable by screen readers) and `visibility: hidden` (takes up physical space in the DOM).
|
||||
|
||||
- Remove all borders.
|
||||
- Remove all borders and padding and hide the element's overflow.
|
||||
- Use `clip` to indicate that no part of the element should be shown.
|
||||
- Make the height and width of the element 1px.
|
||||
- Negate the elements height and width using `margin: -1px`.
|
||||
- Hide the element's overflow.
|
||||
- Remove all padding.
|
||||
- Position the element absolutely so that it does not take up space in the DOM.
|
||||
- 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 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
|
||||
<a class="button" href="https://google.com">
|
||||
|
||||
@ -5,14 +5,9 @@ tags: visual,intermediate
|
||||
|
||||
Adds a fading gradient to an overflowing element to better indicate there is more content to be scrolled.
|
||||
|
||||
- `position: relative` on the parent establishes a Cartesian positioning context for pseudo-elements.
|
||||
- `:after` defines a pseudo element.
|
||||
- `background-image: linear-gradient(...)` adds a linear gradient that fades from transparent to white (top to bottom).
|
||||
- `position: absolute` takes the pseudo element out of the flow of the document and positions it in relation to the parent.
|
||||
- `width: 240px` matches the size of the scrolling element (which is a child of the parent that has the pseudo element).
|
||||
- `height: 25px` is the height of the fading gradient pseudo-element, which should be kept relatively small.
|
||||
- `bottom: 0` positions the pseudo-element at the bottom of the parent.
|
||||
- `pointer-events: none` specifies that the pseudo-element cannot be a target of mouse events, allowing text behind it to still be selectable/interactive.
|
||||
- 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 appropriately 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
|
||||
<div class="overflow-scroll-gradient">
|
||||
|
||||
@ -3,15 +3,11 @@ title: Popout menu
|
||||
tags: interactivity,intermediate
|
||||
---
|
||||
|
||||
Reveals an interactive popout menu on hover and focus.
|
||||
Reveals an interactive popout menu on hover/focus.
|
||||
|
||||
- `position: relative` on the reference parent establishes a Cartesian positioning context for its child.
|
||||
- `position: absolute` takes the popout menu out of the flow of the document and positions it in relation to the parent.
|
||||
- `left: 100%` moves the the popout menu 100% of its parent's width from the left.
|
||||
- `visibility: hidden` hides the popout menu initially and allows for transitions (unlike `display: none`).
|
||||
- `.reference:hover > .popout-menu` means that when `.reference` is hovered over, select immediate children with a class of `.popout-menu` and change their `visibility` to `visible`, which shows the popout.
|
||||
- `.reference:focus > .popout-menu` means that when `.reference` is focused, the popout would be shown.
|
||||
- `.reference:focus-within > .popout-menu` ensures that the popout is shown when the focus is within the reference.
|
||||
- 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
|
||||
<div class="reference" tabindex="0">
|
||||
@ -31,7 +27,7 @@ Reveals an interactive popout menu on hover and focus.
|
||||
position: absolute;
|
||||
visibility: hidden;
|
||||
left: 100%;
|
||||
background: #333;
|
||||
background: #9C27B0;
|
||||
color: white;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
@ -3,33 +3,37 @@ title: Pretty text underline
|
||||
tags: visual,intermediate
|
||||
---
|
||||
|
||||
A nicer alternative to `text-decoration: underline` where descenders do not clip the underline.
|
||||
Natively implemented as `text-decoration-skip-ink: auto` but it has less control over the underline.
|
||||
Provides a nicer alternative to `text-decoration: underline` where descenders do not clip the underline.
|
||||
|
||||
- `text-shadow` uses 4 values with offsets that cover a 4x4 px area to ensure the underline has a "thick" shadow that covers the line where descenders clip it. Use a color that matches the background. For a larger font, use a larger `px` size. Additional values can create an even thicker shadow, and subpixel values can also be used.
|
||||
- `background-image: linear-gradient(...)` creates a 90deg gradient using the text color (`currentColor`).
|
||||
- The `background-*` properties size the gradient as 100% of the width of the block and 1px in height at the bottom and disables repetition, which creates a 1px underline beneath the text.
|
||||
- The `::selection` pseudo selector rule ensures the text shadow does not interfere with text selection.
|
||||
- Use `text-shadow` to apply 4 values with offsets covering a 4x4 px area, ensuring the underline has a thick shadow that covers the line where descenders clip it. For the 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 an appropriate 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
|
||||
<p class="pretty-text-underline">Pretty text underline without clipping descenders.</p>
|
||||
<div class="container">
|
||||
<p class="pretty-text-underline">Pretty text underline without clipping descenders.</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
```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;
|
||||
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::-moz-selection {
|
||||
background-color: rgba(0, 150, 255, 0.3);
|
||||
text-shadow: none;
|
||||
}
|
||||
|
||||
.pretty-text-underline::selection {
|
||||
background-color: rgba(0, 150, 255, 0.3);
|
||||
text-shadow: none;
|
||||
|
||||
@ -5,9 +5,9 @@ tags: animation,beginner
|
||||
|
||||
Creates a pulse effect loader animation using the `animation-delay` property.
|
||||
|
||||
- Use `@keyframes` to define an animation at two points in the cycle, start (`0%`), where the two `<div>` elements have no `width` or `height` and are positioned at the center and end (`100%`), where both `<div>` elements have increased `width` and `height`, but their `position` is reset to `0`.
|
||||
- Use `@keyframes` to define an animation at two points in the cycle: at the start (`0%`), the two `<div>` elements have no `width` or `height` and are positioned at the center and at the end (`100%`), both `<div>` 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 `<div>` elements a disappearing effect as they expand.
|
||||
- `.ripple-loader`, which is the parent container, has a predefined `width` and `height`. It uses `position: relative` to position its children.
|
||||
- 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 `<div>` element, so that each element starts its animation at a different time.
|
||||
|
||||
```html
|
||||
|
||||
@ -3,17 +3,18 @@ title: Reset all styles
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Resets all styles to default values with one property. This will not affect `direction` and `unicode-bidi` properties.
|
||||
Resets all styles to default values using only one property.
|
||||
|
||||
- The `all` property allows you to reset all styles (inherited or not) to default values.
|
||||
- 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
|
||||
<div class="reset-all-styles">
|
||||
<h5>Title</h5>
|
||||
<p>
|
||||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id exercitationem nulla qui
|
||||
repellat laborum vitae, molestias tempora velit natus. Quas, assumenda nisi. Quisquam enim qui
|
||||
iure, consequatur velit sit?
|
||||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure id
|
||||
exercitationem nulla qui repellat laborum vitae, molestias tempora velit
|
||||
natus. Quas, assumenda nisi. Quisquam enim qui iure, consequatur velit sit?
|
||||
</p>
|
||||
</div>
|
||||
```
|
||||
|
||||
@ -6,17 +6,17 @@ tags: animation,advanced
|
||||
Creates a two sided card which rotates on hover.
|
||||
|
||||
- Set the the `backface-visibility` of the cards to none.
|
||||
- Initially `rotateY` the back side of the card by `-180deg` and the front side to `0deg`.
|
||||
- Upon hover, `rotateY` the front side to `180deg` and backside to `0deg`.
|
||||
- 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
|
||||
<div class="card">
|
||||
<div class="card-side front">
|
||||
<div>Front Side</div>
|
||||
<div>Front Side</div>
|
||||
</div>
|
||||
<div class="card-side back">
|
||||
<div>Back Side</div>
|
||||
<div>Back Side</div>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
@ -29,6 +29,7 @@ Creates a two sided card which rotates on hover.
|
||||
max-width: 400px;
|
||||
margin: 2rem;
|
||||
box-shadow: none;
|
||||
background: none;
|
||||
}
|
||||
|
||||
.card-side {
|
||||
@ -39,7 +40,7 @@ Creates a two sided card which rotates on hover.
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 80%;
|
||||
width: 80%;
|
||||
padding:2rem;
|
||||
color: white
|
||||
}
|
||||
@ -56,10 +57,10 @@ Creates a two sided card which rotates on hover.
|
||||
}
|
||||
|
||||
.card:hover .card-side.front {
|
||||
transform: rotateY(180deg);
|
||||
transform: rotateY(180deg);
|
||||
}
|
||||
|
||||
.card:hover .card-side.back {
|
||||
transform: rotateY(0deg);
|
||||
transform: rotateY(0deg);
|
||||
}
|
||||
```
|
||||
|
||||
@ -3,15 +3,11 @@ title: Shape separator
|
||||
tags: visual,intermediate
|
||||
---
|
||||
|
||||
Uses an SVG shape to separate two different blocks to create more a interesting visual appearance compared to standard horizontal separation.
|
||||
Uses an SVG shape to create a separator between two different blocks.
|
||||
|
||||
- `position: relative` on the element establishes a Cartesian positioning context for pseudo elements.
|
||||
- `:after` defines a pseudo element.
|
||||
- `background-image: url(...)` adds the SVG shape (a 24x12 triangle) as the background image of the pseudo element, which repeats by default. It must be the same color as the block that is being separated. For other shapes, we can use [the URL-encoder for SVG](http://yoksel.github.io/url-encoder/).
|
||||
- `position: absolute` takes the pseudo element out of the flow of the document and positions it in relation to the parent.
|
||||
- `width: 100%` ensures the element stretches the entire width of its parent.
|
||||
- `height: 12px` is the same height as the shape.
|
||||
- `bottom: 0` positions the pseudo element at the bottom of the parent.
|
||||
- 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
|
||||
<div class="shape-separator"></div>
|
||||
@ -21,12 +17,12 @@ Uses an SVG shape to separate two different blocks to create more a interesting
|
||||
.shape-separator {
|
||||
position: relative;
|
||||
height: 48px;
|
||||
background: #333;
|
||||
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='%23fff'/%3E%3C/svg%3E");
|
||||
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;
|
||||
|
||||
@ -5,19 +5,19 @@ tags: interactivity,intermediate
|
||||
|
||||
Fades out the siblings of a hovered item.
|
||||
|
||||
- `transition: opacity 0.2s` specifies that changes to opacity will be transitioned over 0.3 seconds.
|
||||
- `.sibling-fade:hover span:not(:hover)` specifies that when the parent is hovered, select any `span` children that are not currently being hovered and change their opacity to `0.5`.
|
||||
- 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
|
||||
<div class="sibling-fade">
|
||||
<span>Item 1</span> <span>Item 2</span> <span>Item 3</span> <span>Item 4</span>
|
||||
<span>Item 5</span> <span>Item 6</span>
|
||||
<span>Item 1</span> <span>Item 2</span> <span>Item 3</span>
|
||||
<span>Item 4</span> <span>Item 5</span> <span>Item 6</span>
|
||||
</div>
|
||||
```
|
||||
|
||||
```css
|
||||
span {
|
||||
padding: 0 1rem;
|
||||
padding: 0 16px;
|
||||
transition: opacity 0.3s;
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ Creates a staggered animation for the elements of a list.
|
||||
- Set the `opacity` to `0` and `transform` to `translateX(100%)` to make list elements transparent and move them all the way to the right.
|
||||
- Specify the appropriate `transition` properties for list elements, except `transition-delay` which is specified relative to the `--i` custom property.
|
||||
- Use inline styles to specify a value for `--i` for each list element, which will in turn be used for `transition-delay` to create the stagger effect.
|
||||
- Use the `:checked` selector for the checkbox to appropriately style list elements, setting `opacity` to `1` and `transform` to `translateX(0)` to make them appear and slide into view.
|
||||
- Use the `:checked` pseudo-class selector for the checkbox to appropriately style list elements, setting `opacity` to `1` and `transform` to `translateX(0)` to make them appear and slide into view.
|
||||
|
||||
```html
|
||||
<div class="container">
|
||||
|
||||
@ -5,8 +5,8 @@ tags: visual,intermediate
|
||||
|
||||
Creates a list with sticky headings for each section.
|
||||
|
||||
- Use `overflow-y: auto` to allow the list container (`dl`) to overflow vertically.
|
||||
- Set headings (`dt`) `position` to `sticky` and apply `top: 0` to stick to the top of the container.
|
||||
- Use `overflow-y: auto` to allow the list container (`<dl>`) to overflow vertically.
|
||||
- Set headings (`<dt>`) `position` to `sticky` and apply `top: 0` to stick to the top of the container.
|
||||
|
||||
```html
|
||||
<div class="container">
|
||||
|
||||
@ -5,17 +5,18 @@ tags: visual,beginner
|
||||
|
||||
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.
|
||||
- `'Segoe UI'` is used on Windows 10.
|
||||
- `Roboto` is used on Android.
|
||||
- `Oxygen-Sans` is used on Linux with KDE.
|
||||
- `Ubuntu` is used on Ubuntu (all variants).
|
||||
- `Cantarell` is used on Linux with GNOME Shell.
|
||||
- `"Helvetica Neue"` and `Helvetica` is used on macOS 10.10 and below (wrapped in quotes because it has a space).
|
||||
- `'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.
|
||||
- `sans-serif` is the fallback sans serif font if none of the other fonts are supported.
|
||||
|
||||
```html
|
||||
<p class="system-font-stack">This text uses the system font.</p>
|
||||
@ -23,7 +24,8 @@ Uses the native font of the operating system to get close to a native app feel.
|
||||
|
||||
```css
|
||||
.system-font-stack {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu,
|
||||
Cantarell, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
|
||||
Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', Helvetica, Arial,
|
||||
sans-serif;
|
||||
}
|
||||
```
|
||||
|
||||
@ -3,7 +3,7 @@ title: Image text overlay
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Display a text on top of an image using an overlay.
|
||||
Displays a text on top of an image using an overlay.
|
||||
|
||||
- Use `backdrop-filter` to apply a `blur(14px)` and `brightness(80%)` effect to make text readable regardless of background image and color.
|
||||
|
||||
|
||||
@ -3,15 +3,12 @@ title: 3-tile layout
|
||||
tags: layout,beginner
|
||||
---
|
||||
|
||||
Align items horizontally using `display: inline-block` to create a 3-tile layout.
|
||||
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`.
|
||||
- `.tiles` is the container component, `.tile` is an item that needs to be displayed inline.
|
||||
- Use `width: calc((900px / 3))` to divide the width of the container evenly into 3 columns.
|
||||
- Set `font-size: 0;` on `.tiles` to avoid whitespace.
|
||||
- Set `font-size: 20px` to `h2` in order 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.
|
||||
|
||||
- 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 `<h2>` 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
|
||||
<div class="tiles">
|
||||
|
||||
@ -5,17 +5,14 @@ tags: visual,interactivity,beginner
|
||||
|
||||
Creates a toggle switch with CSS only.
|
||||
|
||||
- This effect is styling only the `<label>` element to look like a toggle switch, and hiding the actual `<input>` checkbox by positioning it offscreen. When clicking the label associated with the `<input>` element, it sets the `<input>` checkbox into the `:checked` state.
|
||||
- The `for` attribute associates the `<label>` with the appropriate `<input>` checkbox element by its `id`.
|
||||
- `.switch:after` defines a pseudo-element for the `<label>` to create the circular knob.
|
||||
- `input[type='checkbox']:checked + .switch:after` targets the `<label>`'s pseudo-element's style when the checkbox is `checked`.
|
||||
- `transform: translateX(20px)` moves the pseudo-element (knob) 20px to the right when the checkbox is `checked`.
|
||||
- `background-color: #7983ff;` sets the background-color of the switch to a different color when the checkbox is `checked`.
|
||||
- `.offscreen` moves the `<input>` checkbox element, which does not comprise any part of the actual toggle switch, out of the flow of document and positions it far away from the view, but does not hide it so it is accessible via keyboard and screen readers.
|
||||
- `transition: all 0.3s` specifies all property changes will be transitioned over 0.3 seconds, therefore transitioning the `<label>`'s `background-color` and the pseudo-element's `transform` property when the checkbox is checked.
|
||||
- Use the `for` attribute to associate the `<label>` with the checkbox `<input>` element.
|
||||
- Use the `:after` pseudo-element of the `<label>` to create a circular knob for the switch.
|
||||
- Use the `:checked` pseudo-class selector to change the position of the knob, using `transform: translateX(20px)` and the `background-color` of the switch.
|
||||
- Use `position: absolute` and `left: -9999px` to visually hide the `<input>` element.
|
||||
|
||||
```html
|
||||
<input type="checkbox" id="toggle" class="offscreen" /> <label for="toggle" class="switch"></label>
|
||||
<input type="checkbox" id="toggle" class="offscreen" />
|
||||
<label for="toggle" class="switch"></label>
|
||||
```
|
||||
|
||||
```css
|
||||
|
||||
@ -3,13 +3,12 @@ title: Transform centering
|
||||
tags: layout,beginner
|
||||
---
|
||||
|
||||
Vertically and horizontally centers a child element within its parent element using `position: absolute` and `transform: translate()` (as an alternative to `flexbox` or `display: table`).
|
||||
Similar to `flexbox`, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.
|
||||
Vertically and horizontally centers a child element within its parent element using CSS transforms.
|
||||
|
||||
- `position: absolute` on the child element allows it to be positioned based on its containing block.
|
||||
- `left: 50%` and `top: 50%` offsets the child 50% from the left and top edge of its containing block.
|
||||
- `transform: translate(-50%, -50%)` allows the height and width of the child element to be negated so that it is vertically and horizontally centered.
|
||||
- Note that the fixed height and width on parent element is for the demo only.
|
||||
- Set the `position` of the parent to `relative` and that of the child to `absolute` to place it in relation to its parent.
|
||||
- Use `left: 50%` and `top: 50%` to offset the child 50% from the left and top edge of the containing block.
|
||||
- Use `transform: translate(-50%, -50%)` to negate its position, so that it is vertically and horizontally centered.
|
||||
- **Note:** The fixed `height` and `width` of the parent element is for demonstration purposes only.
|
||||
|
||||
```html
|
||||
<div class="parent">
|
||||
@ -19,7 +18,7 @@ Similar to `flexbox`, this method does not require you to know the height or wid
|
||||
|
||||
```css
|
||||
.parent {
|
||||
border: 1px solid #333;
|
||||
border: 1px solid #9C27B0;
|
||||
height: 250px;
|
||||
position: relative;
|
||||
width: 250px;
|
||||
|
||||
@ -3,10 +3,12 @@ title: Triangle
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Creates a triangle shape with pure CSS.
|
||||
Creates a trinagular shape with pure CSS.
|
||||
|
||||
- The color of the border is the color of the triangle. The side the triangle tip points corresponds to the opposite `border-*` property. For example, a color on `border-top` means the arrow points downward.
|
||||
- Experiment with the `px` values to change the proportion of the triangle.
|
||||
- Use three borders to create a triangle shape.
|
||||
- All borders should have the same `border-width` (`20px`).
|
||||
- The opposite side of where the triangle points towards (i.e. top if the triangle points downwards) should have the desired `border-color`, whereas the adjacent borders (i.e. left and right) should have a `border-color` of `transparent`.
|
||||
- Altering the `border-width` values will change the proportions of the triangle.
|
||||
|
||||
```html
|
||||
<div class="triangle"></div>
|
||||
@ -16,7 +18,7 @@ Creates a triangle shape with pure CSS.
|
||||
.triangle {
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-top: 20px solid #333;
|
||||
border-top: 20px solid #9C27B0;
|
||||
border-left: 20px solid transparent;
|
||||
border-right: 20px solid transparent;
|
||||
}
|
||||
|
||||
@ -1,25 +1,26 @@
|
||||
---
|
||||
title: Truncate text multiline
|
||||
title: Truncate multiline text
|
||||
tags: layout,intermediate
|
||||
---
|
||||
|
||||
If the text is longer than one line, it will be truncated for `n` lines and end with an gradient fade.
|
||||
Truncates text that is longer than one line.
|
||||
|
||||
- `overflow: hidden` prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
|
||||
- `width: 400px` ensures the element has a dimension.
|
||||
- `height: 109.2px` calculated value for height, it equals `font-size * line-height * numberOfLines` (in this case `26 * 1.4 * 3 = 109.2`).
|
||||
- `height: 36.4px` calculated value for gradient container, it equals `font-size * line-height` (in this case `26 * 1.4 = 36.4`).
|
||||
- `background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%)` gradient from `transparent` to `#f5f6f9`.
|
||||
- Use `overflow: hidden` to prevent the text from overflowing its dimensions.
|
||||
- Set a fixed `width` to ensure the element has at least one constant dimension.
|
||||
- Set `height: 109.2px` as calculated from the `font-size`, using the formule `font-size * line-height * numberOfLines` (in this case `26 * 1.4 * 3 = 109.2`).
|
||||
- Set `height: 36.4px` as calculated for the gradient container, based on the formula `font-size * line-height` (in this case `26 * 1.4 = 36.4`).
|
||||
- Use `background` with `linear-gradient()` to create a gradient from `transparent` to the `background-color`.
|
||||
|
||||
```html
|
||||
<p class="truncate-text-multiline">
|
||||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
|
||||
labore et.
|
||||
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
|
||||
eirmod tempor invidunt ut labore et.
|
||||
</p>
|
||||
```
|
||||
|
||||
```css
|
||||
.truncate-text-multiline {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
display: block;
|
||||
height: 109.2px;
|
||||
@ -27,7 +28,8 @@ If the text is longer than one line, it will be truncated for `n` lines and end
|
||||
font-size: 26px;
|
||||
line-height: 1.4;
|
||||
width: 400px;
|
||||
position: relative;
|
||||
background: #f5f6f9;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.truncate-text-multiline:after {
|
||||
|
||||
@ -3,12 +3,12 @@ title: Truncate text
|
||||
tags: layout,beginner
|
||||
---
|
||||
|
||||
If the text is longer than one line, it will be truncated and end with an ellipsis `…`.
|
||||
Truncates text that is longer than one line, adding an ellipsis at the end (`…`).
|
||||
|
||||
- `overflow: hidden` prevents the text from overflowing its dimensions (for a block, 100% width and auto height).
|
||||
- `white-space: nowrap` prevents the text from exceeding one line in height.
|
||||
- `text-overflow: ellipsis` makes it so that if the text exceeds its dimensions, it will end with an ellipsis.
|
||||
- `width: 200px;` ensures the element has a dimension, to know when to get ellipsis
|
||||
- Use `overflow: hidden` to prevent the text from overflowing its dimensions.
|
||||
- Use `white-space: nowrap` to prevent the text from exceeding one line in height.
|
||||
- Use `text-overflow: ellipsis` to make it so that if the text exceeds its dimensions, it will end with an ellipsis.
|
||||
- Specify a fixed `width` for the element to know when to display an ellipsis.
|
||||
- Only works for single line elements.
|
||||
|
||||
```html
|
||||
|
||||
@ -3,10 +3,10 @@ title: Zebra striped list
|
||||
tags: visual,beginner
|
||||
---
|
||||
|
||||
Creates a striped list with alternating background colors, which is useful for differentiating siblings that have content spread across a wide row.
|
||||
Creates a striped list with alternating background colors.
|
||||
|
||||
- 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.
|
||||
- Use the `:nth-child(odd)` or `:nth-child(even)` pseudo-class selectors to apply a different `background-color` to elements that match based on their position in a group of siblings.
|
||||
- **Note:** You can use it to apply different styles to other HTML elements like `<div>`, `<tr>`, `<p>`, `<ol>`, etc.
|
||||
|
||||
```html
|
||||
<ul>
|
||||
@ -20,6 +20,6 @@ Creates a striped list with alternating background colors, which is useful for d
|
||||
|
||||
```css
|
||||
li:nth-child(odd) {
|
||||
background-color: #ddd;
|
||||
background-color: #999;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user