Adding Shadows and Elevation to Elements

Tutorial 3 of 5

Introduction

This tutorial will guide you through the process of adding shadows and elevation to HTML elements using CSS. This is a common design technique used to enhance the appearance of web pages and create a sense of depth and hierarchy among elements.

By the end of this tutorial, you will learn how to use the 'box-shadow' property, and how to create different shadow effects, providing your web pages with a more dynamic and professional look.

Prerequisites: Basic knowledge of HTML and CSS.

Step-by-Step Guide

The main concept we'll be working with is the 'box-shadow' property in CSS. This property allows you to attach one or more shadows to an element.

The syntax for 'box-shadow' is as follows:

box-shadow: h-offset v-offset blur spread color;

  • h-offset: Specifies the horizontal offset of the shadow. Positive values place the shadow on the right of the element, while negative values place it on the left.
  • v-offset: Specifies the vertical offset of the shadow. Positive values place the shadow below the element, while negative values place it above.
  • blur: Sets the blur radius. The higher the number, the more blurred the shadow will be.
  • spread: Sets the size of the shadow. Positive values increase the size, negative values decrease it.
  • color: Defines the color of the shadow.

Code Examples

Example 1: Basic Shadow

Here's a simple example where we add a shadow to a div element.

<div class="shadow-box">Hello, World!</div>
.shadow-box {
  width: 200px;
  height: 200px;
  box-shadow: 5px 5px 10px 0px grey;
}

In this example, the shadow is offset 5px to the right and 5px down, has a blur radius of 10px and a spread of 0px, and is colored grey.

Example 2: Multiple Shadows

You can add multiple shadows to a single element by comma-separating them.

<div class="multi-shadow-box">Hello, World!</div>
.multi-shadow-box {
  width: 200px;
  height: 200px;
  box-shadow: 3px 3px 5px 0px grey, -3px -3px 5px 0px grey;
}

In this example, we have two shadows: one offset 3px to the right and 3px down, and another offset 3px to the left and 3px up. Both have a blur radius of 5px, a spread of 0px, and are colored grey.

Summary

In this tutorial, we've learned how to add shadows and elevation to HTML elements using the 'box-shadow' property in CSS. We've covered how to adjust the offset, blur, spread, and color of the shadow, and how to add multiple shadows to a single element.

Next, you might want to learn how to use other CSS properties to style your web pages, such as 'border-radius' for rounded corners, or 'transition' for smooth animations.

Practice Exercises

  1. Add a shadow to a button element. Make the shadow appear on hover.
  2. Create a div element with a shadow that gives it the appearance of being elevated off the page.
  3. Create a card-like element with multiple shadows to give it a complex, layered appearance.

Remember, practice is key. Experiment with different values and see how they affect the shadow. Happy coding!