Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

styled-breakpoints

mg90162.7kMIT14.1.5TypeScript support: included

Simple and powerful css breakpoints for styled-components and emotion

media, query, media-query, media-queries, styled, react, javascript, css-in-js, breakpoint, breakpoints, css-in-react, typescript, styled-media-query, styled media query, styled components, styled-components

readme


styled-breakpoints
GitHub Workflow Status coverage status npm bundle size npm downloads npm version

Simple and powerful tool for creating media queries with SSR support.

Styled Components Logo     OR    Emotion logo



🌼 Preview

Inside components.

const Box = styled.div`
  background-color: pink;

  ${({ theme }) => theme.breakpoints.up('sm')} {
    background-color: hotpink;
  }

  ${({ theme }) => theme.breakpoints.up('md')} {
    background-color: red;
  }
`;

Outside components.

import { useTheme } from 'styled-components'; // or '@emotion/react'

const Layout = () => {
  const { up } = useTheme().breakpoints;
  const isMd = useMediaQuery(up('md'));

  return <>{isMd && <Box />}</>;
};

Examples

👉🏻 Mobile First

From smallest to largest


👉🏻 Desktop First

From largest to smallest


👉🏻 Hooks API



📖 Documentation


🧐 Core concepts

  • Breakpoints act as the fundamental elements of responsive design. They enable you to control when your layout can adapt to a specific viewport or device size.

  • Utilize media queries to structure your CSS based on breakpoints. Media queries are CSS features that allow you to selectively apply styles depending on a defined set of browser and operating system parameters. The most commonly used media query property is min-width.

  • The objective is mobile-first, responsive design. Styled Breakpoints aims to apply the essential styles required for a layout to function at the smallest breakpoint. Additional styles are then added to adjust the design for larger devices. This approach optimizes your CSS, enhances rendering speed, and delivers an excellent user experience.


Getting Started

🚩 Installation

npm install styled-breakpoints@latest

# or

yarn add styled-breakpoints@latest

Configuration

🚩 File Structure

 theme/
 ├── index.ts
 └── styled.d.ts // or emotion.d.ts
 app.tsx

🚩 Available breakpoints

Styled Breakpoints includes six default breakpoints, often referred to as grid tiers, for building responsive designs. These breakpoints can be customized.

const breakpoints = {
  xs: '0px',
  sm: '576px',
  md: '768px',
  lg: '992px',
  xl: '1200px',
  xxl: '1400px',
};

Each breakpoint has been carefully selected to accommodate containers with widths that are multiples of 12. The breakpoints also represent a subset of common device sizes and viewport dimensions, although they do not specifically target every use case or device. Instead, they provide a robust and consistent foundation for building designs that cater to nearly any device.


🚩 Default Configuration

theme/index.ts

import { createStyledBreakpointsTheme } from 'styled-breakpoints';

export const theme = createStyledBreakpointsTheme();


Customization

🚩 Breakpoints

theme/index.ts

import { createStyledBreakpointsTheme } from 'styled-breakpoints';

export const theme = createStyledBreakpointsTheme({
  breakpoints: {
    small: '100px',
    medium: '200px',
    large: '300px',
    xLarge: '400px',
    xxLarge: '500px',
  },
});

🎨 Merge with Another Theme

theme/index.ts

import { createStyledBreakpointsTheme } from 'styled-breakpoints';

export const primaryTheme = {
  fonts: ['sans-serif', 'Roboto'],
  fontSizes: {
    small: '1em',
    medium: '2em',
    large: '3em',
  },
} as const;

export const theme = {
  ...primaryTheme,
  ...createStyledBreakpointsTheme(),
};


💅 Using with Styled Components

🚩 Installation
npm install styled-components

# or

yarn add styled-components

theme/styled.d.ts

import 'styled-components';
import { theme } from './index';

type ThemeConfig = typeof theme;

declare module 'styled-components' {
  export interface DefaultTheme extends ThemeConfig {}
}


<g-emoji class="g-emoji" alias="woman_singer" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f469-1f3a4.png">👩‍🎤</g-emoji> Using with Emotion

🚩 Installation
npm install @emotion/{styled,react}

# or

yarn add @emotion/{styled,react}

theme/emotion.d.ts

import '@emotion/react';
import { theme } from './index';

type ThemeConfig = typeof theme;

declare module '@emotion/react' {
  export interface Theme extends ThemeConfig {}
}


🚀 Integration to Your App


app.tsx

import styled { ThemeProvider } from 'styled-components'; // or '@emotion/react'
import { theme } from './theme';

const Box = styled.div`
  display: none;

  ${({ theme }) => theme.breakpoints.up('sm')} {
    display: block;
  }
`;

const App = () => (
  <ThemeProvider theme={theme}>
    <Box />
  </ThemeProvider>
);

Media queries API

🚀 Caching is implemented in all functions to optimize performance.


👉🏻 Min-width - up

const Box = styled.div`
  display: none;

  ${({ theme }) => theme.breakpoints.up('sm')} {
    display: block;
  }
`;

Will convert to vanilla css:
@media (min-width: 768px) {
  display: block;
}


👉🏻 Max-width - down

We occasionally use media queries that go in the other direction (the given screen size or smaller):


const Box = styled.div`
  display: block;

  ${({ theme }) => theme.breakpoints.down('md')} {
    display: none;
  }
`;

Will convert to vanilla css:
@media (max-width: 767.98px) {
  display: none;
}

Why subtract .02px? Browsers don’t currently support range context queries, so we work around the limitations of min- and max- prefixes and viewports with fractional widths (which can occur under certain conditions on high-dpi devices, for instance) by using values with higher precision.



👉🏻 Single breakpoint - only

There are also media queries and mixins for targeting a single segment of screen sizes using the minimum and maximum breakpoint widths.


const Box = styled.div`
  background-color: pink;

  ${({ theme }) => theme.breakpoints.only('md')} {
    background-color: rebeccapurple;
  }
`;

Will convert to vanilla css:
@media (min-width: 768px) and (max-width: 991.98px) {
  background-color: rebeccapurple;
}


👉🏻 Breakpoints range - between

Similarly, media queries may span multiple breakpoint widths.


const Box = styled.div`
  background-color: gold;

  ${({ theme }) => theme.breakpoints.between('md', 'xl')} {
    background-color: rebeccapurple;
  }
`;

Will convert to vanilla css:
@media (min-width: 768px) and (max-width: 1199.98px) {
  background-color: rebeccapurple;
}


👉🏻 useMediaQuery hook

features:

  • 🧐 optimal performance by dynamically monitoring document changes in media queries.
  • 💪🏻 It supports SSR (server-side rendering).
  • 📦 Minified and gzipped size 284b.

<summary>Type declaration</summary>
 declare function useMediaQuery(query: string) => boolean
import { useTheme } from 'styled-components'; // or from '@emotion/react'
import { useMediaQuery } from 'styled-breakpoints/use-media-query';
import { Box } from 'third-party-library';

const SomeComponent = () => {
  const { only } = useTheme().breakpoints;
  const isMd = useMediaQuery(only('md'));

  return <AnotherComponent>{isMd && <Box />}</AnotherComponent>;
};


License

MIT License

Copyright (c) 2018-2024 Maxim Alyoshin.

This project is licensed under the MIT License - see the LICENSE file for details.



Contributors

mg901
mg901

💬 💻 🎨 📖 💡 🚇 🚧 📆 📣 🔬 👀 ⚠️ 🔧
Abu Shamsutdinov
Abu Shamsutdinov

🐛 💻 💡 🤔 👀 📢
Sova
Sova

💻 💡 🤔 👀 📢
Jussi Kinnula
Jussi Kinnula

🐛 💻
Rafał Wyszomirski
Rafał Wyszomirski

📖
Adrian Celczyński
Adrian Celczyński

🐛 💻
Sam Holmes
Sam Holmes

💻 🤔
Ontopic
Ontopic

🤔
Ryan Bell
Ryan Bell

🤔
Bart Nagel
Bart Nagel

🐛 💻 💡 🤔
Greg McKelvey
Greg McKelvey

💻
Buck DeFore
Buck DeFore

🤔
Pierre Burel
Pierre Burel

🐛
Pawel Kochanek
Pawel Kochanek

🐛 💻
Ian Christopher B. de Jesus
Ian Christopher B. de Jesus

🐛
David de Lusenet
David de Lusenet

🐛 🤔
Dan Adler
Dan Adler

🐛

changelog

14.1.5 (2025-05-11)

