Blog

  • CSS3 3D Transforms

    3D Transformation of Elements

    With CSS3 3D transform feature you can perform basic transform manipulations such as move, rotate, scale and skew on elements in a three-dimensional space.

    A transformed element doesn’t affect the surrounding elements, but can overlap them, just like the absolutely positioned elements. However, the transformed element still takes space in the layout at its default (un-transformed) location.

    Using CSS Transform and Transform Functions

    The CSS3 transform property uses the transform functions to manipulate the coordinate system used by an element in order to apply the transformation effect.

    The following section describes the 3D transform functions:

    The translate3d() Function

    Moves the element from its current position to a new position along the X, Y and Z-axis. This can be written as translate(tx, ty, tz). Percentage values are not allowed for third translation-value parameter (i.e. tz).

    Example

    .container {
    
    width: 125px;
    height: 125px;
    perspective: 500px;
    border: 4px solid #e5a043;
    background: #fff2dd;
    } .transformed {
    -webkit-transform: translate3d(25px, 25px, 50px); /* Chrome, Safari, Opera */
    transform: translate3d(25px, 25px, 50px); /* Standard syntax */
    }

    The function translate3d(25px, 25px, 50px) moves the image 25 pixels along the positive X and Y-axis, and the 50 pixels along the positive Z-axis.

    CSS3 3D Translation Demo

    The 3D transform however uses the three-dimensional coordinate system, but movement along the Z-direction is not always noticeable because the elements exist on a two-dimensional plane (a flat surface) and have no depth.

    The perspective and perspective-origin CSS properties can be used to add a feeling of depth to a scene by making the elements higher on the Z-axis i.e. closer to the viewer appear larger, and those further away to the viewer appear smaller.

    Note: If you apply 3D transformation to an element without setting the perspective the resulting effect will not appear as three-dimensional.


    The rotate3d() Function

    The rotate3d() function rotates the element in 3D space by the specified angle around the [x,y,z] direction vector. This can be written as rotate(vx, vy, vz, angle).

    Example

    .container{
    
    width: 125px;
    height: 125px;
    perspective: 300px;
    border: 4px solid #a2b058;
    background: #f0f5d8;
    } .transformed {
    -webkit-transform: rotate3d(0, 1, 0, 60deg); /* Chrome, Safari, Opera */
    transform: rotate3d(0, 1, 0, 60deg); /* Standard syntax */
    }

    The function rotate3d(0, 1, 0, 60deg) rotates the image along the Y-axis by the angle 60 degrees. You can use negative values to rotate the element in opposite direction.

    CSS3 3D Rotation Demo

    The scale3d() Function

    The scale3d() function changes the size of an element. It can be written as scale(sx, sy, sz). The effect of this function is not evident unless you use it in combination with other transform functions such as rotate and the perspective, as shown in the example below.

    Example

    .container{
    
    width: 125px;
    height: 125px;
    perspective: 300px;
    border: 4px solid #6486ab;
    background: #e9eef3;
    } .transformed {
    -webkit-transform: scale3d(1, 1, 2) rotate3d(1, 0, 0, 60deg); /* Chrome, Safari, Opera */
    transform: scale3d(1, 1, 2) rotate3d(1, 0, 0, 60deg); /* Standard syntax */
    }

    The function scale3d(1, 1, 2) scales the elements along the Z-axis and the function rotate3d(1, 0, 0, 60deg) rotates the image along the X-axis by the angle 60 degrees.

    CSS3 3D Scale Demo

    The matrix3d() Function

    The matrix3d() function can perform all of the 3D transformations such as translate, rotate, and scale at once. It takes 16 parameters in the form of a 4×4 transformation matrix.

    Here is an example of performing the 3D transformation using the matrix3d() function.

    Example

    .container{
    
    width: 125px;
    height: 125px;
    perspective: 300px;
    border: 4px solid #d14e46;
    background: #fddddb;
    } .transformed {
    -webkit-transform: matrix3d(0.359127, -0.469472, 0.806613, 0, 0.190951, 0.882948, 0.428884, 0, -0.913545, 0, 0.406737, 0, 0, 0, 0, 1); /* Chrome, Safari, Opera */
    transform: matrix3d(0.359127, -0.469472, 0.806613, 0, 0.190951, 0.882948, 0.428884, 0, -0.913545, 0, 0.406737, 0, 0, 0, 0, 1); /* Standard syntax */
    }

    However, when performing more than one transformation at once, it is more convenient to use the individual transformation function and list them in order, like this:

    Example

    .container{
    
    width: 125px;
    height: 125px;
    perspective: 300px;
    border: 4px solid #a2b058;
    background: #f0f5d8;
    } .transformed {
    -webkit-transform: translate3d(0, 0, 60px) rotate3d(0, 1, 0, -60deg) scale3d(1, 1, 2); /* Chrome, Safari, Opera */
    transform: translate3d(0, 0, 60px) rotate3d(0, 1, 0, -60deg) scale3d(1, 1, 2); /* Standard syntax */
    }

    3D Transform Functions

    The following table provides a brief overview of all the 3D transformation functions.

    FunctionDescription
    translate3d(tx,ty,tz)Moves the element by the given amount along the X, Y & Z-axis.
    translateX(tx)Moves the element by the given amount along the X-axis.
    translateY(ty)Moves the element by the given amount along the Y-axis.
    translateZ(tz)Moves the element by the given amount along the Z-axis.
    rotate3d(x,y,z, a)Rotates the element in 3D space by the angle specified in the last parameter, around the [x,y,z] direction vector.
    rotateX(a)Rotates the element by the given angle around the X-axis.
    rotateY(a)Rotates the element by the given angle around the Y-axis.
    rotateZ(a)Rotates the element by the given angle around the Z-axis.
    scale3d(sx,sy,sz)Scales the element by the given amount along the X, Y and Z-axis. The function scale3d(1,1,1) has no effect.
    scaleX(sx)Scales the element along the X-axis.
    scaleY(sy)Scales the element along the Y-axis.
    scaleZ(sz)Scales the element along the Z-axis.
    matrix3d(n,n,n,n,n,n, n,n,n,n,n,n,n,n,n,n)Specifies a 3D transformation in the form of a 4×4 transformation matrix of 16 values.
    perspective(length)Defines a perspective view for a 3D transformed element. In general, as the value of this function increases, the element will appear further away from the viewer.
  • CSS3 2D Transforms

    2D Transformation of Elements

    With CSS3 2D transform feature you can perform basic transform manipulations such as move, rotate, scale and skew on elements in a two-dimensional space.

    A transformed element doesn’t affect the surrounding elements, but can overlap them, just like the absolutely positioned elements. However, the transformed element still takes space in the layout at its default (un-transformed) location.

    Using CSS Transform and Transform Functions

    The CSS3 transform property uses the transform functions to manipulate the coordinate system used by an element in order to apply the transformation effect.

    The following section describes these transform functions:

    The translate() Function

    Moves the element from its current position to a new position along the X and Y axes. This can be written as translate(tx, ty). If ty isn’t specified, its value is assumed to be zero.

    Example

    img {
    
    -webkit-transform: translate(200px, 50px);  /* Chrome, Safari, Opera */
       -moz-transform: translate(200px, 50px);  /* Firefox */
        -ms-transform: translate(200px, 50px);  /* IE 9 */
            transform: translate(200px, 50px);  /* Standard syntax */    
    }

    The function translate(200px, 50px) moves the image 200 pixels horizontally along the positive x-axis, and 50 pixels vertically along the positive y-axis.

    CSS3 Translate Demo

    The rotate() Function

    The rotate() function rotates the element around its origin (as specified by the transform-origin property) by the specified angle. This can be written as rotate(a).

    Example

    img {
    
    -webkit-transform: rotate(30deg);  /* Chrome, Safari, Opera */
       -moz-transform: rotate(30deg);  /* Firefox */
        -ms-transform: rotate(30deg);  /* IE 9 */
            transform: rotate(30deg);  /* Standard syntax */    
    }

    The function rotate(30deg) rotates the image in clockwise direction about its origin by the angle 30 degrees. You can use negative values to rotate the element counter-clockwise.

    CSS3 Rotate Demo

    The scale() Function

    The scale() function increases or decreases the size of the element. It can be written as scale(sx, sy). If sy isn’t specified, it is assumed to be equal to sx.

    Example

    img {
    
    -webkit-transform: scale(1.5);  /* Chrome, Safari, Opera */
       -moz-transform: scale(1.5);  /* Firefox */
        -ms-transform: scale(1.5);  /* IE 9 */
            transform: scale(1.5);  /* Modern Browsers */    
    }

    The function scale(1.5) proportionally scale the width and height of the image 1.5 times to its original size. The function scale(1) or scale(1, 1) has no effect on the element.

    CSS3 Scale Demo

    The skew() Function

    The skew() function skews the element along the X and Y axes by the specified angles. It can be written as skew(ax, ay). If ay isn’t specified, its value is assumed to be zero.

    Example

    img {
    
    -webkit-transform: skew(-40deg, 15deg);  /* Chrome, Safari, Opera */
       -moz-transform: skew(-40deg, 15deg);  /* Firefox */
        -ms-transform: skew(-40deg, 15deg);  /* IE 9 */
            transform: skew(-40deg, 15deg);  /* Modern Browsers */    
    }

    The function skew(-40deg, 15deg) skews the element -40 degree horizontally along the
    x-axis, and 15 degree vertically along the y-axis.

    CSS3 Skew Demo

    The matrix() Function

    The matrix() function can perform all of the 2D transformations such as translate, rotate, scale, and skew at once. It takes six parameters in the form of a matrix which can be written as matrix(a, b, c, d, e, f). The following section will show you how each of the 2D transformation functions can be represented using the matrix().

    • translate(tx, ty) = matrix(1, 0, 0, 1, tx, ty); — where tx and ty are the horizontal and vertical translation values.
    • rotate(a) = matrix(cos(a), sin(a), -sin(a), cos(a), 0, 0); — where a is the value in deg. You can swap the sin(a) and -sin(a) values to reverse the rotation. The maximum rotation you could perform is 360 degrees.
    • scale(sx, sy) = matrix(sx, 0, 0, sy, 0 ,0); — where sx and sy are the horizontal and vertical scaling values.
    • skew(ax, ay) = matrix(1, tan(ay), tan(ay), 1, 0 ,0); — where ax and ay are the horizontal and vertical values in deg.

    Here is an example of performing the 2D transformation using the matrix() function.

    Example

    img {
    
    -webkit-transform: matrix(0, -1, 1, 0, 200px, 50px);  /* Chrome, Safari, Opera */
       -moz-transform: matrix(0, -1, 1, 0, 200px, 50px);  /* Firefox */
        -ms-transform: matrix(0, -1, 1, 0, 200px, 50px);  /* IE 9 */
            transform: matrix(0, -1, 1, 0, 200px, 50px);  /* Standard syntax */
    }

    However, when performing more than one transformation at once, it is more convenient to use the individual transformation function and list them in order, like this:

    Example

    img {
    
    -webkit-transform: translate(200px, 50px) rotate(180deg) scale(1.5) skew(0, 30deg);  /* Chrome, Safari, Opera */
       -moz-transform: translate(200px, 50px) rotate(180deg) scale(1.5) skew(0, 30deg);  /* Firefox */
        -ms-transform: translate(200px, 50px) rotate(180deg) scale(1.5) skew(0, 30deg);  /* IE 9 */
            transform: translate(200px, 50px) rotate(180deg) scale(1.5) skew(0, 30deg);  /* Standard syntax */    
    }

    2D Transform Functions

    The following table provides a brief overview of all the 2D transformation functions.

    FunctionDescription
    translate(tx,ty)Moves the element by the given amount along the X and Y-axis.
    translateX(tx)Moves the the element by the given amount along the X-axis.
    translateY(ty)Moves the the element by the given amount along the Y-axis.
    rotate(a)Rotates the element by the specified angle around the origin of the element, as defined by the transform-origin property.
    scale(sx,sy)Scale the width and height of the element up or down by the given amount. The function scale(1,1) has no effect.
    scaleX(sx)Scale the width of the element up or down by the given amount.
    scaleY(sy)Scale the height of the element up or down by the given amount.
    skew(ax,ay)Skews the element by the given angle along the X and Y-axis.
    skewX(ax)Skews the element by the given angle along the X-axis.
    skewY(ay)Skews the element by the given angle along the Y-axis.
    matrix(n,n,n,n,n,n)Specifies a 2D transformation in the form of a transformation matrix comprised of the six values.
  • CSS3 Drop Shadows

    Using CSS3 Drop Shadows

    The CSS3 gives you ability to add drop shadow effects to the elements like you do in Photoshop without using any images. Prior to CSS3, sliced images are used for creating the shadows around the elements that was quite annoying.

    The following section will describe you how to apply the shadow on text and elements.

    CSS3 box-shadow Property

    The box-shadow property can be used to add shadow to the element’s boxes. You can even apply more than one shadow effects using a comma-separated list of shadows. The basic syntax of creating a box shadow can be given with:

    box-shadow: offset-x offset-y blur-radius color;

    The components of the box-shadow property have the following meaning:

    • offset-x — Sets the horizontal offset of the shadow.
    • offset-y — Sets the vertical offset of the shadow.
    • blur-radius — Sets the blur radius. The larger the value, the bigger the blur and more the shadow’s edge will be blurred. Negative values are not allowed.
    • color — Sets the color of the shadow. If the color value is omitted or not specified, it takes the value of the color property.

    See the CSS3 box-shadow property to learn more about the other possible values.

    Example

    .box{
    
    width: 200px;
    height: 150px;
    background: #ccc;
    box-shadow: 5px 5px 10px #999;
    }

    Note: When adding the box-shadow, if the value for the blur-radius component is not specified, or set to zero (0), the edges of the shadow will be sharp.

    Similarly, you can add the multiple box shadow using a comma-separated list:

    Example

    .box{
    
    width: 200px;
    height: 150px;
    background: #000;
    box-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
    }

    CSS3 text-shadow Property

    You can use the text-shadow property to apply the shadow effects on text. You can also apply multiple shadows to text using the same notation as box-shadow.

    Example

    h1 {
    
    text-shadow: 5px 5px 10px #666;
    } h2 {
    text-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
    }
  • CSS3 Text Overflow

    Handling Text Overflow in CSS3

    CSS3 introduced several new property properties for modifing the text contents, however some of these properties are existed from a long time. These properties give you precise control over the rendering of text on the web browser.

    Hiding Overflow Text

    Text can overflow, when it is prevented from wrapping, for example, if the value of white-space property is set to nowrap for the containing element or a single word is too long to fit like a long email address. In such situation you can use the CSS3 text-overflow property to determine how the overflowed text content will be displayed.

    You can either display or clip the overflowed text or clip and display an ellipsis or a custom string in palace of the clipped text to indicate the user.

    Values Accepted by the word-break property are: clip and ellipsis and string.

    Example

    p {
    
    width: 400px;
    overflow: hidden;
    white-space: nowrap;
    background: #cdcdcd;
    } p.clipped {
    text-overflow: clip; /* clipped the overflowed text */
    } p.ellipses {
    text-overflow: ellipsis; /* display '…' to represent clipped text */
    }

    Warning: The string value for the text-overflow property is not supported in most of the web browsers, you should better avoid this.


    Breaking Overflow Text

    You can also break a long word and force it to wrap onto a new line that overflows the boundaries of containing element using the CSS3 word-wrap property.

    Values Accepted by the word-wrap property are: normal and break-word.

    Example

    p {
    
    width: 200px;
    background: #ffc4ff;
    word-wrap: break-word;
    }

    Tip: Please check out the individual property reference for all the possible values and the Browser support for these CSS properties.


    Specify Word Breaking Rules

    You can also specify the line breaking rules for the text (i.e. how to break lines within words) yourself using the CSS3 word-break property.

    Values Accepted by the word-break property are: normalbreak-all and keep-all.

    Example

    p {
    
    width: 150px;
    padding: 10px;
    } p.break {
    background: #bedb8b;
    word-break: break-all;
    } p.keep {
    background: #f09bbb;
    word-break: keep-all;
    }
  • CSS3 Gradients

    Using CSS3 Gradients

    The CSS3 gradient feature provides a flexible solution to generate smooth transitions between two or more colors. Earlier, to achieve such effect we had to use the images. Using CSS3 gradients you can reduce the download time and saves the bandwidth usages. The elements with gradients can be scaled up or down to any extent without losing the quality, also the output will render much faster because it is generated by the browser.

    Gradients are available in two styles: linear and radial.

    Creating CSS3 Linear Gradients

    To create a linear gradient you must define at least two color stops. However to create more complex gradient effects you can define more color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along which the gradient effect is applied. The basic syntax of creating the linear gradients using the keywords can be given with:

    linear-gradient(direction, color-stop1, color-stop2, …)

    Linear Gradient – Top to Bottom

    The following example will create a linear gradient from top to bottom. This is default.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(red, yellow);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(red, yellow);
    /* Standard syntax */
    background: linear-gradient(red, yellow);
    }

    Linear Gradient – Left to Right

    The following example will create a linear gradient from left to right.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(left, red, yellow);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(left, red, yellow);
    /* Standard syntax */
    background: linear-gradient(to right, red, yellow);
    }

    Linear Gradient – Diagonal

    You can also create a gradient along the diagonal direction. The following example will create a linear gradient from the bottom left corner to the top right corner of the element’s box.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(bottom left, red, yellow);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(bottom left, red, yellow);
    /* Standard syntax */
    background: linear-gradient(to top right, red, yellow);
    }

    Setting Direction of Linear Gradients Using Angles

    If you want more control over the direction of the gradient, you can set the angle, instead of the predefined keywords. The angle 0deg creates a bottom to top gradient, and positive angles represent clockwise rotation, that means the angle 90deg creates a left to right gradient. The basic syntax of creating the linear gradients using angle can be given with:

    linear-gradient(angle, color-stop1, color-stop2, …)

    The following example will create a linear gradient from left to right using angle.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(0deg, red, yellow);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(0deg, red, yellow);
    /* Standard syntax */
    background: linear-gradient(90deg, red, yellow);
    }

    Creating Linear Gradients Using Multiple Color Stops

    You can also create gradients for more than two colors. The following example will show you how to create a linear gradient using multiple color stops. All colors are evenly spaced.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(red, yellow, lime);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(red, yellow, lime);
    /* Standard syntax */
    background: linear-gradient(red, yellow, lime);
    }

    Setting the Location Color Stops

    Color stops are points along the gradient line that will have a specific color at that location. The location of a color stop can be specified either as a percentage, or as an absolute length. You may specify as many color stops as you like to achieve the desired effect.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(red, yellow 30%, lime 60%);
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(red, yellow 30%, lime 60%);
    /* Standard syntax */
    background: linear-gradient(red, yellow 30%, lime 60%);
    }

    Note: While setting the color-stops location as a percentage, 0% represents the starting point, while 100% represents the ending point. However, you can use values outside of that range i.e. before 0% or after 100% to get the effect you want.


    Repeating the Linear Gradients

    You can repeat linear gradients using the repeating-linear-gradient() function.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: white;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-repeating-linear-gradient(black, white 10%, lime 20%);
    /* For Internet Explorer 10 */
    background: -ms-repeating-linear-gradient(black, white 10%, lime 20%);
    /* Standard syntax */
    background: repeating-linear-gradient(black, white 10%, lime 20%);
    }

    Creating CSS3 Radial Gradients

    In a radial gradient color emerge from a single point and smoothly spread outward in a circular or elliptical shape rather than fading from one color to another in a single direction as with linear gradients. The basic syntax of creating a radial gradient can be given with:

    radial-gradient(shape size at position, color-stop1, color-stop2, …);

    The arguments of the radial-gradient() function has the following meaning:

    • position — Specifies the starting point of the gradient, which can be specified in units (px, em, or percentages) or keyword (left, bottom, etc).
    • shape — Specifies the shape of the gradient’s ending shape. It can be circle or ellipse.
    • size — Specifies the size of the gradient’s ending shape. The default is farthest-side.

    The following example will show you create a radial gradient with evenly spaced color stops.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-radial-gradient(red, yellow, lime);
    /* For Internet Explorer 10 */
    background: -ms-radial-gradient(red, yellow, lime);
    /* Standard syntax */
    background: radial-gradient(red, yellow, lime);
    }

    Setting the Shape of Radial Gradients

    The shape argument of the radial-gradient() function is used to define the ending shape of the radial gradient. It can take the value circle or ellipse. Here’s is an example:

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-radial-gradient(circle, red, yellow, lime);
    /* For Internet Explorer 10 */
    background: -ms-radial-gradient(circle, red, yellow, lime);
    /* Standard syntax */
    background: radial-gradient(circle, red, yellow, lime);
    }

    Note: If the shape argument is omitted or not specified, the ending shape defaults to a circle if the size is a single length, otherwise an ellipse.


    Setting the Size of Radial Gradients

    The size argument of the radial-gradient() function is used to define the size of the gradient’s ending shape. Size can be set using units or the keywords: closest-sidefarthest-sideclosest-cornerfarthest-corner.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: red;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-radial-gradient(left bottom, circle farthest-side, red, yellow, lime);
    /* For Internet Explorer 10 */
    background: -ms-radial-gradient(left bottom, circle farthest-side, red, yellow, lime);
    /* Standard syntax */
    background: radial-gradient(circle farthest-side at left bottom, red, yellow, lime);
    }

    Repeating the Radial Gradients

    You can also repeat radial gradients using the repeating-radial-gradient() function.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: white;
    /* For Safari 5.1 to 6.0 */
    background: -webkit-repeating-radial-gradient(black, white 10%, lime 20%);
    /* For Internet Explorer 10 */
    background: -ms-repeating-radial-gradient(black, white 10%, lime 20%);
    /* Standard syntax */
    background: repeating-radial-gradient(black, white 10%, lime 20%);
    }

    CSS3 Transparency and Gradients

    CSS3 gradients also support transparency. You can use this to create fading effects on background images when stacking multiple backgrounds.

    Example

    .gradient {
    
    /* Fallback for browsers that don't support gradients */
    background: url("images/sky.jpg");
    /* For Safari 5.1 to 6.0 */
    background: -webkit-linear-gradient(left, rgba(255,255,255,0), rgba(255,255,255,1)), url("images/sky.jpg");
    /* For Internet Explorer 10 */
    background: -ms-linear-gradient(left, rgba(255,255,255,0), rgba(255,255,255,1)), url("images/sky.jpg");
    /* Standard syntax */
    background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,1)), url("images/sky.jpg");
    }
  • CSS3 Background

    Using CSS3 Backgrounds

    The CSS3 provides several new properties to manipulate the background of an element like background clipping, multiple backgrounds, and the option to adjust the background size.

    The following section will describe you all the new background features of CSS3, for other background related properties please check out the CSS background tutorial.

    CSS3 background-size Property

    The background-size property can be used to specify the size of the background images.  Prior to CSS3, the size of the background images was determined by the actual size of the images. The background image size can be specified using the pixels or percentage values as well as the keywords autocontain, and cover. Negative values are not allowed.

    Example

    .box {
    
    width: 250px;
    height: 150px;
    background: url("images/sky.jpg") no-repeat;
    background-size: contain;
    border: 6px solid #333;
    }

    Tip: The background-size property is typically used to create full size background images that scale according to the size of viewport or witdh of the browser.


    CSS3 background-clip Property

    The background-clip property can be used to specify whether an element’s background extends into the border or not. The background-clip property can take the three values: border-boxpadding-boxcontent-box.

    Example

    .box {
    
    width: 250px;
    height: 150px;
    padding: 10px;
    border: 6px dashed #333;
    background: orange;
    background-clip: content-box;
    }

    See the tutorial on CSS box model to learn more about element’s boxes.


    CSS3 background-origin Property

    The background-origin property can be used to specify the positioning area of the background images. It can take the same values as background-clip property: border-boxpadding-boxcontent-box.

    Example

    .box {
    
    width: 250px;
    height: 150px;
    padding: 10px;
    border: 6px dashed #333;
    background: url("images/sky.jpg") no-repeat;
    background-size: contain;
    background-origin: content-box;
    }

    Note: The background-origin property is ignored if the value of background-attachment property is specified as 'fixed'.


    CSS3 Multiple Backgrounds

    CSS3 gives you ability to add multiple backgrounds to a single element. The backgrounds are layered on the top of one another. The number of layers is determined by the number of comma-separated values in the background-image or background shorthand property.

    Example

    .box {
    
    width: 100%;
    height: 500px;
    background: url("images/birds.png") no-repeat center,  url("images/clouds.png")  no-repeat center, lightblue;
    }

    The first value in the comma-separated list of backgrounds i.e. the background-image ‘birds.png’ will appear on the top and the last value i.e. the ‘lightblue’ color will appear at the bottom. Only the last background can include a background-color.

  • CSS3 Color

    Defining Colors in CSS3

    In the previous section you’ve learnt how to define colors using the color keywords and RGB notations. In addition to that CSS3 adds some new functional notations for setting color values for the elements which are rgba()hsl() and hsla().

    In the following section we’ll discuss about these color model one by one.

    RGBA Color Values

    Colors can be defined in the RGBA model (red-green-blue-alpha) using the rgba() functional notation. RGBA color model are an extension of RGB color model with an alpha channel — which specifies the opacity of a color.

    The alpha parameter accepts a value from 0.0 (fully transparent) to 1.0 (fully opaque).

    Example

    h1 {
    
    color: rgba(0,0,255,0.5);
    } p {
    background-color: rgba(0%,0%,100%,0.3);
    }

    HSL Color Values

    Colors also can be defined the HSL model (hue-saturation-lightness) using the hsl() functional notation. Hue is represented as an angle (from 0 to 360) of the color wheel or circle (i.e. the rainbow represented in a circle). This angle is given as a unit less number because the angle is so typically measured in degrees that the unit is implicit in CSS.

    Saturation and lightness are represented as percentages. 100% saturation means full color, and 0% is a shade of gray. Whereas, 100% lightness is white, 0% lightness is black, and 50% lightness is normal. Check out the example below:

    Example

    h1 {
    
    color: hsl(360,70%,60%);
    } p {
    background-color: hsl(480,50%,80%);
    }

    Tip: By the definition red=0=360, and the other colors are spread around the circle, so green=120blue=240, etc. As an angle, it implicitly wraps around such that -120=240480=120, and so on.


    HSLA Color Values

    Colors can be defined in the HSLA model (hue-saturation-lightness-alpha) using the hsla() functional notation. HSLA color model are an extension of HSL color model with an alpha channel — which specifies the opacity of a color.

    The alpha parameter accepts a value from 0.0 (fully transparent) to 1.0 (fully opaque).

    Example

    h1 {
    
    color: hsla(360,80%,50%,0.5);
    } p {
    background-color: hsla(480,60%,30%,0.3);
    }
  • CSS3 Border

    Using CSS3 Borders

    The CSS3 provides two new properties for styling the borders of an element in a more elegant way — the border-image property for adding the images to borders, and the border-radius property for making the rounded corners without using any images.

    The following section will describe you these new border properties of CSS3, for other border related properties please check out the CSS border tutorial.

    Creating CSS3 Rounded Corners

    The border-radius property can be used to create rounded corners. This property typically defines the shape of the corner of the outer border edge. Prior to CSS3, sliced images are used for creating the rounded corners that was rather bothersome.

    Example

    .box {
    
    width: 300px;
    height: 150px;
    background: #ffb6c1;
    border: 2px solid #f08080;
    border-radius: 20px;
    }

    Adding CSS3 Border Images

    The border-image property allows you to specify an image to act as an element’s border.
    The design of the border is created from the sides and corners of the image specified in border-image source URL. The border image may be sliced, repeated, scaled and stretched in various ways to fit the size of the border image area.

    Example

    .box {
    
    width: 300px;
    height: 150px;
    border: 15px solid transparent;
    -webkit-border-image: url("border.png") 30 30 round; /* Safari 3.1-5 */
    -o-border-image: url("border.png") 30 30 round; /* Opera 11-12.1 */
    border-image: url("border.png") 30 30 round;
    }
  • CSS Validation

    Why Validate Your CSS Code

    As a beginner, it is very common that you will make mistake in writing your CSS code. Incorrect or non-standard code may cause unexpected results in how your page displayed or functions in a web browser.

    The World Wide Web Consortium (W3C) has created a great tool https://jigsaw.w3.org/css-validator/ to automatically check your style sheets, and point out any problems/errors your code might have, such as invalid CSS property missing closing bracket or missing semicolon (;) etc. It is absolutely free.

    Validating a Website

    Website validation is the process to ensure that the pages of a website conform to the formal guidelines and standards set by the World Wide Web Consortium (W3C).

    There are several specific reasons for validating a website, some of them are:

    • It helps to create Web pages that are cross-browser, cross-platform compatible. It also likely to be compatible with the future version of Web browsers and Web standards.
    • Standards compliant web pages increase the search engines spider visibility and your pages will more likely be appear in search results.
    • It will reduce unexpected errors and make your web pages more accessible to the visitor of your website.

    Note: Validation is important. It will ensure that your web pages are interpreted in the same way (the way you want it) by the various web browsers, search engines etc. as well as users and visitors of your Web site.

    Follow the link given below to validate your CSS document.

  • CSS Opacity

    Cross Browser Opacity

    Opacity is now a part of the CSS3 specifications, but it was present for a long time. However, older browsers have different ways of controlling the opacity or transparency.

    CSS Opacity in Firefox, Safari, Chrome, Opera and IE9

    Here is the most up to date syntax for CSS opacity in all current browsers.

    Example

    p {
    
    opacity: 0.7;
    }

    The above style rule will make the paragraph element 70% opaque (or 30% transparent).

    The opacity property takes a value a value from 0.0 to 1.0. A setting of opacity: 1; would make the element completely opaque (i.e. 0% transparent), whereas opacity: 0; would make the element completely transparent (i.e. 100% transparent).


    CSS Opacity in Internet Explorer 8 and lower

    Internet Explorer 8 and earlier version supports a Microsoft-only property “alpha filter” to control the transparency of an element.

    Example

    p {
    
    filter: alpha(opacity=50);
    zoom: 1;  /* Fix for IE7 */
    }

    Note: Alpha filters in IE accept values from 0 to 100. The value 0 makes the element completely transparent (i.e. 100% transparent), whereas the value 100 makes the element completely opaque (i.e. 0% transparent).


    CSS Opacity for All Browser

    Combining the both steps above you will get the opacity for all browsers.

    Example

    p {
    
    opacity: 0.5;  /* Opacity for Modern Browsers */
    filter: alpha(opacity=50);  /* Opacity for IE8 and lower */
    zoom: 1;  /* Fix for IE7 */
    }

    Warning: Including alpha filter to control transparency in Internet Explorer 8 and lower versions creates invalid code in your style sheet since this is a Microsoft-only property, not a standard CSS property.


    CSS Image Opacity

    You can also make transparent images using CSS Opacity.

    The three images in the illustration below are all from the same source image. The only differences between them are the level of their opacity.

    100% Opaque Image50% Opaque Image25% Opaque Image
    opacity:1opacity:0.5opacity:0.25

    Change Image Opacity on Mouse Over

    The following example demonstrates a common use of CSS image opacity, where the opacity of images changes when the user moves the mouse pointer over an image.

    — Move your mouse pointer over the images to see the effect.


    Text in Transparent Box

    When using opacity on an element not only the background of the element that will have transparency, but all of its child elements become transparent as well. It is making the text inside the transparent element hard to read if the value of opacity becomes higher.

    OPACITYOPACITYOPACITYOPACITY

    To prevent this either you can use transparent PNG images, or put the text block outside of the transparent box and push it visually inside using the negative margin or CSS positioning.

    Example

    div {
    
    float: left;
    opacity: 0.7;
    border: 1px solid #949781;
    } p {
    float: left;
    position: relative;
    margin-left: -400px;
    }

    CSS Transparency Using RGBA

    In addition to RGB CSS3 has introduced a new way RGBA to specify a color that includes alpha transparency as part of the color value. RGBA stands for Red Blue Green Alpha.

    The RGBA declaration is a very easy way to set transparency for a color.

    Example

    div {
    
    background: rgba(200, 54, 54, 0.5);
    } p {
    color: rgba(200, 54, 54, 0.25);
    }

    The first three numbers representing the color in RGB values i.e. red (0-255), green (0-255), blue (0-255) and the fourth representing alpha transparency value between 0 to 1 (0 makes the color fully transparent , whereas the value of 1 makes it fully opaque).

    One important characteristic to note about the RGBA transparency is that — the ability to control the opacity of individual color. With RGBA, we can make the text color of an element transparent and leave background intact.

    RGBARGBARGBARGBA

    — Or leave the text color alone and change only the transparency of background.

    RGBARGBARGBARGBA

    You can see how easily you can control the opacity of individual colors rather than the entire element using RGBA. However it is always recommended to define a fallback color for the browsers that do not support the RGBA colors.

    Note: The RGBA transparency doesn’t affect the child elements the way the opacity property does. The alpha value of RGBA affects the transparency of individual color rather than the entire element.


    Declaring a Fallback Color

    All browsers do not support RGBA colors. However, you can provide an alternative such as solid colors or transparent PNG images for the browsers that don’t support it.

    Example

    p {
    
    /* Fallback for web browsers that doesn't support RGBA */
    background: rgb(0, 0, 0);
    /* RGBa with 0.5 opacity */
    background: rgba(0, 0, 0, 0.5);
    }