SoFunction
Updated on 2025-04-06

Best practices for React to implement responsive layouts

What is a responsive layout?

Responsive layout is a web design technology designed to ensure that web pages can be adaptively displayed on various devices (such as mobile phones, tablets, PCs). By using elastic grids, flexible pictures and CSS media queries, such a layout can be achieved, so that users can get a good user experience no matter what device they use.

Responsive layout implementation method in React

In React, there are many ways to implement responsive layout. Here are several commonly used methods:

1. Using CSS Flexbox and Grid

CSS Flexbox and Grid are powerful tools for implementing responsive layouts. Here is a simple example of using Flexbox:

import React from 'react';
import './'; // Import style files
const App = () => {
  return (
    <div className="container">
      <div className="box">1</div>
      <div className="box">2</div>
      <div className="box">3</div>
      <div className="box">4</div>
    </div>
  );
};

export default App;

existIn this article, we use Flexbox to create a responsive layout:

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.box {
  background-color: lightblue;
  margin: 10px;
  width: 20%; /* Each box accounts for 20% width */
  padding: 20px;
  text-align: center;
}

2. Use CSS media query

CSS media query is another important tool for implementing responsive layouts. It allows us to apply different styles according to different characteristics of the device (such as width, height, direction, etc.).

Here is a basic media query example:

.box {
  width: 100%; /* By default, the box accounts for 100% width */
}

@media only screen and (min-width: 600px) {
  .box {
    width: 48%; /* When the screen width is greater than 600px, the box accounts for 48% of the width */
  }
}

@media only screen and (min-width: 900px) {
  .box {
    width: 23%; /* When the screen width is greater than 900px, the box accounts for 23% of the width */
  }
}

With the above code snippet, the box automatically adjusts its width to fit the screen when the viewport width changes.

3. Use third-party libraries

In React, there are some plug-ins that can help us easily implement responsive layouts, e.g.react-bootstrapstyled-componentswait. The following is usedreact-bootstrapExamples of implementing responsive grid layout:

First, we need to installreact-bootstrapandbootstrap

npm install react-bootstrap bootstrap

Then we can use it in the component:

import React from 'react';
import { Container, Row, Col } from 'react-bootstrap';
import 'bootstrap/dist/css/'; // Import Bootstrap style
const App = () => {
  return (
    <Container>
      <Row>
        <Col xs={12} sm={6} md={4}>
          <div className="box">1</div>
        </Col>
        <Col xs={12} sm={6} md={4}>
          <div className="box">2</div>
        </Col>
        <Col xs={12} sm={6} md={4}>
          <div className="box">3</div>
        </Col>
      </Row>
    </Container>
  );
};

export default App;

Here, usereact-bootstrapofColComponents can easily implement responsive column layouts, specifying the number of columns under different screen widths.

4. Using CSS-in-JS solution

With the popularity of CSS modularity, use CSS-in-JS solutions (e.g.styled-components) has become a common practice. Here is an example of how to use it to implement a responsive layout:

Install firststyled-components

npm install styled-components

Then use in the component:

import React from 'react';
import styled from 'styled-components';

const Container = `
  display: flex;
  flex-wrap: wrap;
`;

const Box = `
  background-color: lightblue;
  margin: 10px;
  flex: 1 1 100%; /* By default, the box accounts for 100% width */

  @media (min-width: 600px) {
    flex: 1 1 48%; /* When the screen width is greater than 600px, the box accounts for 48% of the width */
  }

  @media (min-width: 900px) {
    flex: 1 1 23%; /* When the screen width is greater than 900px, the box accounts for 23% of the width */
  }
`;

const App = () => {
  return (
    <Container>
      <Box>1</Box>
      <Box>2</Box>
      <Box>3</Box>
    </Container>
  );
};

export default App;

Summarize

Implementing responsive layout is particularly important in modern web development. In React, we can achieve this by using CSS Flexbox, Grid, media queries, third-party libraries, and CSS-in-JS solutions. Depending on the specific needs of the project, we can choose the most appropriate method to ensure that our web pages provide an excellent user experience on a variety of devices.

As the React ecosystem continues to evolve, responsive design approaches are constantly being updated, so maintaining attention to new technologies and best practices is something every developer must do.

This is the article about the best practices of React to implement responsive layout. For more relevant React responsive layout content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!