Bug Fixes

  • readme: set correct path for github workflow status (#2087) (20c96fc)

14.1.4 (2024-08-18)

Bug Fixes

14.1.3 (2024-04-12)

Bug Fixes

  • use-media-query: hook doesn't observe viewport changes (#1853) (242e849)

14.1.2 (2024-03-25)

Bug Fixes

14.1.2 (2024-03-23)

Bug Fixes

14.1.1 (2024-03-23)

Bug Fixes

14.1.0 (2024-03-05)

Features

  • 🎸 improve type inference for orientation (#1803) (727f647)

14.0.0 (2024-02-08)

Features

  • use-media-query: no longer supports Safari < 14 (#1782) (f50a5a1)

BREAKING CHANGES

  • use-media-query: 🧨 useMediaQuery hook no longer supports Safari < 14

  • test: add istanbul ignore comment

  • refactor(use-media-query): replace state with matches

  • test: add instnbul ignore comment

13.1.4 (2024-02-07)

Bug Fixes

13.1.3 (2024-02-07)

Bug Fixes

13.1.2 (2024-02-04)

Bug Fixes

13.1.1 (2024-02-04)

Bug Fixes

13.1.0 (2024-02-04)

Features

  • docs: add an example for custom breakpoints (#1772) (d65e3d8)

13.0.0 (2024-02-04)

Features

BREAKING CHANGES

  • 🧨 remove StyledBreakpointsTheme & MediaQueries types

  • build: add semantic-release-githubsquash plugin

12.1.10 (2024-01-17)

Bug Fixes

12.1.9 (2023-11-12)

Bug Fixes

  • fix the infinite loop during orientation validation (#1668) (c8f7ae6)

12.1.8 (2023-11-08)

Bug Fixes

  • optimize 'only' method for O(1) performance (#1657) (3c8c92c)

12.1.7 (2023-11-08)

Bug Fixes

  • styled-breakpoints: remove shared cache for public methods (#1656) (ad2ae03)

12.1.6 (2023-11-04)

Bug Fixes

  • remove styled-components from dev-deps (efa2a55)

12.1.5 (2023-07-20)

Bug Fixes

  • docs: add [@latest](https://github.com/latest) tag to the installation instructions (#1562) (b914155)

12.1.4 (2023-07-19)

Bug Fixes

12.1.3 (2023-07-18)

Bug Fixes

12.1.2 (2023-07-08)

Bug Fixes

  • docs: reorganize documentation structure (#1546) (b70a1e9)

12.1.1 (2023-06-11)

Bug Fixes

  • docs: add import of MediaQueries type to Strict Typed Breakpoints section (1799035)

12.1.0 (2023-06-09)

Features

  • add strict types for default and custom media queries (8794580), closes #1390

12.0.4 (2023-05-29)

Bug Fixes

  • fix a bug that occurred when passing custom breakpoints (225ee5b)

12.0.3 (2023-05-23)

Bug Fixes

  • add banner in support of Ukraine (c6bab5a)
  • fix typography in documentation (acb747c)
  • fix typography in documentation (#1504) (e2611b0)

12.0.2 (2023-05-23)

Bug Fixes

  • fix broken link to useMediaQuery hook (51ee3d6)

12.0.1 (2023-05-23)

Bug Fixes

  • add guide for migration from useBreakpoint to useMediaQuery (3beb3d3)

12.0.0 (2023-05-23)

Features

  • add docs for v12 (1b8d610)
  • add useMediaQuery hook (dcceefb)
  • add correct types for createStyledBreakpointsTheme (86fe548)
  • add correct types for DefaultStyledBreakpointsTheme (#1494) (fcf7463)
  • add correct types for styled breakpoints (1d4bc62)
  • remove createTheme, up, down, between, only (6b9ac35), closes #1453 #1449 #1486
  • set options as optional argument (#1493) (f35715a)

BREAKING CHANGES

  • remove functions createTheme, up, down, between, only,useBreakpoint. Remove react-styled and react-emotion directoies.

12.0.0-beta.7 (2023-05-23)

Features

12.0.0-beta.6 (2023-05-12)

Features

  • add correct types for styled breakpoints (1d4bc62)

12.0.0-beta.5 (2023-05-11)

Features

  • add correct types for DefaultStyledBreakpointsTheme (#1494) (fcf7463)

12.0.0-beta.4 (2023-05-11)

Features

12.0.0-beta.3 (2023-05-11)

Features

  • add correct types for createStyledBreakpointsTheme (86fe548)

12.0.0-beta.2 (2023-05-11)

Features

12.0.0-beta.1 (2023-05-11)

Features

BREAKING CHANGES

  • remove functions createTheme, up, down, between, only,useBreakpoint. Remove react-styled and react-emotion directoies.

11.2.3 (2023-04-27)

Bug Fixes

  • only: remove incorrect validation for the minimum breakpoint (#1479) (b8b9040)

11.2.2 (2023-04-27)

Bug Fixes

  • between: remove incorrect validation for the minimum value (#1475) (66418e0)

11.2.1 (2023-04-26)

Bug Fixes

  • down: remove validation for last breakpoint (#1472) (f55e97f)

11.2.0 (2023-04-25)

Features

  • down: add validation for last breakpoint (8e38d90)

11.1.5 (2023-04-25)

Bug Fixes

  • up: remove incorrect validation when breakpoint is equal zero (ae47e05)

Reverts

11.1.2 (2023-04-18)

Bug Fixes

  • change url for build badge (ad19573)

11.1.1 (2022-07-01)

Bug Fixes

11.1.0 (2022-05-06)

Features

11.0.5 (2022-03-31)

Bug Fixes

11.0.4 (2022-01-23)

Bug Fixes

  • use-breakpoint: window access for ssr error fix (#1154) (888ecc2)

11.0.3 (2022-01-19)

Bug Fixes

  • use-breakpoint: remove rules-of-hooks warning (#1148) (17c1c88)

11.0.2 (2022-01-19)

Bug Fixes

  • add type declaration for createTheme (34c3023)

11.0.1 (2022-01-19)

Bug Fixes

  • fix export for createStyledBreakpoints function (54f6f9d)

11.0.0 (2022-01-19)

Code Refactoring

  • up and between functions (1a6bf94)

BREAKING CHANGES

  • add createTheme function and rewrite up and between functions

10.2.3 (2022-01-11)

Bug Fixes

10.2.2 (2022-01-11)

Bug Fixes

10.2.1 (2021-12-22)

Bug Fixes

10.2.0 (2021-12-20)

Features

  • add another default breakpoint xxl: 1400px, remove conversion… (#1115) (6fc1bac)

10.1.1 (2021-12-19)

Bug Fixes

  • fixed behavior when useBreakpoint could not work without ThemeProvider (4235a79)

10.1.0 (2021-12-19)

Features

10.0.1 (2021-05-27)

Bug Fixes

10.0.0 (2021-03-03)

Bug Fixes

  • peer-deps: replace react "^16.8.0" with "^17.0.0" #916 (938dbff)

BREAKING CHANGES

  • no breaking changes. Update React to version ^17.x.x.

9.0.12 (2021-02-25)

Bug Fixes

  • use-brekpoint: move the call of the useTheme hook to useBreakpoint (8a08fdb)

9.0.11 (2021-02-25)

Bug Fixes

  • use-brekpoint: useBreakpoint works without theme (#911) (c58e505)

9.0.10 (2021-02-24)

Bug Fixes

9.0.9 (2021-01-12)

Bug Fixes

  • add exception for MediaQueryList event listener for Safari < 14 (8b989f0)

9.0.8 (2020-12-06)

Bug Fixes

  • use-breakpoint: eliminated erroneous rehydration after server si… (#869) (385d4e1)

9.0.7 (2020-12-05)

Bug Fixes

9.0.6 (2020-12-05)

Bug Fixes

9.0.5 (2020-12-05)

Bug Fixes

9.0.4 (2020-12-05)

Bug Fixes

  • fix typo in package.json on 'down' import (#861) (0caa31f)

9.0.3 (2020-12-01)

Bug Fixes

9.0.2 (2020-11-30)

Bug Fixes

9.0.1 (2020-11-16)

Bug Fixes

  • readme: fix the link to desktop first (7c3e867)

9.0.0 (2020-11-13)

Code Refactoring

  • replaced default breakpoints (76bc360)

Features

  • add support for emotion (a4c612c)

BREAKING CHANGES

  • replaced default breakpoints

8.2.3 (2020-11-11)

Bug Fixes

  • readme: add build status (084f643)

8.2.2 (2020-11-11)

Bug Fixes

  • fix: remove package-lock.json (de767aa)

8.2.1 (2020-11-11)

Bug Fixes

  • rename a direactory react-styled with react (e928b6b)

8.2.0 (2020-11-11)

Features

  • add useBreakpoint hook for react & styled-components (#833) (708856e)

8.1.3 (2020-11-11)

Bug Fixes

  • set correct types for useBreakpoint hook (#829) (86e12a5)

8.1.2 (2020-10-03)

Bug Fixes

Bug Fixes

  • change Props theme type (#616) (654f761)
  • replace invariant with throwError function (e935dc0)

8.1.1 (2020-05-09)

Bug Fixes

8.1.0 (2020-03-15)

Features

  • Add example for using the library with object notation (#533) (0db488d)

8.0.1 (2020-02-26)

Bug Fixes

  • build: import from types instead of types.d to fix build (#513) (0595aeb)

8.0.0 (2020-02-26)

chore

  • update node.js to 12.6.1 LTS (c778a00)

BREAKING CHANGES

  • update node.js to 12.6.1 LTS

7.2.0 (2020-01-04)

Features

7.1.0 (2019-11-26)

Features

  • reduce size from 1.16kb to 958b (30b396c)

7.0.0 (2019-11-18)

Code Refactoring

  • replace interfaces with types (0cab390)

BREAKING CHANGES

  • replace interfaces IProps, IMediaQueries, IOptions, with types Props, MediaQueries, Options

6.11.0 (2019-11-12)

Features

  • add export for interfaces (bcc67a0)

6.10.1 (2019-11-12)

Bug Fixes

  • fix entry point for modules (f8ff3a5)

6.10.0 (2019-11-12)

Features

6.9.1 (2019-10-19)

Bug Fixes

  • fix typings for typescript (3ce3916)

6.9.0 (2019-09-15)

Bug Fixes

  • readme: fix shield for bundle size (96dcc5f)

Features

  • demo: add demo with typescript (4a6a6b1)

6.8.0 (2019-09-15)

Features

6.7.3 (2019-09-12)

Bug Fixes

  • remove greenkeeper badge (dd8c27e)

6.7.2 (2019-08-28)

Bug Fixes

  • fix types for typescript (2396772)

6.7.1 (2019-07-20)

Bug Fixes

  • nvm: bump node version from (326b154)

6.7.0 (2019-07-20)

Features

  • orientation: add orientation (09584cf)

6.6.4 (2019-07-15)

Bug Fixes

  • add link to issue#4 (326fc91)
  • fix error message for "above", "between" (df1965b)

6.6.3 (2019-05-07)

Bug Fixes

6.6.2 (2019-03-30)

Bug Fixes

6.6.1 (2019-03-30)

Bug Fixes

6.6.0 (2019-03-30)

Features

  • add test for last breakpoint (02969de)

6.5.8 (2019-03-30)

Bug Fixes

  • types: add types for es and lib folder (f68c1df)

6.5.7 (2019-03-30)

Bug Fixes

6.5.6 (2019-03-30)

Bug Fixes

  • error-messages: fix error messages and remove (74dc45f)

6.5.5 (2019-03-23)

Bug Fixes

  • package: update igogo to version 1.7.1 (2be1cb2)

6.5.4 (2019-03-16)

Bug Fixes

  • package: update igogo to version 1.7.0 (d52f807)

6.5.3 (2019-03-16)

Bug Fixes

6.5.2 (2019-03-15)

Bug Fixes

  • readme: mention support for ts and flow (f7bdba4)

6.5.1 (2019-03-15)

Bug Fixes

6.5.0 (2019-03-13)

Features

  • typescript: add types for typescript (b4782df)

6.4.3 (2019-02-21)

Bug Fixes

  • package: update igogo to version 1.6.0 (3f18323)

6.4.2 (2019-02-21)

Bug Fixes

  • package: update igogo to version 1.5.0 (7967952)

6.4.1 (2019-02-09)

Bug Fixes

6.4.0 (2019-02-08)

Features

  • add an API description to the readme (8dbfdc2)

6.3.0 (2019-02-08)

Features

6.2.0 (2019-01-25)

Features

  • src: add custom msg for error (638b573)

6.1.5 (2019-01-22)

Bug Fixes

  • helpers: fix flow-types (580e59a)

6.1.4 (2019-01-19)

Bug Fixes

6.1.3 (2019-01-18)

Bug Fixes

  • travis: change npm token (2eb9736)

6.1.2 (2019-01-17)

Bug Fixes

  • remove postbuild script (2e7343d)

6.1.1 (2019-01-17)

Bug Fixes

  • travis: fix build and postbuild scripts (fc67aa8)

6.1.0 (2019-01-17)

Features

6.0.0 (2019-01-14)

Code Refactoring

  • public api: remove above and below functions (ca29bcc)

BREAKING CHANGES

  • public api: replace 'above' fn with 'up' and 'below' with 'down'

5.0.0 (2019-01-14)

Bug Fixes

  • flow: fix imported types (9f8277e)

Features

  • user-api: change user-api (10fb828)

BREAKING CHANGES

  • user-api: Remove function createBreakpoints. Now the breakpoints are announced in the theme provider.