React MUI Breakpoints, from the Material-UI library, are used to define different styles for different screen sizes.
Material UI Breakpoints
MUI Breakpoints are based on the standard CSS breakpoints and are used to adjust the layout and design of a website or application when viewed on different devices. Each breakpoint corresponds to a specific screen width range and allows developers to create responsive designs that adapt to different screen sizes.
React MUI Breakpoints Syntax:
[theme.breakpoints.style(key)]: {
cssProperty: value,
}
React MUI Breakpoints Default Values:
- xs (extra-small: Gets triggered when the screen size is above 0px.
- sm (small): Gets triggered when the screen size is above or equal to 600px.
- md (medium): Gets triggered when the screen size is above or equal to 900px.
- lg (large): Gets triggered when the screen size is above or equal to 1200px.
- xl: (extra-large): Gets triggered when the screen size is above or equal to 1536px.
Media queries in CSS are used to apply different styles to a webpage based on the characteristics of the device displaying it. They allow developers to create responsive designs that adapt to different screen sizes, orientations, and other features of the device. To that end, the theme includes five style assistants:
- theme.breakpoints.up(key): Gets triggered at any value above or equal to the 'key' provided.
- theme.breakpoints.down(key): Gets triggered at any value below or equal to the 'key' provided.
- theme.breakpoints.only(key): Gets triggered only at the value of the 'key' provided.
- theme.breakpoints.not(key): Gets triggered at any value above or below the 'key' provided but not at the exact value of the key.
- theme.breakpoints.between(start, end): Gets triggered in between the range of the keys ( start and end ) provided.
A JavaScript media query is a way to use JavaScript to check the characteristics of the device displaying a webpage, such as its screen size or resolution. This allows developers to create responsive designs that adapt to different devices by applying different styles or behavior to elements on the page based on the characteristics of the device.
JavaScript Media Queries Syntax:
const Breakpoint = useMediaQuery('(cssProperty: value)');
The above syntax gives 'true' when the 'MediaQuery' gets a match and 'false' otherwise.
Custom Breakpoints in React MUI Breakpoints
Custom breakpoints are additional breakpoints that are defined and used in addition to the default breakpoints provided by Material-UI. These breakpoints are used to create more specific styles or layouts based on the screen size.
- theme.breakpoints.values: Refers to the minimum width value of for the given screen name keys.
- theme.breakpoints.unit: Refers to the unit like "px", "rem" etc.
- theme.breakpoint.step: Its value divided by 100 is used to implements the exclusive breakpoints
Custom breakpoints can be defined using the createTheme() function, which allows developers to specify their own values for the breakpoints.
React MUI Custom Breakpoints Syntax:
const theme = createTheme({
breakpoints: {
values: {
key1: value,
// for example
sm: 400,
//or
mobile: 200,
laptop: 1000,
},
},
});
React MUI Breakpoints Examples:
Lets see some example to use the React MUI BreakPoints.
Example 1: JavaScript Media Queries with custom breakpoints
Below is the code to use JavaScript Media Queries with custom breakpoints.
JavaScript
// Filename - App.js
import * as React from 'react';
import useMediaQuery from '@mui/material/useMediaQuery';
import { Button, TextField, Box, Snackbar } from '@mui/material';
const SimpleMediaQuery = () => {
const Desktop = useMediaQuery('(min-width:1200px)');
const Ipad = useMediaQuery('(min-width:1000px)');
const Mobile = useMediaQuery('(min-width:800px)');
return (
<Box sx={{
margin: '25px', marginTop: '100px',
display: 'flex', flexDirection: Mobile ? 'row' : 'column'
}}>
<TextField label='Enter Name' sx={{ width: '250px' }} />
<Button variant='contained'
sx={{ margin: '10px', width: '100px' }}>
Submit
</Button>
<Snackbar
anchorOrigin={{ vertical: 'top', horizontal: 'left' }}
open={!Desktop}
onClose={Desktop}
autoHideDuration={2000}
message={!Desktop ? 'This is Desktop Screen' : ''}
key='1'
/>
<Snackbar
anchorOrigin={{ vertical: 'top', horizontal: 'left' }}
open={!Ipad}
onClose={Ipad}
autoHideDuration={2000}
message={!Ipad ? 'This is Ipad Screen' : ''}
key='2'
/>
<Snackbar
anchorOrigin={{ vertical: 'top', horizontal: 'left' }}
open={!Mobile}
onClose={Mobile}
autoHideDuration={2000}
message={!Mobile ? 'This is Mobile Screen' : ''}
key='3'
/>
</Box>
)
};
export default SimpleMediaQuery;
Output:
JavaScript Media Queries with custom breakpoints Output
Example 2: React MUI Breakpoints with default values
In the below example, we will be using the 'sx' props and default breakpoints in a different way with 'flex' property.
JavaScript
// Filename - App.js
import { Box } from '@mui/material';
const breakpoints = {
border: "1px solid black",
margin: 2,
flex: { xs: "100%", sm: "calc(50% - 50px)",
md: "calc(33% - 50px)", lg: "calc(25% - 50px)" },
};
const Break = () => {
return (
<Box sx={{ display: "flex", flexWrap: "wrap",
textAlign: 'center' }}>
<Box sx={breakpoints}>One</Box>
<Box sx={breakpoints}>Two</Box>
<Box sx={breakpoints}>Three</Box>
<Box sx={breakpoints}>Four</Box>
</Box>
);
}
export default Break;
Output:
React MUI Breakpoints with default values Output
Reference:
https://mui.com/material-ui/customization/breakpoints/
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects