Chapter 7. CSS Functions

CSS functions are used for many things in CSS. They can be used to create special types of processes such as creating animations or use custom fonts, or create visual effects like transparencies or transforming elements in both two-dimensional and three-dimensional planes.

Let's see what CSS functions are all about.

Filter

CSS filters allow us to manipulate the color of an element in different ways.

brightness()

The brightness() CSS function is used with the filter property, and it looks like this:

filter: brightness(20%);

Description

The brightness() function modifies the illumination of an image. Values are declared as either a percentage or a number without a unit, for example, 10% and 0.5%

A value of 100% leaves the element unchanged; a value of 0% makes the element completely black. Values over 100% are allowed and create a more intense effect. There is no limit to the value.

A value of 1 leaves the element unchanged; a value of 0 makes the element completely black. Values over 1 are allowed and create a more intense effect. There is no limit to the value. Also, negative values are not valid for either the percentage of the number.

CSS:

.element {
  filter: brightness(20%);
}

contrast()

The contrast() CSS function is used with the filter property, and it looks like this:

filter: contrast(10);

Description

The contrast() function modifies the contrast of an element. Values are declared as either a percentage or a number without a unit, for example, 10% and 0.5%.

A value of 100% leaves the element unchanged; a value of 0% makes the element completely black. Values over 100% are allowed and create a more intense effect. There is no limit to the value.

A value of 1 leaves the element unchanged; a value of 0 makes the element completely black. Values over 1 are allowed and create a more intense effect. There is no limit to the value. Also, negative values are not valid and decimal values are allowed for both.

CSS:

.element {
  filter: contrast(10);
}

grayscale()

The grayscale() CSS function is used with the filter property, and it looks like this:

filter: grayscale(.8);

Description

The grayscale() function converts an element to shades of black. Values are declared as either a percentage or a number without a unit, for example, 10% and 0.5%.

A value of 0% leaves the element unchanged; a value of 100% makes the element grayscale. Values over 100% are not allowed.

A value of 0 leaves the element unchanged; a value of 1 makes the element grayscale. Values over 1 are not allowed. Also, negative values are not valid for either. Decimal values are allowed

invert()

The invert() CSS function is used with the filter property, and it looks like this:

filter: invert(1);

Description

The invert() function inverts the color of the element. If used in an image, it makes the image look like a film negative.

A value of 100% completely inverts the element's color; a value of 0% leaves the element unchanged. Values over 100% are not allowed.

A value of 1 completely inverts the element's color; a value of 0 leaves the element unchanged. Values over 1 are not allowed. Also, negative values are not valid. Decimal values are allowed for both.

hue-rotate()

The hue-rotate() CSS function is used with the filter property, and it looks like this:

filter: hue-rotate(80deg);

Description

The hue-rotate() function applies a hue rotation to the element. It accepts an angle value.

The angle value defines the degrees around the color wheel that the element sample will be modified to.

There is no maximum value. However, if the value is larger than 360deg, the rotation will just go around. For example, if we declare 380deg, that would the same as 20deg.

blur()

The blur() CSS function is used with the filter property, and it looks like this:

filter: blur(10px);

Description

The blur() function gives the smudge effect. Values are declared as length values (px, em, in, mm, cm, vw and so on). The higher the value, the more intense the blur effect is, and vice versa.

Percentage and negative values are not allowed, but decimal values are.

saturate()

The saturate() CSS function is used with the filter property, and it looks like this:

filter: saturate(300%);

Description

It affects the saturation levels of an element. Values are declared as either a percentage or a number without a unit, for example, 10% and 0.5%

The default saturation value of an element is 100%, or 1 if using a unitless number.

A value of 0% completely desaturates the element (it removes all color leaving the element in grayscale); a value of 100% leaves the element unchanged. Values over 100% are allowed creating a more intense effect.

A value of 0 completely desaturates the element (it removes all color leaving the element in grayscale); a value of 1 leaves the element unchanged. Values over 1 are allowed creating a more intense effect.

sepia()

The sepia() CSS function is used with the filter property, and it looks like this:

