CSS 1: Animation, gradients and buttons effects

Syntax and properties

CSS Display and Google fonts

To implement a CSS file into our HTML we:

link + tab //will result in

<link rel="stylesheet" type="text/css" href="style.css">

CSS stands for Cascading style sheets, and can be implemented in:

-external
<link rel="stylesheet" type="text/css" href="style.css">
(rel stands for relationship)

-internal
Selectors {
  properties: values !important;
}

-inline
<p style="color:red"> indeed </p>

id# > class. > tag

To implement Google Fonts in our CSS file we:

Selectors mirror HTML tags for priority ( tag > id# > .class ).

Here are some Inline, Inline-block, and block:

Even if used a div tag, we overwrite it with CSS

display: block will occupy the entire line and accept width/height, with the tags div, h1, p, li:

Even if used the span tag, we used block in the CSS

Inline elements will share the same line between them and won't take the full width of their containers.

The vertical-align property aligns inline and inline-block elements relative to their parent container's line-height/linebox, for a more examples check Parallax.

chevron-rightVertical-align css on Inline-block elementshashtag

By default, inline child elements align to the baseline, but we can use line-height: normal and vertical-align to change it.

Inline-block elements are easier to align due to their fixed height and width.

vertical-align inline-block elements

CSS Selectors and Media Query

We use it to select HTML elements to style with CSS properties:

Interactive CSS selector exercise
chevron-rightList of CSS selectorshashtag

A media query consists of a media type that adapts elements to different screens:

CSS selectors inherit their properties, any changes are limited to the media query.

Also, more specific selectors will take priority, including the media query ones:

CSS transition and Animation

The transform property allows us to move, rotate, scale, and skew HTML elements:

Width scale(1.2) and height 0.6, skew(30deg) and translate(100px, 200px)
chevron-rightTransform property guidehashtag

The translate() method moves the element's current position by X/Y:

The rotate() method rotates the element clock/anti-clockwise in degrees:

The scale() method increases or decreases the width/height of an element:

The skew() method skews elements on their X/Y axis by degrees:

Both translateX and left move the element, but translateX doesn't change the CSS layout, it won't trigger a reflow(positioning) and repaint(element placing), for faster DOM render.

chevron-rightTranslateX and left useSpring() animationhashtag

Left needs its position to be declared to work, both will move the same distance.

Position-absolute translateX will move 20% of its content, while relative 20% of its container.

CSS transition

The transition property allows us to change CSS values over a duration.

chevron-rightTransition property guidehashtag

The transition property covers any changes the selectors receive, after an event:

You will need a transition-property when different durations for properties:

We can change the timing function at each stage of the transition:

We can delay the transition between the event and the property change:

Transition + Transform CSS property

We can add the transition effect to a transformation method.

CSS animation property

This property allows us to control every step of a CSS animation with keyframe rule:

chevron-rightAnimation property guidehashtag

An animation requires the name of its keyframe rule and its duration.

The animation-delay specifies a delay for it to start:

The animation-iteration-count for how many times it should run, (can be infinite):

We use animation-direction to choose in which order the keyframe will run:

The animation-timing-function property is the same as transition, including steps():

The animation-fill-mode property sets the properties that are gonna be retained at the end of the animation:

We can use the animation-timing-function steps() to use image frames.

chevron-rightsteps() animationhashtag

We move the pixel images as background, on a 1-frame width window:

Example of it in action with pixel art

Check React-spring-1 to see how to pixel easings: steps() animate with useSpring().

in the next example we will use transform-origin:

Starting point from which the transform starts, Internal to the element, and center/50% 50% by default:

chevron-rightSteps(<direction>) and animation-fill-modehashtag

For single-dimension elements we need only 1 Transform-origin keyword:

The steps(start/end) sets the starting frame, while the forwards/backwards the frame to have at the end.

Steps(start) will "skip" the first frame, while completely ending, while steps(end) will start from the very first frame but end 1 frame before

steps(start) and steps(end)
chevron-rightTypewriter effect usin transition and steps() animationhashtag

We create a text that gets visible on hover and includes a typing effect for each letter:

The animated part being:

typewriter effect gif (kinda)

Clock CSS Designs and JS-based animation

We start with a basic CSS clock design:

chevron-rightClock design and animationhashtag

HTML and basic clock is:

For the tri-color center, they overlay the same space, so they need position: absolute :

For the clock hands we:

In the animation we include translateY() for the center/hand intersect:

animated clock hands with center intersection

For the next part, we need the insertAdjacentElement()arrow-up-right method ;

chevron-rightAdded clock ticks with javascripthashtag

We first design the CSS ticks in couples:

Now for the HTML and Js implementation:

We insertAdjacentElement() the created <style> tag with the 30 CSS selectors right into(beforeend) the <head> of the HTML document:

Js implemented design
chevron-rightAnimating the clock hands for javascript current timehashtag

We need to set the animation duration for a 360deg clock rotation:

On the Js file, we only set the starting current time and let the animation run its course :

We can change the animation-timing-function from steps() to linear if we want gradual movement, etc,

Now we are gonna add extra CSS animated elements to the clock:

chevron-rightPendolum animated effecthashtag

We get the bottom part with a box-shadow:

We can animate both with same keyframes:

animated pendolum CSS added

Here are more examples of how linear-gradient degree rotation works:

bottom, left, top, and right

For an overview of how CSS gradients work, check HE.

Grany gradient, Clip-path(), smoke and scrolling animation

By using a noise image in the gradient background, we can:

chevron-rightGrany gradient guidehashtag

We use transparent in gradient and noise background-image:

For a more visible grany effect we use filter(), more contrast() for more color space, and more brightness() for more grains visible:

We overlay by putting the gradient below the color:

raw gradient, filter and overlayed colors

To use grany radial gradients as an overlay:

chevron-rightGrany radial-gradient() guidehashtag

For the HTML we need 2 containers:

We need a relative position background to move internal layers:

While the 2 absolute layers:

Radial gradient on single layer

We can create a 3D shadow with a double radial-gradient():

chevron-rightDouble gradient layer hashtag

In the HTML, remember the order of the shadow tags:

We don't need text and the sphere to overlay, position relative:

The external shadow will be:

For the sphere and its internal shadow we:

We can use mix-blend-mode and filters to create animated text gradient effect:

chevron-rightGradient invisible effect guidehashtag

We will need an extra HTML tag:

For the text color, we use background-clip and color: invisible to use the background gradient() as text color:

For the mix-blend background, we use multiply/screen to show the dark/white colors:

invisible gradient tet effect

For multiple grany gradient backgrounds effect:

chevron-rightMultiple radial gradient guidehashtag

To avoid having our transparent be mixed with other background colors, we isolate:

We leave an empty space at the center of the first layer with the color-stop at 8%, remember that, the difference between color stops makes the grany more visible:

We use a partial-transparency for the blue center:

double grany radial gradient CSS

Animated smoke CSS effect

We are gonna animate a list of layers in a different order:

chevron-rightCSS smoke guidehashtag

For the absolute layers, we need a relative container.

We style each smokeball:

To create a spread smoke effect we use 2 animations:

And we also add a different delay to each smokeball:

For the actual animated frames:

smoke effect

CSS and JS scrolling timer effect

The HTML structure of the clock is gonna be:

chevron-rightScrolling effect JS animationhashtag

On the script.js file we get the Date() object for the timezone, and we convert the time numbers to string data:

We split() the time string into an array of 2, and we unshift() a 0 in case single digit.

We separate the first/last child of the container and update if the current time value differs (with <div> and value);

We create/clone the same HTML element with updated time, append it (below) to the current HTML, and animate it.

After the animation is complete we remove the CSS and remove the old element.firstElementChild with the new/cloned one:

The CSS animations being:

scrolling clock js animation

Custom popup, checklist, expandable and buttons effects

We use first-of-type pseudo selector and attr() CSS content property:

chevron-rightCapital letter and Popup text guidehashtag

The popup attr() content is gonna be contained in the tooltip-data:

For the capital we use first-of-type::first-letter:

After getting the HTML content attribute, we use before:after :

We use opacity to make it invisible, but also visibility:hidden/visible or display:none/inline-block for the transition effect:

Capital letter and popup on hover

CSS custom checklist

We can display:none the <input> and still have a checkbox:

chevron-rightCSS custom checklist guidehashtag

In the HTML we create the <label> for the <input> tag:

We can use border-bottom and underline:

For the actual input:

For the transition effect we:

Expandable text and animated sidebars.

We use the <details> tag and absolute+hover transition:

chevron-rightExpandable and sidebars guidehashtag

In the HTML the <detail> tag includes a triangle icon near <summary> text, it also adds the attribute open to the tag if opened:

We add another -/+ icon to the right side of <summary>:

For a fade-in effect of the expandables:

For the relative sidebar, we overflow:hidden and absolute right: 100% before the hover:

On hover we move the menu visible again:

Expandable and sidenav on hover

CSS :before:after effects

We can see a gallery of CSS transition effects here:

A gallery of CSS effects

Last updated