ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

CSS Product Card Design with Hover Effects

CSS

Goal -- WPM

Ready
Exercise Algorithm Area
1.product-card {
2border: 1px solid #ddd;
3border-radius: 8px;
4overflow: hidden;
5width: 280px;
6margin: 20px;
7box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
8transition: transform 0.3s ease, box-shadow 0.3s ease;
9background-color: #fff;
10}
11
12.product-card:hover {
13transform: translateY(-5px);
14box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
15}
16
17.product-image img {
18width: 100%;
19height: 200px;
20object-fit: cover;
21}
22
23.product-info {
24padding: 15px;
25}
26
27.product-title {
28font-size: 1.2rem;
29margin-bottom: 10px;
30color: #333;
31}
32
33.product-price {
34font-size: 1rem;
35color: #007bff;
36font-weight: bold;
37margin-bottom: 15px;
38}
39
40.add-to-cart-btn {
41display: block;
42width: 100%;
43padding: 10px;
44background-color: #28a745;
45color: white;
46border: none;
47border-radius: 5px;
48cursor: pointer;
49font-size: 1rem;
50transition: background-color 0.3s ease;
51}
52
53.add-to-cart-btn:hover {
54background-color: #218838;
55}
56
57/* Edge case: Ensure image aspect ratio is maintained */
58.product-image img[src=""] {
59display: none; /* Hide if no image source */
60}
Algorithm description viewbox

CSS Product Card Design with Hover Effects

Algorithm description:

This CSS code styles a product card with interactive hover effects. It uses `box-shadow` to give depth and `transition` properties to animate changes in `transform` and `box-shadow` when the user hovers over the card. The product image, title, price, and an 'Add to Cart' button are styled for a clean e-commerce look. An edge case is handled for products without an image source, preventing broken image display.

Algorithm explanation:

The algorithm leverages CSS properties for visual styling and interaction. The `.product-card` class applies a base style with `border-radius`, `box-shadow`, and `transition` for smooth animations. The `:hover` pseudo-class is used to modify `transform` (slight upward movement) and `box-shadow` (increased depth) when the mouse is over the card. The `transition` property ensures these changes are animated over 0.3 seconds. The `object-fit: cover` on the image maintains its aspect ratio. An edge case is handled where if an image `src` is empty, the image element is hidden. This approach is efficient as it relies on browser rendering and doesn't require JavaScript for basic hover effects. The time complexity is effectively O(1) for rendering and interaction, as it's declarative styling.

Pseudocode:

1. Define base styles for the product card container (border, shadow, rounded corners, transitions).
2. Apply a subtle upward transform and increased shadow on hover.
3. Style the product image to cover its container and maintain aspect ratio.
4. Style the product information section (title, price).
5. Style the 'Add to Cart' button with a background color and hover effect.
6. Add a rule to hide the image element if its source is empty.