You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-Bootstrap Col组件无法并排显示,全部换行的问题

问题解决方法
  • 核心错误:Col组件导入错误
    你代码里把Col错误导入成了Row:

    import Col from "react-bootstrap/Row"; // 错误语句
    

    正确导入应该是:

    import Col from "react-bootstrap/Col"; // 修正后的导入
    
  • 修正后的完整代码

    import { styled } from "@mui/material/styles";
    import Paper from "@mui/material/Paper";
    import Container from "react-bootstrap/Container";
    import Row from "react-bootstrap/Row";
    import Col from "react-bootstrap/Col";
    export default function RecipeLanding() {
      const Item = styled(Paper)(({ theme }) => ({
        backgroundColor: theme.palette.mode === "dark" ? "#1A2027" : "#fff",
        ...theme.typography.body2,
        padding: theme.spacing(1),
        textAlign: "center",
        color: theme.palette.text.secondary,
      }));
      return (
        <Container>
          <Row>
            <Col>
              <Item>1 of 2</Item>
            </Col>
            <Col>2 of 2</Col>
          </Row>
          <Row>
            <Col>1 of 3</Col>
            <Col>2 of 3</Col>
            <Col>3 of 3</Col>
          </Row>
        </Container>
      );
    }
    
  • 额外排查点

    1. 确认index.html中引入的Bootstrap CSS CDN版本正确且加载正常,比如适配React-Bootstrap v2.x的Bootstrap 5.x版本:
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
      
    2. 检查React-Bootstrap与Bootstrap CSS的版本是否匹配(React-Bootstrap v2对应Bootstrap 5,v1对应Bootstrap 4)
    3. 通过浏览器开发者工具查看Row和Col元素的CSS属性,确认flex布局相关样式未被Material UI或自定义样式覆盖

内容的提问来源于stack exchange,提问作者Sandeep Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:05:19