![React Material:UI Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/718/36698718/b_36698718.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it...
Let's say that you have three basic tabs using the following code:
import React, { useState } from 'react';
import { withStyles } from '@material-ui/core/styles';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
const styles = theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper
}
});
function TabAlignment({ classes }) {
const [value, setValue] = useState(0);
const onChange = (e, value) => {
setValue(value);
};
return (
<div className={classes.root}>
<Tabs value={value} onChange={onChange}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</div>
);
}
export default withStyles(styles)(TabAlignment);
Here's what you should see when the screen first loads:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/39fbb81c-8587-4e7d-baef-56929e5508ce.png?sign=1739677561-8OYV2YKXMcnhi5nIXhksmiwJeL2YPvQ7-0-c76b0a36c91d6e0df7fa9146f0e14a66)
By default, tabs are aligned to the left. You can center your tabs by setting the centered property, as follows:
<Tabs value={value} onChange={onChange} centered>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
Here's what centered tabs look like:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/d8af98bd-1853-465b-9ba1-86100051bcbf.png?sign=1739677561-izt1u4aVi19quLVU5RvON6ZbwYraE1UP-0-2717ab80710fb65b0553c5a00854a257)
When your tabs are centered, all of the empty space goes to the left and right of the tabs. The alternative is setting the variant property to fullWidth:
<Tabs value={value} onChange={onChange} variant="fullWidth">
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
Here's what full width tabs look like:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/e9bf778b-ce3f-4224-b2a3-36a7f5cb8fa5.png?sign=1739677561-DhlfIIXC2Q29TWCaiBIdSQUcvPchnYH3-0-2f537bf1fc54a8465879a2797eff9e02)
The tabs are centered, but they're spaced evenly to cover the width of the screen.