DEV Community

Cover image for Respecting User Preferences with Media Queries
Felipe Genuino
Felipe Genuino

Posted on

Respecting User Preferences with Media Queries

Media Queries are an essential part of responsive web development, allowing developers to adapt a web page's layout and style based on device characteristics and user preferences. It's crucial to ensure an optimized user experience, considering factors like screen size, orientation, preferred theme, and even data savings. In this context, let's explore how to use Media Queries to respect user preferences.

Preferred Theme (Light/Dark Mode)

User preference for a light or dark theme can be respected through Media Queries. With increased awareness of the importance of reducing eye strain, it's vital to offer a theme option that suits the user's preference. Media Queries enable detecting the color scheme preference (prefers-color-scheme) and adjusting the page's style accordingly.

/* Style for light theme */
@media (prefers-color-scheme: light) {
    body {
        background-color: white;
        color: black;
    }
}

/* Style for dark theme */
@media (prefers-color-scheme: dark) {
    body {
        background-color: black;
        color: white;
    }
}
Enter fullscreen mode Exit fullscreen mode

Adapting to Screen Size

Another way to respect user preferences is to ensure that the page layout adapts according to the device's screen size. This can be achieved using Media Queries to define specific style rules for different screen sizes.

/* Style for small screens (mobile) */
@media (max-width: 767px) {
    /* Specific style for small screens */
}

/* Style for medium screens (tablets) */
@media (min-width: 768px) and (max-width: 1023px) {
    /* Specific style for tablets */
}

/* Style for large screens (desktop) */
@media (min-width: 1024px) {
    /* Specific style for desktop */
}
Enter fullscreen mode Exit fullscreen mode

Considering Device Orientation

The device's orientation (portrait or landscape) can be an important preference for many users. Use Media Queries to ensure your layout is suitable for both orientations.

/* Style for portrait orientation */
@media (orientation: portrait) {
    /* Specific style for portrait orientation */
}

/* Style for landscape orientation */
@media (orientation: landscape) {
    /* Specific style for landscape orientation */
}
Enter fullscreen mode Exit fullscreen mode

Considering Reducing Animations

prefers-reduced-motion: reduce is a valuable option to ensure a more user-friendly experience for those who prefer or need less movement in web interfaces. By using this media query, developers can dynamically modify the behavior of animations and transitions, reducing them or even removing them completely.

This contributes to making the digital environment more accessible and comfortable for people who may be sensitive to intense motions or who seek a quieter experience while browsing the web.

The example illustrates how to adjust transitions and even hide animated elements in compliance with this preference, demonstrating a commitment to accessibility and inclusion in design.

 :root{
        --transitionTime: 0.2s;
    }
    @media (prefers-reduced-motion: reduce) {
        #tsparticles { display:none; }
        :root{
            --transitionTime: 0s;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Conclusion

Respecting user preferences is a crucial aspect of modern web development. Media Queries are a powerful tool to achieve this goal, allowing for the adaptation of layout and style according to user needs and choices.

By integrating Media Queries effectively, we can create a more personalized and enjoyable user experience, considering factors such as preferred theme, screen size, and device orientation.

Remember, the key is to offer flexibility and options to the user, ensuring that your web application is truly inclusive and adapts to diverse user preferences and contexts. 🌟

Top comments (0)