40 Advanced CSS Tutorials

40 Advanced CSS Tutorials

1. CSS Grid Layout

CSS Grid Layout is a two-dimensional layout system for creating complex, responsive layouts.

Example: Grid layout with auto-fit columns.

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
.grid-item {
background: #e5e7eb;
padding: 1rem;
}
Item 1
Item 2
Item 3

Note: Use minmax for responsive column sizing.

2. Flexbox Advanced

Flexbox allows precise control over alignment, order, and spacing of elements.

Example: Centered flex items with order.

.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.flex-item:nth-child(2) {
order: -1;
}
Item 1
Item 2 (Ordered First)
Item 3

Note: Use order to reorder flex items.

3. Custom Properties (Variables)

CSS custom properties allow reusable values for consistent styling.

Example: Themed colors with variables.

:root {
--main-bg: #1e40af;
--text-color: #ffffff;
}
.element {
background: var(--main-bg);
color: var(--text-color);
}
Themed Element

Note: Update variables dynamically with JavaScript.

4. CSS Animations

CSS animations create smooth, keyframe-based transitions for elements.

Example: Fade animation.

.fade {
animation: fadeIn 2s ease-in-out infinite;
}
@keyframes fadeIn {
0% { opacity: 0; }
50% { opacity: 1; }
100% { opacity: 0; }
}
Fading Element

Note: Optimize animations for performance.

5. CSS Transitions

Transitions smoothly interpolate property changes over time.

Example: Hover transition.

.box {
width: 100px;
height: 100px;
background: #1e40af;
transition: transform 0.3s ease, background 0.3s ease;
}
.box:hover {
transform: scale(1.2);
background: #2563eb;
}

Note: Use transform for better performance.

6. CSS Filters

Filters apply graphical effects like blur, brightness, or grayscale to elements.

Example: Image blur effect.

.image {
filter: blur(5px);
transition: filter 0.3s ease;
}
.image:hover {
filter: none;
}
Blur Image

Note: Combine filters for complex effects.

7. Blend Modes

Blend modes control how an element's colors blend with its background.

Example: Multiply blend mode.

.blend {
background: #1e40af;
mix-blend-mode: multiply;
}
Blended Text

Note: Test blend modes for visual compatibility.

8. Clip Path

Clip-path creates custom shapes by clipping an element’s visible area.

Example: Polygon clip-path.

.clipped {
clip-path: polygon(0 0, 100% 0, 100% 80%, 0 100%);
background: #1e40af;
padding: 1rem;
}
Clipped Element

Note: Use clip-path for creative layouts.

9. CSS Shapes

Shapes allow text to wrap around custom paths or images.

Example: Circle shape with text wrap.

