Lesson 9 Review
In our last lesson, we learned how to use Advanced Selectors and generate virtual content using Pseudo-elements. Today, we focus on the aesthetic presentation layer and how to write DRY (Don't Repeat Yourself) code using CSS Variables.
Step 1: CSS Variables (Custom Properties)
Imagine you are building a massive website, and you use your brand's primary color (#4C8BF5) on 50 different buttons, headings, and borders. If your boss asks you to change the brand color to purple, you would have to find and replace that hex code 50 times.
CSS Variables solve this. You declare a value once at the top of your file, give it a custom name, and reference that name everywhere else.
Subtopic 1.1: Declaring and Using Variables
We usually declare variables inside the :root pseudo-class. This attaches the variables to the highest level of the HTML document, making them accessible globally.
- A variable must start with two dashes:
--variable-name. - To use the variable, you wrap its name in the
var()function.
The CSS:
:root {
--primary-color: #4C8BF5;
--danger-color: #e74c3c;
--main-padding: 20px;
}
/* 2. Use the var() function to apply them */
.save-button {
background-color: var(--primary-color);
padding: var(--main-padding);
}
.delete-button {
background-color: var(--danger-color);
padding: var(--main-padding);
}
The Output:
Step 2: Shadows (Adding Depth)
Modern web design relies heavily on flat design with subtle shadows to create the illusion of depth (often called "Card Design" or "Material Design"). We use two properties: box-shadow for containers and text-shadow for typography.
Subtopic 2.1: The Box Shadow Mechanics
The box-shadow property accepts up to 5 values in a very specific order:
- X-Offset: Horizontal distance (positive moves right, negative moves left).
- Y-Offset: Vertical distance (positive moves down, negative moves up).
- Blur Radius: How soft the shadow is (higher is softer/wider).
- Spread Radius (Optional): How much the shadow expands or contracts.
- Color: Usually an RGBA value so the shadow is semi-transparent.
The CSS: Floating Card Effect
background-color: white;
padding: 30px;
border-radius: 8px;
/* X=0, Y=10px, Blur=20px, Color=Black at 15% opacity */
box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.15);
}
The Output:
Subtopic 2.2: Text Shadow
text-shadow works exactly the same way, but it does not accept a "Spread Radius". It is strictly X, Y, Blur, and Color.
The CSS: Neon Glow Text
color: white;
/* X=0, Y=0, Blur=15px, Color=Cyan */
text-shadow: 0px 0px 15px #00ffff;
}
The Output:
CYAN GLOW
Step 3: Background Gradients
Gradients allow you to smoothly transition between two or more colors. The browser actually renders gradients as background images, not solid colors, which is why we use the background-image or background shorthand property.
Subtopic 3.1: Linear Gradients
A linear gradient flows in a straight line. You must define a direction (or angle) and at least two color stops.
The CSS: Directional and Angled Gradients
background: linear-gradient(to right, #4C8BF5, #8e44ad);
}
.gradient-angled {
/* 45 degrees, starting red, ending orange */
background: linear-gradient(45deg, #e74c3c, #f1c40f);
}
The Output:
Step 4: The Math Function: calc()
One of the most powerful tools in CSS is the calc() function. It allows you to perform basic math (addition, subtraction, multiplication, division) directly inside your CSS rules.
Its superpower is mixing units. For example, what if you want a box to be 100% of the screen width, MINUS exactly 50 pixels to leave room for a fixed sidebar?
calc(), you MUST put spaces around the plus and minus operators. calc(100%-50px) is invalid and will fail. It must be calc(100% - 50px).
The HTML & CSS: Mixing Percentages and Pixels
<div class="layout-wrapper">
<div class="sidebar">Fixed 200px</div>
<div class="main-content">Fluid (100% - 200px)</div>
</div>
/* CSS */
.layout-wrapper { display: flex; }
.sidebar {
width: 200px;
background-color: #34495e;
}
.main-content {
/* Calculates width dynamically as the browser resizes! */
width: calc(100% - 200px);
background-color: #ecf0f1;
}
The Output:
Final Quiz: Test Your Visual & Mathematical Logic
Click the buttons below to verify your knowledge.
0 Comments