【问题标题】:React Material UI Box text goes out of the divReact Material UI Box 文本超出了 div
【发布时间】:2020-08-14 15:18:30
【问题描述】:

我正在尝试构建一个简单的反应页面,但我一开始就卡住了:我的文本开箱即用,我不明白为什么会这样。

有人可以看看我的风格吗? Picture with the inherited styles

如果我删除whiteSpace="nowrap",文本会在下面。 我的目标是在图片底部添加和文本。 有人知道问题可能出在哪里吗?提前非常感谢!

HeaderMui.js

 const url = process.env.PUBLIC_URL + "/img/castle.jpg";
const useStyles = makeStyles((主题 => ({

标题:{ 高度:“57vh”, 背景:url(${url}), 背景尺寸:“封面”, alignItems:“中心”, justifyContent: "中心", alignItems:“中心”, backgroundPosition: "中心中心", 背景附件:“固定”, 弹性增长:1, },

标题文本:{ 文本对齐:“左”, 字体大小:20, 线高:20, boxSizing: "边框框", 显示:'内联块', alignItems:“中心”, 文本对齐:“中心” }, 文本包装器:{

display: "block",
paddingRight: 15,
paddingLeft: 15,
alignItems: "center"

},

}

)));

function HeaderMui() {
  const classes = useStyles();

  return (
    <Box className={classes.header} flexWrap="nowrap">
    <Box className={classes.textWrapper} width="100%" flexWrap="nowrap">
    <h2 >Hogwarts Online</h2>
      < Box component="span" className={classes.headerText} display="inline-block" flexWrap="wrap" whiteSpace="nowrap"> Hogwarts Online is the first platform out there for the wizards far away from Hogwarts. Experience the power of British magical education while still working muggle job. All you need is a magic wand and to be a proved wizard!</Box>
      </Box>
    </Box>
  );
}
export default HeaderMui;

App.js

function App(){ const greaterThanSm = useMediaQuery(theme => theme.breakpoints.up("sm")); const greaterThanMd = useMediaQuery(theme => theme.breakpoints.up("md")); return ( <> <Hidden only="xs"> <AppbarMui /> </Hidden> <Grid container spacing={3}> <Grid item xs={12} md={7} lg={12} > <HeaderMui /> </Grid> </Grid> </> ); } export default App;

【问题讨论】:

  • 请在代码沙箱中分享您的代码并复制此问题
  • @rotimi-best 这里是link
  • 您的沙盒中有错误。您在组件文件夹中的文件没有.js
  • 谢谢指出,现已更正

标签: css reactjs material-ui


【解决方案1】:

赫敏。

我认为您遇到的问题肯定是因为 headerText 类中的boxSizing: "border-box",

请查看此文档,https://www.w3schools.com/css/css3_box-sizing.asp

box-sizing 允许我们拥有特定元素的总宽度和高度中的内容。这就是为什么如果内容的 width + padding + margin 大于父元素的宽度,则内容会超出父元素的区域。

希望这会有所帮助。 :)

【讨论】:

  • 凯文,谢谢你的回答。我删除了 boxSizing,遗憾的是,什么都没有改变。我猜问题出在非常大的 div 中(在附图中可以看到)
  • 嗯,需要在span的父元素中使用width: fit-content调整width的样式。能否像在元素检查器中一样附上另一张显示 span 元素的父元素的图片?
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2018-01-31
  • 2016-12-21
  • 2021-05-26
  • 2021-01-19
  • 2018-11-30
  • 1970-01-01
相关资源
最近更新 更多