DEV Community

Mario
Mario

Posted on • Originally published at mariokandut.com on

Media queries with styled components

Styled components are visual primitives to style your React App and have plenty of great features, like the ability to write CSS right in the component, complexity reduction, faster loading, clear scope and other performance improvements.

This article is about how to use media queries in styled-components.

Quick refresher on media queries from MDN:

Media queries are useful when you want to modify your site or app depending on a device's general type (such as print vs. screen) or specific characteristics and parameters (such as screen resolution or browser viewport width).

A media query syntax looks something like this @media screen and (max-width: 768px) { SOME CONDITIONAL STYLING }.

I have some good news for you: Media queries with styled components just work the same as in good old CSS.

There are no code changes, adaptions or helper classes needed when writing media queries in styled components, though some best-practices should be considered.

Using @media with styled-components

When writing media queries in styled-components I follow these three steps, and I can only recommend this approach.

  1. Define breakpoints based on application needs.
  2. Define devices using media query syntax.
  3. Apply the media query in the styled component.

1. Define breakpoints

The first step should be a definition of the supported screen sizes in your application. As the needs in applications are different, a good starting point are the standard breakpoints in Google Chrome Dev Tools. Add or subtract breakpoints as for your specific need.

Let's create an object which holds the sizes.

const sizes = {
  mobileS: '320px',
  mobileM: '375px',
  mobileL: '425px',
  tablet: '768px',
  laptop: '1024px',
  laptopL: '1440px',
  desktop: '2560px',
};
Enter fullscreen mode Exit fullscreen mode

2. Define devices

Based on the sizes we can create a media query for each supported device. We can improve readability of the code, when using ES6 template strings.

export const devices = {
  mobileS: `(min-width: ${sizes.mobileS})`,
  mobileM: `(min-width: ${sizes.mobileM})`,
  mobileL: `(min-width: ${sizes.mobileL})`,
  tablet: `(min-width: ${sizes.tablet})`,
  laptop: `(min-width: ${sizes.laptop})`,
  laptopL: `(min-width: ${sizes.laptopL})`,
  desktop: `(min-width: ${sizes.desktop})`,
};
Enter fullscreen mode Exit fullscreen mode

3. Apply media query in styled-component

To apply the media query in a styled component just add it to the styling. Let's consider the following application.

const App = () => (
  <ProfilePage>
    <Card>
      <ProfileImg />
      <ProfileText />
    </Card>
  </ProfilePage>
);
Enter fullscreen mode Exit fullscreen mode

This is a pseudo-code of a profile page. The page should be responsive. The ProfilePage should have a different maximum width on laptop and desktop, and the Card should have the ProfileText below the ProfileImage on small devices.

import styled from 'styled-components';
import { device } from './device';

const ProfilePage = styled.div`
  margin: auto;
  text-align: center;

  @media ${device.laptop} {
    max-width: 800px;
  }

  @media ${device.desktop} {
    max-width: 1400px;
  }
`;
Enter fullscreen mode Exit fullscreen mode

For moving the text under the image in the Card component, flex-direction can be of help. When developing a web application starting with the smallest supported screen is always helpful.

const Card = styled.div`
  display: flex;
  flex-direction: column;
  margin: 0.5rem;

  @media ${device.laptop} {
    flex-direction: row;
  }
`;
Enter fullscreen mode Exit fullscreen mode

YaY , 🥳🥳🥳. That's it. Requirements fulfilled.

TL;DR

  • Media queries with styled components just work the same as in good old CSS!

Thanks for reading and if you have any questions , use the comment function or send me a message @mariokandut. If you want to know more about React, have a look at these React Tutorials.

References (and Big thanks):

Styled Components,JSRamblingsMDN - Using Media Queries

Top comments (0)