.shape {
float: left;
shape-outside: circle(50%);
width: 150px;
height: 150px;
background: #1e40af;
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Note: Ensure sufficient contrast for readability.

10. Scroll Snap

Scroll snap aligns content to specific points during scrolling.

Example: Vertical scroll snap.

.snap-container {
scroll-snap-type: y mandatory;
overflow-y: auto;
height: 200px;
}
.snap-item {
scroll-snap-align: start;
height: 200px;
}
Section 1
Section 2
Section 3

Note: Test on touch devices for smooth snapping.

11. CSS Counters

CSS counters automatically number elements, like lists or sections.

Example: Auto-numbered sections.

body {
counter-reset: section;
}
.section::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
First
Second

Note: Use for dynamic numbering without JavaScript.

12. Advanced Selectors

Advanced selectors like :where and :is simplify complex targeting.

Example: Using :is for multiple selectors.

:is(h1, h2, h3) {
color: #1e40af;
}

Heading 1

Heading 2

Heading 3

Note: Reduces CSS repetition.

13. CSS Grid Areas

Grid areas allow named regions for precise layout control.

Example: Named grid areas.

.grid {
display: grid;
grid-template-areas: 
"header header"
"sidebar main";
grid-template-columns: 200px 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
Header
Main

Note: Use for intuitive layout naming.

14. Sticky Positioning

Sticky positioning keeps elements fixed within a scrolling container.

Example: Sticky header.

.sticky {
position: sticky;
top: 0;
background: #1e40af;
color: white;
padding: 1rem;
}
Sticky Header

Scroll down...

Content

Note: Ensure parent has overflow for sticky to work.

15. CSS Variables in Media Queries

CSS variables can be redefined within media queries for responsive designs.

Example: Responsive font size.

:root {
--font-size: 16px;
}
@media (max-width: 600px) {
:root {
--font-size: 14px;
}
}
.element {
font-size: var(--font-size);
}
Responsive Text

Note: Simplifies responsive styling.

16. CSS Houdini

Houdini APIs extend CSS with custom paint, layout, and animations.

Example: Custom paint worklet (requires worklet file).

.houdini {
--custom-prop: 10;
background: paint(my-paint);
}
Houdini Paint (Placeholder)

Note: Requires browser support and external worklet.

17. Subgrid

Subgrid allows child grids to inherit parent grid tracks.

Example: Nested grid with subgrid.

.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr;
}
.child-grid {
display: grid;
grid-template-columns: subgrid;
}
Child 1
Child 2

Note: Check browser support for subgrid.

18. Aspect Ratio

The aspect-ratio property maintains element proportions.

Example: Fixed aspect ratio box.

.box {
aspect-ratio: 16 / 9;
width: 200px;
background: #1e40af;
}

Note: Fallback with padding for older browsers.

19. CSS Logical Properties

Logical properties adapt to writing modes and directions.

Example: Logical margin.

.logical {
margin-inline: 1rem;
padding-block: 1rem;
background: #e5e7eb;
}
Logical Spacing

Note: Ideal for multilingual layouts.

20. Scroll Behavior

Smooth scrolling enhances navigation with scroll-behavior.

Example: Smooth scroll to anchor.

html {
scroll-behavior: smooth;
}
a[href="#section"] {
display: block;
padding: 1rem;
}
Go to Section

Scroll down...

Target Section

Note: Not supported in all browsers.

21. CSS Container Queries

Container queries style elements based on their container’s size.

Example: Responsive container styling.

.container {
container-type: inline-size;
}
@container (min-width: 300px) {
.container-item {
font-size: 1.2rem;
}
}
Container Item

Note: Requires modern browser support.

22. Gradient Animations

Animated gradients create dynamic backgrounds using keyframes.

Example: Moving gradient.

.gradient {
background: linear-gradient(45deg, #1e40af, #2563eb);
background-size: 200%;
animation: gradientMove 5s linear infinite;
}
@keyframes gradientMove {
0% { background-position: 0% 50%; }
100% { background-position: 200% 50%; }
}
Animated Gradient

Note: Optimize for performance with GPU acceleration.

23. CSS Masks

Masks control element visibility with images or gradients.

Example: Gradient mask.

.masked {
mask-image: linear-gradient(to right, transparent, black);
background: #1e40af;
padding: 1rem;
}
Masked Element

Note: Check browser support for masks.

24. Custom Scrollbars

Custom scrollbars style the appearance of scrollable areas.

Example: Styled scrollbar.

.scrollbar {
scrollbar-width: thin;
scrollbar-color: #1e40af #e5e7eb;
}
.scrollbar::-webkit-scrollbar {
width: 8px;
}
.scrollbar::-webkit-scrollbar-thumb {
background: #1e40af;
}

Scroll to see custom scrollbar...

Note: Use both standard and WebKit for compatibility.

25. CSS Overscroll Behavior

Overscroll behavior controls scroll chaining and bounce effects.

Example: Prevent overscroll.

.no-overscroll {
overscroll-behavior: contain;
height: 200px;
overflow-y: auto;
}

Scroll without overscroll...

Note: Improves UX in nested scrollable areas.

26. Advanced Pseudo-Elements

Pseudo-elements like ::marker style specific parts of elements.

Example: Custom list markers.

ul {
list-style: none;
}
li::marker {
content: "→";
color: #1e40af;
}
  • Item 1
  • Item 2

Note: Limited to specific elements like lists.

27. CSS Feature Queries

Feature queries (@supports) apply styles based on browser support.

Example: Fallback for grid.

@supports (display: grid) {
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
@supports not (display: grid) {
.grid {
display: flex;
flex-wrap: wrap;
}
}
Item 1
Item 2

Note: Provides graceful degradation.

28. CSS Backdrop Filter

Backdrop filter applies effects like blur to the area behind an element.

Example: Blurred backdrop.

.backdrop {
backdrop-filter: blur(5px);
background: rgba(255, 255, 255, 0.2);
padding: 1rem;
}
Blurred Backdrop

Note: Check browser support for backdrop-filter.

29. CSS Color Functions

Color functions like hsl and rgb allow dynamic color manipulation.

Example: HSL color adjustment.

.color {
background: hsl(220, 80%, 50%);
padding: 1rem;
color: white;
}
HSL Color

Note: HSL is intuitive for hue adjustments.

30. CSS Grid Auto Flow

Auto-flow controls how grid items are placed automatically.

Example: Dense grid flow.

.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: dense;
}
Item 1
Item 2

Note: Dense flow fills gaps efficiently.

31. Transform 3D

3D transforms create perspective-based animations.

Example: 3D rotation.

.transform {
transform: perspective(500px) rotateY(45deg);
transition: transform 0.5s ease;
}
.transform:hover {
transform: perspective(500px) rotateY(0deg);
}
3D Transform

Note: Use perspective for depth.

32. CSS Custom Cursors

Custom cursors enhance UX with unique pointer styles.

Example: Custom cursor image.

.cursor {
cursor: url('cursor.png'), auto;
}
Hover for Custom Cursor

Note: Use fallback cursors for compatibility.

33. CSS Writing Modes

Writing modes control text direction for multilingual layouts.

Example: Vertical text.

.vertical {
writing-mode: vertical-rl;
background: #e5e7eb;
padding: 1rem;
}
Vertical Text

Note: Ideal for East Asian scripts.

34. CSS Blend Isolation

Isolation controls blending context for child elements.

Example: Isolated blending.

.isolated {
isolation: isolate;
background: #1e40af;
padding: 1rem;
}
Isolated Element

Note: Useful with blend modes.

35. CSS Content Visibility

Content visibility optimizes rendering for off-screen content.

Example: Auto visibility.

.content {
content-visibility: auto;
contain-intrinsic-size: 500px;
}
Optimized Content

Note: Improves page load performance.

36. CSS Gap Property

Gap sets spacing between grid or flex items.

Example: Grid with gap.

.grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(3, 1fr);
}
Item 1
Item 2
Item 3

Note: Replaces margin-based spacing.

37. CSS Object Fit

Object-fit controls how images or videos fit within their containers.

Example: Cover fit image.

.image {
width: 200px;
height: 100px;
object-fit: cover;
}
Object Fit

Note: Use with object-position for alignment.

38. CSS Will Change

Will-change hints browsers about upcoming animations for optimization.

Example: Optimize transform.

.animate {
will-change: transform;
transition: transform 0.3s ease;
}
.animate:hover {
transform: scale(1.1);
}
Optimized Animation

Note: Use sparingly to avoid memory issues.

39. CSS Containment

Containment optimizes rendering by isolating elements.

Example: Strict containment.

.contained {
contain: strict;
width: 200px;
height: 100px;
background: #e5e7eb;
}
Contained Element

Note: Improves rendering performance.

40. CSS Motion Path

Motion path animates elements along a custom path.

Example: Path animation.

.motion {
offset-path: path('M0,100 Q100,0 200,100');
animation: move 3s linear infinite;
}
@keyframes move {
100% { offset-distance: 100%; }
}

Note: Limited browser support; use fallbacks.

Macro Nepal Helper