CSS Exam 4: Motion and Visual Effects (10 Composite Exercises)
CSS Exam 4: Motion and Visual Effects
Bring your pages to life with smooth transitions, 2D/3D transforms, and advanced animations.
Exercise 1
Smooth Transitions
Create a button with a smooth color change:
- Base background color is blue.
- On hover, change the color to red.
- The transition should take 0.5s.
- Use the timing function ease-in-out.
Solution
.btn {
background-color: blue;
transition: background-color 0.5s ease-in-out;
}
.btn:hover {
background-color: red;
}
Exercise 2
2D Transforms
Apply the following transforms to an image on hover:
- Scale the image to 1.2.
- Rotate it by 10 degrees.
- Translate it 20px to the right.
Solution
img:hover {
transform: scale(1.2) rotate(10deg) translateX(20px);
}
Exercise 3
Keyframes Animation
Create a simple animation:
- Define `@keyframes` named "fadeIn".
- Start at opacity 0 and end at opacity 1.
- Apply the animation to (.box) for 2 seconds and run once.
Solution
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
animation: fadeIn 2s ease-in;
}
Exercise 4
3D Transforms
Create a flip-card effect:
- Add `perspective` to the parent container (e.g., 1000px).
- Rotate (.card) on the Y axis by 180 degrees on hover.
- Use `preserve-3d` to keep 3D depth for children.
Solution
.container {
perspective: 1000px;
}
.card {
transform-style: preserve-3d;
transition: transform 0.6s;
}
.container:hover .card {
transform: rotateY(180deg);
}
Exercise 5
CSS Variables (Custom Properties)
Use variables to make changes easier:
- Define a main color variable (`--main-color`) with the value "blue" in `:root`.
- Use it for the button background and the heading text color.
Solution
:root {
--main-color: blue;
}
button {
background-color: var(--main-color);
}
h1 {
color: var(--main-color);
}
Exercise 6
Infinite Animation
Make an element spin forever:
- Define `@keyframes` for rotation from 0 to 360 degrees.
- Apply a linear infinite animation for 2 seconds.
Solution
@keyframes spin {
to { transform: rotate(360deg); }
}
.loader {
animation: spin 2s linear infinite;
}
Exercise 7
Transition Delay
Create a staggered effect:
- On hover, change width first.
- After width finishes (e.g., 0.5s), change height.
- Use `transition-delay` to achieve this.
Solution
.box {
transition: width 0.5s, height 0.5s 0.5s; /* delay height by 0.5s */
}
.box:hover {
width: 200px;
height: 200px;
}
Exercise 8
Filters
Apply visual effects to an image:
- Make the image grayscale at 100%.
- Add a 5px blur.
- On hover, remove these effects (back to normal).
Solution
img {
filter: grayscale(100%) blur(5px);
transition: filter 0.3s;
}
img:hover {
filter: none; /* or grayscale(0) blur(0) */
}
Exercise 9
Clip-path
Change the element shape:
- Use `clip-path` to make the image appear as a circle.
- Or make it appear as a triangle with polygon.
Solution
/* Circle */
.circle {
clip-path: circle(50%);
}
/* Triangle */
.triangle {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
Exercise 10
Animation Direction
Control animation direction:
- Make the animation run forward then backward (alternate).
- Keep the end state after the animation finishes (animation-fill-mode: forwards).
Solution
.box {
animation-direction: alternate;
animation-fill-mode: forwards;
}