filter: sepia(100%);

Description

The sepia() function converts an element to sepia. Think of a grayscale image but in shades of brown.

A value of 100% completely turns the element to sepia; a value of 0% leaves the element unchanged. Values over 100% are not allowed.

A value of 1 completely turns the element to sepia; a value of 0 leaves the element unchanged. Values over 1 are not allowed.

Also, for both, negative values are not valid.

Transforms

CSS transforms have gained such popularity that it's rare not to see some sort of transformation in a website nowadays, for example, button shapes, animations, and layouts.

Let's see the transformation CSS functions.

matrix()

The matrix() CSS function is used with the transform property, and it looks like this:

matrix(0.5, 0, 0.0881635, 0.5, 0, 0);

Description

The matrix() function is the shorthand for all transformation properties, since they can be combined here. This function is used to define a two-dimensional transformation matrix.

This function requires a solid understanding of math, but in reality this function isn't something to be done by hand. Instead, we can use a tool like Eric Meyer's and Aaron Gustafson's The Matrix Resolutions (http://tiny.cc/eric-meyer-matrix).

The explanation of the advanced mathematics of the matrix() function are beyond the scope of this book. However, for very detailed explanations you can refer to any of these two articles:

CSS:

/*This*/
.element { transform: skew(10deg) scale(.5); }
/*Is the same as this*/
.element { transform: matrix(0.5, 0, 0.0881635, 0.5, 0, 0); }

matrix3d()

The matrix3d() CSS function is used with the transform property, and it looks like this:

matrix3d(0.852825, 0.195593, -0.484183, 0, 0.0958426, 0.852825, 0.513326, 0, 0.513326, -0.484183, 0.708564, 0, 0.948667, 1.04842, 0.0291436, 1);

Description

Just like the two-dimensional matrix() function, the matrix3d() function is a shorthand, but this one is for all transform 3D properties in a 4 x 4 grid.

