Open In App

React Bootstrap Grid

React Bootstrap Grid is like the maestro of web page layouts in React Bootstrap. It’s your go-to buddy for crafting designs that look good and adapt seamlessly across devices. From straightforward single-column setups to intricate multi-column arrangements, this tool’s got the flexibility and power to make it happen.

There are two main approaches to implement React Bootstrap Grid:



Using Grid component:

The Grid component is the main component for creating a grid layout.



It takes a number of props that can be used to control the layout of the grid. Here are some of the most important props:

Using the Row and Col components:

The Row and Col components are used to create rows and columns within the grid. The Row component defines a new row in the grid, and the Col component defines a column within that row.

The Col component takes a number of props that can be used to control the layout of the column. Here are some of the most important props:

Fluid Container:

Use <Container fluid /> for width: 100% across all viewport and device sizes.

Example: Below is the code example of the Fluid Container:




import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
 
function ExampleContainer() {
    return (
        <Container fluid>
            <Row>
                <Col className="bg-secondary">A fluid container</Col>
            </Row>
        </Container>
    );
}
 
export default ExampleContainer;

Output:

Auto-layout columns:

If column widths are not explicitly defined, the Col component will display columns of equal width.

Example: Below is the code example of the Auto-layout columns:




import Container
    from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
 
function Example1() {
    return (
        <Container>
            <Row>
                <Col className='bg-success'>
                    1 of 2
                </Col>
                <Col className='bg-warning'>
                    2 of 2
                </Col>
            </Row>
            <Row>
                <Col className='bg-warning'>
                    1 of 3
                </Col>
                <Col className='bg-success'>
                    2 of 3
                </Col>
                <Col className='bg-warning'>
                    3 of 3
                </Col>
            </Row>
        </Container>
    );
}
 
export default Example1;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

Setting one column width:

Flexbox grid columns with auto-layout enable you to define the width of a single column, leading to automatic resizing of adjacent columns. This can be achieved using predefined grid classes, grid mixins, or inline widths. Importantly, the width of the center column influences the resizing of the surrounding columns.

Example: Below is the code example of the Setting one column width:




function Example2() {
    return (
        <Container>
            <Row>
                <Col className='bg-primary'>
                    1 of 3
                </Col>
                <Col xs={6}
                    className='bg-secondary'>
                    2 of 3 (wider)
                </Col>
                <Col>3 of 3</Col>
            </Row>
            <Row>
                <Col className=''>
                    1 of 3
                </Col>
                <Col xs={5}
                    className='bg-primary'>
                    2 of 3 (wider)
                </Col>
                <Col
                    className='bg-warning'>
                    3 of 3
                </Col>
            </Row>
        </Container>
    );
}
 
export default Example2;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

Variable width content:

You can set the column value to auto to size columns based on the natural width of their content.

Example: Below is the code example of the Variable width content:




import Container
    from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
 
function Example3() {
    return (
        <Container>
            <Row className="justify-content-md-center">
                <Col xs lg="2"
                    className='bg-primary'>
                    1 of 3
                </Col>
                <Col md="auto"
                    className='bg-warning'>
                    2 of 3 (variable content) Lorem ipsum,
                    dolor sit amet consectetur adipisicing
                    elit. Aperiam eaque ex iure odio illo
                    sed, praesentium delectus qui ducimus
                    unde doloremque temporibus deleniti
                    accusantium ratione esse dicta illum
                    recusandae nemo?
                </Col>
                <Col xs lg="2"
                    className='bg-secondary'>
                    3 of 3
                </Col>
            </Row>
        </Container>
    );
}
 
export default Example3;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

Responsive grids:

Using the Col component, you can designate column widths for six breakpoint sizes (xs, sm, md, lg, xl, and xxl). At each breakpoint, you have the option to define the number of columns to span. Alternatively, you can utilize the prop `<Col lg={true} />` for automatic layout widths.

Example: Below is the code example of the Responsive grids:




function Example5() {
    return (
        <Container>
            <Row>
                <Col sm={8}
                    className='bg-primary'>
                    sm=8
                </Col>
                <Col sm={4}
                    className='bg-secondary'>
                    sm=4
                </Col>
            </Row>
            <Row>
                <Col sm
                    className='bg-secondary'>
                    sm=true
                </Col>
                <Col sm
                    className='bg-warning'>
                    sm=true
                </Col>
                <Col sm
                    className='bg-primary'>
                    sm=true
                </Col>
                <Col sm
                    className='bg-secondary'>
                    sm=true
                </Col>
            </Row>
        </Container>
    );
}
 
export default Example5;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:

Setting column widths in Row:

The Row component allows you to define column widths for six breakpoint sizes (xs, sm, md, lg, xl, and xxl). At each breakpoint, you can specify the number of columns that will be placed adjacent to each other. Additionally, you have the option to set the columns to their natural widths by using the “auto” value. It’s important to note that Row column widths will take precedence over Col widths set at lower breakpoints when viewed on larger screens. For example, the size specified by `<Col xs={6} />` will be overridden by `<Row md={4} />` on medium and larger screens.

Example: Below is the code example of the setting column widths in row:




import Container
    from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
 
function Example4() {
    return (
        <Container>
            <Row xs={2} md={4} lg={6}>
                <Col className='bg-warning'>
                    1 of 2
                </Col>
                <Col>
                    2 of 2
                </Col>
            </Row>
            <Row xs={1} md={2}>
                <Col className='bg-primary'>
                    1 of 3
                </Col>
                <Col className='bg-warning'>
                    2 of 3
                </Col>
                <Col className='bg-secondary'>
                    3 of 3
                </Col>
            </Row>
            <Row xs="auto">
                <Col className='bg-warning'>
                    1 of 3
                </Col>
                <Col className='bg-secondary'>
                    2 of 3
                </Col>
                <Col className='bg-primary'>
                    3 of 3
                </Col>
            </Row>
        </Container>
    );
}
 
export default Example4;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output:


Article Tags :