【问题标题】:Using pseudo code to style radio button with styled-components使用伪代码为带有样式组件的单选按钮设置样式
【发布时间】:2019-06-18 18:31:22
【问题描述】:

我开始了解样式化组件的工作原理,但我遇到的问题之一是伪代码。对我来说,目前,似乎没有按预期工作。我希望单选按钮在按钮内显示检查。按钮样式正确,但未显示复选标记。

样式化组件

export const FormRadio = styled.input.attrs(props => ({
    type: 'radio',
    name: 'action',
    id: 'action-radio-1',
    value: props.value,
}))`
    appearance: none;
    display: inline-block;
    position: relative;
    background-color: #f1f1f1;
    color: #254294;
    height: 1.5em;
    width: 1.5em;
    border: 0;
    border-radius: 1em;
    cursor: pointer;
    margin-right: 0.274vw;
    outline: none;

    &:checked::before {
        position: absolute;
        font: 17px/1 'Open Sans', sans-serif;
        left: 0.45em;
        top: 0.12em;
        content: '\02143';
        transform: rotate(40deg);
    }
`;

【问题讨论】:

    标签: css styled-components


    【解决方案1】:

    您的内容似乎破坏了它。并且由于它位于absolute,因此您需要在样式中添加widthheight

    所以,这样的事情应该可以工作(请注意,我简化了组件以专注于checked:before

    const Test = styled.input`
    &[type="radio"]{
    
      position: absolute
      &:checked:before{
          content:"";
          font: 17px/1 'Open Sans', sans-serif;
          position:absolute;
          width: 100%;
          height: 100%;
          background:orange;
          border-radius: 100%;
          left: 0;
          top: 0;
        }
    
    }
    
    `;
    

    【讨论】:

    • 谢谢!实际上,我终于通过进行一些分类发现了它,并注意到当我删除 content 属性时,它开始显示。奇怪的是内容值不起作用,因为它在标准 CSS 中工作,但无论哪种方式,现在都很好。
    • 是的,我认为这与输入有关,不一定与标准 css 与样式组件有关,但我也觉得这很奇怪!
    猜你喜欢
    • 2019-10-18
    • 2013-06-08
    • 2017-10-25
    • 2021-12-16
    • 2012-11-28
    • 2016-07-11
    • 2017-11-14
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多