Dmitrii Korolkov

github | email

CSS3 tutorial for beginners part 2

Welcome to the second part of our CSS3 tutorial for beginners! In this article, we'll dive deeper into some of the more powerful and helpful features that CSS3 offers. We'll explore advanced selectors, CSS variables, gradients, transforms, animations, flexbox, grid layouts, and filters. Let's get started!


1. Advanced Selectors and Pseudo-classes/Elements

CSS3 builds on basic selectors by introducing more refined pseudo-classes and pseudo-elements. These allow you to target elements based on their position or state, making your styles more precise:


/* Select every even list item */
li:nth-child(even) {
  background: #f2f2f2;
}

/* Add content before paragraphs */
p::before {
  content: "Note: ";
  font-weight: bold;
}

2. CSS Variables (Custom Properties)

CSS variables make it easier to manage your styles by storing values that you can reuse throughout your stylesheet. This not only simplifies maintenance but also enhances flexibility:


:root {
  --primary-color: #3498db;
  --secondary-color: #2ecc71;
  --font-stack: 'Helvetica Neue', sans-serif;
}

body {
  font-family: var(--font-stack);
  color: var(--primary-color);
}

.button {
  background-color: var(--secondary-color);
  padding: 10px 15px;
  border: none;
  border-radius: 5px;
}

3. CSS3 Gradients

Gradients let you create smooth transitions between colors without relying on images. CSS3 supports both linear and radial gradients:


/* Linear Gradient Example */
.box {
  background: linear-gradient(45deg, #ff6b6b, #f06595);
}

/* Radial Gradient Example */
.circle {
  background: radial-gradient(circle, #74c0fc, #4dabf7);
}

4. Transforms and 3D Effects

The transform property enables you to rotate, scale, skew, or translate elements. For 3D effects, you can incorporate perspective:


.rotate {
  transform: rotate(15deg);
}

.scale {
  transform: scale(1.1);
}

/* 3D flip card effect */
.card {
  perspective: 1000px;
}
.card-inner {
  transition: transform 0.6s;
  transform-style: preserve-3d;
}
.card:hover .card-inner {
  transform: rotateY(180deg);
}

5. Advanced Transitions and Animations

Beyond simple hover transitions, CSS3 lets you create complex animations using keyframes. This adds dynamic movement to your web pages:


@keyframes slideIn {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}

.animated {
  animation: slideIn 0.5s ease-out;
}

6. Layout Techniques: Flexbox and Grid

6.1 Flexbox

Flexbox simplifies the creation of responsive layouts by distributing space along a single row or column:


.flex-container {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

6.2 CSS Grid

CSS Grid is a powerful layout system for creating two-dimensional grid-based designs:


.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}

7. CSS Filters

CSS filters allow you to apply visual effects like blur, brightness, and contrast directly to elements:


.filtered {
  filter: grayscale(50%) blur(2px);
}

8. Best Practices and Optimization Tips

To get the most out of CSS3, keep these tips in mind:


- Organize your code with comments and logical sections.
- Leverage CSS variables for reusable values.
- Use precise selectors to maintain performance.
- Test layouts across various devices and browsers.
- Utilize browser developer tools for real-time debugging and fine-tuning

9. Putting It All Together: An Advanced Example

This example combines several CSS3 features into one cohesive layout:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS3 Advanced Example</title>
  <style>
    :root {
      --primary: #e74c3c;
      --secondary: #8e44ad;
    }
    body {
      font-family: Arial, sans-serif;
      background: #ecf0f1;
      margin: 0;
      padding: 20px;
    }
    .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-gap: 20px;
    }
    .box {
      background: linear-gradient(135deg, var(--primary), var(--secondary));
      padding: 20px;
      border-radius: 10px;
      color: #fff;
      box-shadow: 0 4px 8px rgba(0,0,0,0.2);
      transition: transform 0.3s;
    }
    .box:hover {
      transform: translateY(-10px);
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="box">
      <h2>Box 1</h2>
      <p>A beautiful gradient box.</p>
    </div>
    <div class="box">
      <h2>Box 2</h2>
      <p>Hover to see the effect.</p>
    </div>
  </div>
</body>
</html>