javascript - 如何使标题按钮居中并将项目推到工具栏的最右侧?
问题描述
我的目标是将 [了解更多、关于、资源、联系方式] 按钮置于导航栏中间,然后将配置文件图标和切换按钮推到页面的最右侧!
理想情况下,与此类似,但在徽标/按钮/配置文件图标+开关之间有更多空白:这就是我缩小屏幕时工具栏的样子。
我正在努力使用带有 React 和 Material UI 的 CSS 样式来完成我在宽桌面屏幕上想要的东西。非常感谢对此的一些建议。
这是我的代码:
const useStyles = makeStyles((theme) => ({
appBar: {
borderBottom: `1px solid ${theme.palette.divider}`,
"@media (max-width: 950px)": {
paddingLeft: 0,
}
},
link: {
margin: theme.spacing(1, 1.5),
display: 'block',
},
toolBar: {
display: "flex",
justifyContent: "space-between",
},
toolbarTitle: {
flexGrow: 1,
fontFamily: 'Track',
textAlign: "left",
},
navLinks: {
fontWeight: 700,
size: "18px",
marginLeft: "38px",
padding: "0 1rem",
alignItems: "center",
justifyContent: "space-between",
textAlign: "center"
},
profileAvatar: {
},
}));
const displayDesktop = () => {
return <Toolbar>
<Link
component={NavLink}
to="/"
underline="none"
color="textPrimary"
>
{Logo}
</Link>
<div>
{getMenuButtons()}
</div>
{getUserAccount()}
<Switch
checked={isDarkMode}
onChange={toggleDarkMode}
/>
</Toolbar>;
};
const Logo = (
<Typography variant="h6" component="h1" className={classes.toolbarTitle} color="inherit">
Logo
</Typography>
);
const getMenuButtons = () => {
return headersData.map(({ label, href }) => {
return (
<Button
{...{
key: label,
color: "inherit",
to: href,
component: RouterLink,
className: classes.navLinks
}}
>
{label}
</Button>
);
});
};
const getUserAccount = () => {
return <div>
<IconButton
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleMenu}
color="inherit"
>
<AccountCircle />
</IconButton>
<Menu
id="menu-appbar"
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
open={open}
onClose={handleClose}
>
<MenuItem
component={NavLink} to="/profile"
onClick={handleClose}
>
Profile
</MenuItem>
<MenuItem
component={NavLink} to="/logout"
onClick={handleClose}
>
Logout
</MenuItem>
</Menu>
</div>
}
return (
<React.Fragment>
<CssBaseline />
<AppBar
position="static"
color="default"
elevation={0}
className={classes.appBar}
>
<Toolbar className={classes.toolbar}>
{mobileView ? displayMobile() : displayDesktop()}
</Toolbar>
</AppBar>
</React.Fragment>
);
}
我已经为我的徽标、导航链接、配置文件图标和开关功能粘贴了我的代码。
解决方案
将 Toolbar 中的组件包装成一个单独div
的组件,除了Logo
组件。然后在这个 div 中添加 flex 样式
<Toolbar style={{ width: "100%", justifyContent: "space-between" }}>
<Link to="/" underline="none" color="textPrimary">
{Logo}
</Link>
<div style={{ display: "flex", alignItems: "center" }}>
{getMenuButtons()}
</div>
<div>
{getUserAccount()}
<Switch checked={true} />
</div>
</Toolbar>
中间div
( 的包装getMenuButtons
)可以在不使用 flex 的情况下使用,因为它具有内联元素(按钮/锚点)。
推荐阅读
- postgresql - 无法将 Go 应用程序连接到 docker-compose 中的 postgres 容器
- css - 如何修复 Y 轴滚动条在具有固定页脚的引导可滚动内容布局中翻倍?
- c++ - 使用按位计算日志
- javascript - 当变量在 jest 测试用例中转换时,它会被提升吗?
- .net-core - F#:如何测试另一个项目的内部模块?
- python - python和ctypes,ctypes从嵌套结构返回错误的浮点值
- kubernetes - 如何为服务 GRPC 配置 haproxy-ingress
- c# - 如何在 C# PropertyGrid 中编辑 StringCollection?[“找不到'System.string'类型的构造函数”]
- python - 在 Mac 上安装 python 2.6
- d - 如何声明一组红黑树?