This function requires a solid understanding of math, but in reality this function isn't something to be done by hand. Instead, we can use a tool like Eric Meyer and Aaron Gustafson's The Matrix Resolutions (http://tiny.cc/eric-meyer-matrix).

CSS:

/*This*/
.element { transform: rotate3d(10, 10, 1, 45deg) translate3d(1px, 1px, 0); }
/*Is the same as this*/
.element { transform: matrix3d(0.852825, 0.195593, -0.484183, 0, 0.0958426, 0.852825, 0.513326, 0, 0.513326, -0.484183, 0.708564, 0, 0.948667, 1.04842, 0.0291436, 1); }

rotate()

The rotate() CSS function is used with the transform property, and it looks like this:

rotate(10deg);

Description

The rotate() function rotates an element around a fixed point in a two-dimensional space. It accepts an angle value using the deg, grad, rad, or turn units. The deg unit is most commonly used. Negative values are valid.

CSS:

.element {
  transform: rotate(10deg);
}

rotate3d()

The rotate3d() CSS function is used with the transform property, and it looks like this:

rotate3d(1, 2, 1, 10deg);

Description

The rotate3d() function rotates an element around a fixed position in a three-dimensional plane via the X, Y, and Z axes. It accepts four values: three unitless number values that correspond to the X, Y, and Z axes, and an angle value that defines the amount of rotation.

Positive values rotate the element clockwise in the corresponding axis. Negative values rotate the element counter-clockwise.

CSS:

.element {
  transform: rotate3d(1, 2, .5, 10deg);
}

rotateX()

The rotateX() CSS function is used with the transform property, and it looks like this:

transform: rotateX(25deg);

The previous code is similar to the following code:

transform: rotate3d(1, 0, 0, 25deg);

Description

The rotateX() function rotates an element on the X axes in a three-dimensional plane. It accepts an angle value.

Positive values rotate the element clockwise. Negative values rotate the element counter-clockwise.

CSS:

/*This*/
.element { transform: rotateX(25deg); }
/*Is the same as this*/
.element { transform: rotate3d(1, 0, 0, 25deg); }

rotateY()

The rotateY() CSS function is used with the transform property, and it looks like this:

transform: rotateY(75deg);

The previous line is the same as this:

transform: rotate3d(0, 1, 0, 75deg);

Description

The rotateY() function rotates an element on the Y axes in a three-dimensional plane. It accepts an angle value.

Positive values rotate the element clockwise. Negative values rotate the element counter-clockwise.

CSS:

/*This*/
.element { transform: rotateY(75deg); }
/*Is the same as this*/
.element { transform: rotate3d(0, 1, 0, 75deg); }

rotateZ()

The rotateY() CSS function is used with the transform property, and it looks like this:

transform: rotateZ(33deg);

Which is the same as this:

transform: rotate3d(0, 0, 1, 33deg);

Description

The rotateY() function rotates an element on the Z axes in a three-dimensional plane. It accepts an angle value.

Positive values rotate the element clockwise. Negative values rotate the element counter-clockwise.

CSS:

/*This*/
.element { transform: rotateZ(33deg); }
/*Is the same as this*/
.element { transform: rotate3d(0, 0, 1, 33deg); }

scale()

The scale() CSS function is used with the transform property, and it looks like this:

.element { transform: scale(2); }

Or:

.element { transform: scale(2, 3); }

Description

The scale() function changes the size of an element in a two-dimensional plane to make it larger or smaller. It supports one or two unitless number values, where the second value is optional. The number indicates the number of times the element should be scaled. For example, a value of 2 means the element is scaled (enlarged) 200%; a value of 0.5 means the element should be scaled (reduced) to 50%.

The first value represents a horizontal scale and the second a vertical scale. If a single value is declared, it means that both orientations will use the same value.

Negative values are allowed. However, when negative values are used, the element is flipped.

Tip

When an element is scaled, it does not affect the layout; it will simply overlap or appear below other elements depending on the source order.

CSS:

/*Element is flipped in both directions and scaled to 200% its size*/
.element { transform: scale(-2); }
/*Element is scaled to 200% horizontally and 300% vertically*/
.element { transform: scale(2, 3); }

scale3d()

The scaled3d() CSS function is used with the transform property, and it looks like this:

transform: scale3d(2, 2, 2);

Description

The scaled3d() function changes the size of an element in a three-dimensional plane via the X, Y, and Z axes to make it larger or smaller.

It supports three unitless number values which are required. Negative values are allowed. However, when negative values are used, the element is flipped.

CSS:

/*The element is twice the size*/
.element { transform: scale3d(2, 2, 2); }
/*Flipped element in both X and Y-axis while scaled to 300%, and 200% on the Z-axis*/
.element { transform: scale3d(-3, -3, 2); }

scaleX()

The scaleX() CSS function is used with the transform property, and it looks like this:

transform: scaleX(-2);

Description

The scaleX() function changes the size of an element on the X axes in a two-dimensional plane. It supports a unitless number value.

Negative values are allowed. However, the element is flipped when negative values are used.

CSS:

.element {
  transform: scaleX(-2);
}

scaleY()

The scaleY() CSS function is used with the transform property, and it looks like this:

transform: scaleY(4);

Description

The scaleY() function changes the size of an element on the Y axes in a two-dimensional plane. It supports a unitless number value.

Negative values are allowed. However, the element is flipped when negative values are used.

CSS:

.element {
  transform: scaleY(4);
}

scaleZ()

The scaleZ() CSS function is used with the transform property, and it looks like this:

transform: scaleZ(3);

Description

The scaleZ() function changes the size of an element on the Y axes in a two-dimensional plane. It supports a unitless number value.

Negative values are allowed. However, the element is flipped when negative values are used.

CSS:

.element {
  transform: scaleY(4);
}

skew()

The skew() CSS function is used with the transform property, and it looks like this:

transform: skew(20deg);

Or you can also use the following code:

transform: skew(20deg, 25deg);

Description

The skew() function skews or tilts an element on the X axes or both the X and the Y axes on a two-dimensional plane. For example, a parallelogram is a skewed rectangle.

It supports one or two angle values: the first one corresponds to the X axes and the second one to the Y axes. If only one value is declared, the element is skewed only on the X axes. Negative values are allowed.

It's recommended that you use either the skewX() or skewY() functions rather than skew(), because skew() has been removed from the spec (although most browsers still support it).

CSS:

/*One value only affects the element on the X-axis*/
.element { transform: skew(20deg); }
/*Two values affects the element on both X and Y-axis*/
.element { transform: skew(20deg, 25deg); }

skewX()

The @skewX() CSS function is used with the transform property, and it looks like this:

transform: skewX(40deg);

Description

The @skewX() function skews or tilts an element on the X axes on a two-dimensional plane.

It supports one angle value. Negative values are allowed.

CSS:

.element {
  transform: skewX(40deg);
}

skewY()

The @skewY() CSS function is used with the transform property, and it looks like this:

transform: skewY(36deg);

Description

The @skewY() function skews or tilts an element on the Y axes in a two-dimensional plane.

It supports one angle value. Negative values are allowed.

CSS:

.element {
  transform: skewY(36deg);
}

steps()

The steps() timing function is used with the transition-timing-function or the animation-timing-function properties, and it looks like this:

transition-timing-function: steps(3);
animation-timing-function: steps(3);

Description

The steps() timing function divides the transition or the animation into intervals of equal sizes. We can also specify if the steps of transition or animation happen at the start or the end of the interval. The end value is the default in case no argument is declared.

It supports one numeric value, or one numeric value and an optional value of either start or end.

The best way to understand how start or end works is with an example: the animation will begin right away when using start, and it will be delayed a bit when using end.

CSS:

/*The transition is divided in 3 equal size intervals*/
.element { transition-timing-function: steps(3); }
/*The transition is divided in 3 equal size intervals but it will delay a bit before it starts*/
.element { transition-timing-function: steps(3, end); }

translate()

The translate() CSS function is used with the transform property, and it looks like this:

transform: translate(20px);

Or like this:

transform: translate(20px, -50%);

Description

The translate() function affects the position of an element on the X axes or both the X and the Y axes on a two-dimensional plane.

It supports both length and percentage values. Negative values are allowed. It supports one or two length and percentage values; the first one corresponds to the X-axis and the second one to the Y-axis. If only one value is declared, the element is moved only on the X-axis. Negative values are allowed.

CSS:

/*One value, the element is moved on the X-axis only*/
.element { transform: translate(20px); }
/*Two values, the element is moved on the X and Y-axis*/
.element { transform: translate(20px, -50%); }

translate3d()

The translate3d() CSS function is used with the transform property and the perspective function, and it looks like this:

transform: perspective(100px)  translate3d(75px, 75px, -200px);

Description

The translate3d() function is used to move an element on the X, Y, and Z axes on a three-dimensional plane.

It supports both length and percentage values. Negative values are allowed.

In order to be able to see this function work, we need to give the element in question a three-dimensional plane with the perspective function, otherwise the translate3d() declaration will have no effect.

CSS:

.element {
  transform: perspective(100px) translate3d(75px, 75px, -200px);
}

translateX()

The translateX() CSS function is used with the transform property, and it looks like this:

transform: translateX(99%);

Description

The translateX() function is used to move an element on the X axes in a two-dimensional plane.

It supports both length and percentage values. Negative values are allowed.

CSS:

.element {
  transform: translateX(99%);
}

translateY()

The translateY() CSS function is used with the transform property, and it looks like this:

transform: translateY(55px);

Description

This is used to move an element on the Y axes in a two-dimensional plane. It supports both length and percentage values. Negative values are allowed.

CSS:

.element {
  transform: translateY(55px);
}

translateZ()

The translateZ() CSS function is used with the transform property and with the perspective function, and it looks like this:

transform: perspective(100px) translateZ(77px);

Description

This is used to move an element on the Z axes on a three-dimensional plane. It supports both length and percentage values. Negative values are allowed.

In order to be able to see this function work, we need to give the element in question a three-dimensional plane with the perspective function; otherwise, the translateZ() declaration will have no effect.

CSS:

.element {
  transform: perspective(100px) translateZ(77px);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.44.53