[英]MaterialUI: how to align button in x-axis end of the axis?
I have a navbar component that looks like我有一个导航栏组件,看起来像
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton edge="start" color="inherit" aria-label="menu" onClick={toggleDrawer(true)}>
<MenuIcon />
</IconButton>
<Link href="#" color="primary" underline="none" variant="h5" className={classes.brand}>
{brand}
</Link>
<Button variant="contained" color="secondary" className={classes.primaryAction}>{content['primary-action']}</Button>
</Toolbar>
However the <Button variant="contained> is placed before the Link, how can I place the button all the way to the end of the x axis?然而 <Button variant="contained> 放置在链接之前,我怎样才能将按钮一直放置到 x 轴的末端?
This is the styles这是 styles
const useStyles = makeStyles((theme: Theme) => ({
toolbar: {
minHeight: 200
},
brand: {
lineHeight: 1,
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)'
},
primaryAction: {
justifySelf: 'end'
},
iconWrapper: {
minWidth: 40,
},
}));
Try to apply flexGrow: 1
on the link and align it right:尝试在链接上应用
flexGrow: 1
并将其右对齐:
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton edge="start" color="inherit" aria-label="menu" onClick={toggleDrawer(true)}>
<MenuIcon />
</IconButton>
<Link href="#" color="primary" underline="none" variant="h5" sx={{ flexGrow: 1, textAlign: 'right' }}>
{brand}
</Link>
<Button variant="contained" color="secondary" className={classes.primaryAction}>{content['primary-action']}</Button>
</Toolbar>
Try using CSS flex to place the buttons and link inside the toolbar.尝试使用CSS flex将按钮和链接放置在工具栏内。
Here's a working CodeSandbox. 这是一个工作的 CodeSandbox。 (Note that if you want the Link to come after the Button, just move the component below the Button in the code as per the comment I left).
(请注意,如果您希望链接出现在按钮之后,只需按照我留下的评论将代码中的组件移动到按钮下方)。
Here's the JSX:这是 JSX:
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton
edge="start"
color="inherit"
aria-label="menu"
onClick={toggleDrawer(true)}
>
<MenuIcon />
</IconButton>
<div className={classes.verticalCenter}>
{/* Just move this link to come after the Button below to change the order */}
<Link
href="#"
color="primary"
underline="none"
variant="h5"
className={classes.brand}
>
{brand}
</Link>
<Button
variant="contained"
color="secondary"
className={classes.primaryAction}
>
{content["primary-action"]}
</Button>
</div>
</Toolbar>
</AppBar>
And the CSS:和 CSS:
const useStyles = makeStyles((theme) => ({
toolbar: {
minHeight: 200,
display: "flex",
justifyContent: "space-between"
},
brand: {
lineHeight: 1,
padding: 16
},
primaryAction: {
justifySelf: "flex-end"
},
iconWrapper: {
minWidth: 40
},
verticalCenter: {
display: "flex",
alignItems: "center"
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.