【发布时间】:2021-07-24 14:59:39
【问题描述】:
我在我的 React 应用程序的 div 中添加了 justify-content: space-evenly,但我没有看到任何变化。我已经申请了display: flex、align-items: flex-end,它们都按预期工作。我还尝试flex-direction 来检查它是否有效,并且确实有效。我不明白为什么justify-content 不起作用?我尝试了justify-content 的其他值,例如space-around、space-between 和center,但我得到了相同的结果,justify-content 似乎根本不起作用。下面是我的组件代码和组件继承的父样式...
---- 组件----
// Libraries
import styled from "styled-components";
import { useSelector, useDispatch } from "react-redux";
// Components and icons
import { StyledTop } from "../Components/styles";
import {
RecentlyPlayedIcon,
SettingsIcon,
QuitIcon,
} from "../Components/icons";
const RightBarButtons = () => {
// Grab needed state and set vars
const theme = useSelector((state) => state.theme);
const rightBarState = useSelector((state) => state.rightBarSelection);
const dispatch = useDispatch();
// Handlers
const buttonHandler = (buttonName) => {
if (buttonName !== rightBarState) {
dispatch({
type: `SET_RIGHT_BAR_${buttonName.toUpperCase()}`,
});
}
};
return (
<StyledTopButtons>
<button>
<QuitIcon theme={theme} />
</button>
<button
onClick={() => {
buttonHandler("settings");
}}
style={{
borderLeft: `4px solid ${
theme === "light" ? "rgba(0,0,0,.05)" : "rgba(255, 255, 255, 0.125)"
} `,
borderRight: `4px solid ${
theme === "light" ? "rgba(0,0,0,.05)" : "rgba(255, 255, 255, 0.125)"
} `,
}}
>
<SettingsIcon rightBarState={rightBarState} theme={theme} />
</button>
<button
onClick={() => {
buttonHandler("recently_played");
}}
>
<RecentlyPlayedIcon rightBarState={rightBarState} theme={theme} />
</button>
</StyledTopButtons>
);
};
const StyledTopButtons = styled(StyledTop)`
margin: 3.9rem 4rem;
width: calc(100% - 8rem);
display: flex;
align-items: flex-end;
justify-content: space-evenly;
button {
transform: translateX(-2.6rem);
height: 2.2rem;
cursor: pointer;
svg {
height: 100%;
pointer-events: none;
}
}
`;
export default RightBarButtons;
---- StyledTop 组件,我继承自 ----
export const StyledTop = styled.div`
height: 2.8rem;
width: 100%;
h1 {
margin: 4rem;
font-size: 2.8rem;
font-weight: 600;
color: ${(props) => (props.theme === "light" ? "black" : "white")};
cursor: pointer;
}
span {
color: #3ca8c9;
}
`;
如果有人可以帮助我弄清楚为什么它的行为不符合预期,我将不胜感激。 谢谢。
【问题讨论】:
-
好吧,我很不明白为什么会这样。我试图从头开始复制你的组件,除了逻辑,它似乎工作正常......codesandbox.io/s/flexbox-test-ilfr7
-
@RichardZhan 感谢您的浏览,包括沙盒链接。我明天早上再试一次,然后报告!
-
你好@user15459167 这能回答你的问题吗? stackoverflow.com/questions/24052569/…
-
嘿,@PriyankKachhela,这确实帮助我指明了正确的方向,谢谢!
-
@RichardZhan 我只是把答案放在下面。再次感谢您的帮助。
标签: css reactjs sass styled-components