【发布时间】:2022-01-01 05:41:21
【问题描述】:
我一直在努力使用 StyledComponents 为我的 React 应用程序设计样式,但是我已经成功地在这个 codepen 中重新创建并修复了这个问题:
https://codepen.io/simoncunningham/pen/mdwMgyL
(基本上使用flex: 1)来确保复选框扩展到父级的整个宽度)但是我无法用我的StyledComponents反映这种变化,任何人都可以看到我可能遗漏了什么吗?
const StyledWrapper = styled.div`
position: relative;
display: inline-flex;
min-height: 1.5rem;
margin-right: 1rem;
padding: 0.5rem
width: 200px;
`;
const StyledHiddenInput = styled.input`
position: absolute;
z-index: -1;
visibility: hidden;
&[type="checkbox"]:checked ~ label::after {
background-image: url("${checkboxImage}");
width: 20px;
height: 20px;
}
`;
const StyledLabel = styled.label`
position: relative;
margin-bottom: 0;
display: flex;
margin-left: 0;
margin-right: 24px;
flex-direction: row-reverse;
justify-content: space-between;
flex: 1;
&::before {
display: block;
top: 4px;
width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius: 4px;
}
&::after {
position: absolute;
display: block;
top: 4px;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
`;
const StyledLabelWrapper = styled.div`
margin-left: 24px;
margin-right: 0;
`;
<StyledWrapper>
<StyledHiddenInput />
<StyledLabel>
<StyledLabelWrapper>{label}</StyledLabelWrapper>
</StyledLabel>
</StyledWrapper>
Codepen 输出的屏幕截图(按预期工作,复选框显示在尽可能右侧)::
样式化组件的输出截图:
【问题讨论】:
标签: html css flexbox styled-components