css - 将内容居中并使其位置可调
问题描述
我是react
和的新手css
。我的目标是将我的 3 个开关和它们的名称放在页面的中心。然后我希望 3 个开关按钮自动调整它们的位置,无论其左侧文本的长度如何。
这是我目前所拥有的。
这是switch button + text
. 我制作了一种样式以将文本与开关图标的中间对齐。我尝试了,verticalAlign: "middle"
但文本位于底部位置。
import React, { useState } from "react";
import Switch from "react-switch";
const SwitchMode = ({text}) => {
const [checked, setChecked] = useState(false)
const handleChange = StateCheck => {
setChecked(StateCheck)
}
return(
<div className="switch-position">
<label>
<span style={{display: "inline-flex", verticalAlign: '50%', marginRight: '4%'}}>{text}</span>
<Switch
onChange={handleChange}
checked={checked}
className="react-switch"
/>
</label>
</div>
)
}
export default SwitchMode
这是css
我暂时做的
body {
margin: 0;
}
.image-position {
display: flex;
justify-content: center;
width: 100%;
margin-top: 5%;
height: auto;
}
.service-position {
display: flex;
flex-direction: column;
margin-top: 5%;
}
.switch-position {
display: inline;
justify-content: center;
width: 100%;
margin-top: 1%;
}
在这个文件中,我创建了我的切换按钮,应用 css 并给它们命名。
import React from 'react';
import './index.css';
import ReactRoundedImage from "react-rounded-image";
import Img from './images/Diamond.png'
import SwitchMode from './Switch';
const Profile = () => {
return(
<div>
<div className='image-position'>
<ReactRoundedImage image={Img} roundedSize="0" imageWidth="150" imageHeight="150" />
</div>
<div className='service-position'>
<SwitchMode text="Discord"/>
<SwitchMode text="Google"/>
<SwitchMode text="FEJZIOFZEJ"/>
</div>
</div>
)
}
export default Profile
这是实际结果
解决方案
You can do something like this:
body {
margin: 0;
}
.image-position {
display: flex;
justify-content: center;
width: 100%;
margin-top: 5%;
height: auto;
}
.service-position {
display: flex;
justify-content: center;
flex-direction: row;
margin-top: 5%;
}
.switch-position {
text-align: center;
width: 100px;
}
and change your SwitchMode
component to this:
const SwitchMode = ({ text }) => {
const [checked, setChecked] = useState(false);
const handleChange = (StateCheck) => {
setChecked(StateCheck);
};
return (
<div className="switch-position">
<label>
<span>{text}</span>
<Switch
onChange={handleChange}
checked={checked}
className="react-switch"
/>
</label>
</div>
);
};
The main things I did was set justify-content: center
and flex-direction: row
on .service-position
. To make sure the .switch-position
items displayed in a row and were centered.
For centering the text next to the switch button I used text-align: center
and to make sure the text is displayed above the switch button I set a fixed with on .switch-position
.
推荐阅读
- html - 如何为绝对定位的 HTML 元素对齐基线
- flask - 如何记录 POST 端点的返回数据?
- qt - 未找到 QML 模块
- security - 如何在 java 中使用 bouncycastle 生成具有主题替代名称的 CSR
- apache-pulsar - getPartitionedTopicMetadata 时无法获得连接 - io.netty.channel.ConnectTimeoutException:连接超时
- python - 如何防止在 Docker 映像中运行命令
- python - 在数据框列输出中保留商标符号
- amazon-web-services - 更改触发 AWS CodeBuild 触发器的分支?
- swagger - API 文档 Swagger 与 JMS 序列化程序
- reactjs - React Navigation 5 使用类组件设置导航选项