简体   繁体   English

在样式组件中使用 css 选择器

[英]using css selectors in styled components

I recently moved to styled components as my styling method.我最近将styled components作为我的样式方法。 I'm facing an issue on how to use selectors in styled components.我面临如何在样式组件中使用selectors的问题。 For example i have a css like this例如我有一个这样的 css

.input-w-l-container{
    // padding:10px;
    border:1px solid;
    height:42px;
    border: 1px solid rgba(0, 0, 0, 0.32);
    box-sizing: border-box;
    border-radius: 4px;
    overflow: hidden;
  }
  .input-w-l-container>label{
    position:absolute;
    top:-11px;
    // left:25px;
    font-size: 12px;
    padding-left: 5px;
    padding-right: 10px;
    letter-spacing: 0.4px;
    color: rgba(0, 0, 0, 0.3);

  }

  .input-w-l-container>input{
    border:none;
    height:32px;
    margin-top: -5px;
    outline: none;
    padding-left: 4px;
  }

How to use this css using style components.如何使用样式组件使用此 css。 My current approach is something like the below code.我目前的方法类似于下面的代码。 But obviously it does not work但显然它不起作用

const InputContainer = styled.div`
    border:1px solid;
    height:42px;
    border: 1px solid rgba(0, 0, 0, 0.32);
    box-sizing: border-box;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    flex-flow: column-reverse;
    font-size: 12px !important;

    & > label {
        position:absolute;
        top:-11px;
        font-size: 12px;
        padding-left: 5px;
        padding-right: 10px;
        letter-spacing: 0.4px;
        color: rgba(0, 0, 0, 0.3);
    }

    & > input {
        border:none;
        height:32px;
        margin-top: -5px;
        outline: none;
        padding-left: 4px;
    }

`;

the styled components add inline styles to the element.样式化组件向元素添加内联样式。 so you can add there only the styles that you can add on this component inline.所以你只能在那里添加你可以在这个组件上内联添加的样式。

what you can do is create a few styles, like this:您可以做的是创建一些样式,如下所示:

const InputContainer = styled.div`
border:1px solid;
height:42px;
border: 1px solid rgba(0, 0, 0, 0.32);
box-sizing: border-box;
border-radius: 4px;
overflow: hidden;
display: flex;
flex-flow: column-reverse;
font-size: 12px !important;
`
const Label = styled.label`
    position:absolute;
    top:-11px;
    font-size: 12px;
    padding-left: 5px;
    padding-right: 10px;
    letter-spacing: 0.4px;
    color: rgba(0, 0, 0, 0.3);
`;
const Input = styled.input`
    border:none;
    height:32px;
    margin-top: -5px;
    outline: none;
    padding-left: 4px;
`;

then replace the label and input element with Label and Input accordingly.然后更换labelinput与元件LabelInput相应。 for example instead this:例如,而不是这样:

<InputContainer>
   <label>name</label><input type="text" placeholder="Jhon Doe"/>
</InputContainer>

type this>输入这个>

<InputContainer>
   <Label>name</Label><Input type="text" placeholder="Jhon Doe"/>
</InputContainer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM