DEV Community

Ankit Verma
Ankit Verma

Posted on

CSS Tips and Tricks to Level Up Your Web Design

CSS (Cascading Style Sheets) is a powerful tool for styling web pages, but mastering it requires practice and knowledge of some lesser-known techniques. Whether you're a beginner or an experienced developer, these CSS tips and tricks will help you create cleaner, more efficient, and visually appealing designs.

  1. Use CSS Variables for Maintainability

CSS variables allow you to store reusable values like colors, fonts, and spacing, making your styles more manageable.

:root {
    --primary-color: #3498db;
    --secondary-color: #2ecc71;
    --font-size-base: 16px;
}

body {
    background-color: var(--primary-color);
    font-size: var(--font-size-base);
}
Enter fullscreen mode Exit fullscreen mode
  1. Utilize :has() for Parent Selection

The new :has() pseudo-class allows you to style parent elements based on their children.

div:has(img) {
    border: 2px solid red;
}
Enter fullscreen mode Exit fullscreen mode

This applies a border to any div that contains an img element.

  1. Center Elements with Flexbox and Grid

Use Flexbox to easily center elements both vertically and horizontally:

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}
Enter fullscreen mode Exit fullscreen mode

Or use CSS Grid:

.container {
    display: grid;
    place-items: center;
    height: 100vh;
}
Enter fullscreen mode Exit fullscreen mode
  1. Create Responsive Typography with clamp()

The clamp() function lets you define responsive font sizes without media queries:

h1 {
    font-size: clamp(1.5rem, 5vw, 3rem);
}
Enter fullscreen mode Exit fullscreen mode

This ensures that the font size stays between 1.5rem and 3rem, adjusting dynamically based on the viewport width.

  1. Add Smooth Hover Effects with transition

Enhance user experience by adding smooth hover effects:


button {
    background-color: #3498db;
    transition: background-color 0.3s ease-in-out;
}

button:hover {
    background-color: #2ecc71;
}
Enter fullscreen mode Exit fullscreen mode
  1. Use aspect-ratio for Consistent Element Dimensions

The aspect-ratio property helps maintain proportional sizing for elements like images and videos.

.image-container {
    width: 100%;
    aspect-ratio: 16 / 9;
}
Enter fullscreen mode Exit fullscreen mode
  1. Apply scroll-snap for Smooth Scrolling

Improve scrolling experience with scroll-snap-type and scroll-snap-align:

.container {
    scroll-snap-type: y mandatory;
    overflow-y: scroll;
    height: 100vh;
}

.section {
    scroll-snap-align: start;
}
Enter fullscreen mode Exit fullscreen mode
  1. Use backdrop-filter for Glassmorphism Effects

The backdrop-filter property creates stunning blurred backgrounds:

.glass-effect {
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 10px;
}
Enter fullscreen mode Exit fullscreen mode
  1. Optimize Performance with will-change

If you have animations, will-change can hint to the browser to optimize rendering.

div {
    will-change: transform, opacity;
}
Enter fullscreen mode Exit fullscreen mode
  1. Hide Scrollbars but Keep Scrolling Functional

If you want a clean design without visible scrollbars:

.container {
    overflow: auto;
    scrollbar-width: none; /* Firefox */
}

.container::-webkit-scrollbar {
    display: none; /* Chrome, Safari */
}
Enter fullscreen mode Exit fullscreen mode

CSS is constantly evolving, offering new ways to create beautiful, efficient, and responsive web designs. By applying these tips and tricks, you can optimize your styles, enhance user experience, and make your CSS workflow smoother. 🚀

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more