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> ); }额外排查点
- 确认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"> - 检查React-Bootstrap与Bootstrap CSS的版本是否匹配(React-Bootstrap v2对应Bootstrap 5,v1对应Bootstrap 4)
- 通过浏览器开发者工具查看Row和Col元素的CSS属性,确认flex布局相关样式未被Material UI或自定义样式覆盖
- 确认index.html中引入的Bootstrap CSS CDN版本正确且加载正常,比如适配React-Bootstrap v2.x的Bootstrap 5.x版本:
内容的提问来源于stack exchange,提问作者Sandeep Thomas
相关产品推荐
相关产品推荐

