DHRUVANG Created with Sketch.
Published on

Lesser-Known CSS Tips and Tricks for Frontend Developers

CSS is an essential tool for frontend developers, but it’s easy to overlook some of its lesser-known features and techniques. These hidden gems can improve your workflows, enhance user experience, and solve tricky design problems. Here’s a collection of CSS tips and tricks you might not know about but should definitely explore.


1. The :is() Pseudo-Class

Simplify selectors by grouping them together using :is(). This reduces repetition and makes your CSS more maintainable.

:is(h1, h2, h3) {
    color: darkblue;
    font-weight: bold;
}

Instead of writing separate rules for h1, h2, and h3, the :is() pseudo-class combines them into one.


2. The Power of clamp()

The clamp() function helps create responsive values that stay within a defined range.

font-size: clamp(1rem, 2.5vw, 3rem);

Here, the font size starts at 1rem, scales with the viewport width (2.5vw), and stops growing at 3rem.


3. CSS Variables with Default Fallbacks

Use CSS variables with fallback values for greater flexibility and error-proofing.

:root {
    --primary-color: #007bff;
}

button {
    background-color: var(--primary-color, #0000ff); /* Falls back to blue */
}

4. aspect-ratio for Responsive Elements

Maintain a consistent aspect ratio for elements without relying on padding hacks.

img {
    aspect-ratio: 16 / 9;
    width: 100%;
    height: auto;
}

This ensures the image maintains a 16:9 ratio, regardless of its size.


5. Centering Made Easy with place-items

Use place-items to center content in a grid or flex container effortlessly.

.container {
    display: grid;
    place-items: center;
    height: 100vh;
}

This centers both horizontally and vertically in just one line.


6. Hover Within a Parent with :has()

Style parent elements based on child interactions using the :has() pseudo-class.

.card:has(:hover) {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

Here, the card gets a shadow effect when any of its children are hovered.


7. The :focus-within Pseudo-Class

Style parent elements when a child element receives focus.

.form-group:focus-within {
    border: 2px solid #007bff;
}

This is useful for highlighting form fields during user interaction.


8. Custom Borders with border-image

Apply intricate patterns or gradients as borders using border-image.

div {
    border: 10px solid;
    border-image: linear-gradient(to right, red, blue) 1;
}

9. Animating Gradients

Create mesmerizing gradient animations with background-position or background-size.

@keyframes gradient {
    0% {
        background-position: 0% 50%;
    }
    100% {
        background-position: 100% 50%;
    }
}

div {
    background: linear-gradient(270deg, #ff7eb3, #ff758c, #42a5f5);
    background-size: 400% 400%;
    animation: gradient 5s infinite linear;
}

10. Scroll Snap for Smooth Scrolling

Use scroll-snap to create smooth scroll experiences for carousels or lists.

.carousel {
    scroll-snap-type: x mandatory;
    overflow-x: scroll;
    display: flex;
}

.carousel > div {
    scroll-snap-align: center;
}

11. Line Clamping for Text Overflow

Limit the number of lines of text using -webkit-line-clamp.

p {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

This is perfect for truncating long text.


12. filter for Visual Effects

Apply effects like blur or grayscale directly to elements.

img {
    filter: grayscale(100%);
    transition: filter 0.3s;
}

img:hover {
    filter: none;
}

13. Smooth Scroll Behavior

Enable smooth scrolling for anchor links.

html {
    scroll-behavior: smooth;
}

14. Custom Pointer Cursor

Create custom cursors for a unique UI experience.

button {
    cursor: url('custom-cursor.png'), pointer;
}

15. Responsive Typography with ch Units

The ch unit adjusts based on the width of the 0 character, perfect for aligning text layouts.

input {
    width: 20ch;
}

Conclusion

These lesser-known CSS tips and tricks can save you time, improve performance, and enhance user experience. Incorporate them into your projects to level up your frontend development skills!