【问题标题】:translate a classic css stylesheet to react styled-component use + selector翻译经典的 css 样式表以响应样式组件使用 + 选择器
【发布时间】:2021-12-25 16:52:26
【问题描述】:

您好,我正在学习 React,为了练习,我正在从简单的 html 页面传递到 react 组件,我正在使用 react 样式组件来应用样式。

在我的经典 css 样式表中,我对输入元素有以下规则:

.input100 {
  font-family: Poppins-Medium;
  font-size: 16px;
  color: #333333;
  line-height: 1.2;
  display: block;
  width: 100%;
  height: 55px;
  background: transparent;
  padding: 0 7px 0 43px;
}

.focus-input100 {
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
}

.focus-input100::after {
  content: attr(data-symbol);
  font-family: Material-Design-Iconic-Font;
  color: #adadad;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  height: calc(100% - 20px);
  bottom: 0;
  left: 0;
  padding-left: 13px;
  padding-top: 3px;
}

我在我的反应组件中翻译了这些规则,例如:

const Input = styled.input`
  font-family: Poppins-Medium;
  font-size: 16px;
  color: #333333;
  line-height: 1.2;
  display: block;
  width: 100%;
  height: 55px;
  background: transparent;
  padding: 0 7px 0 43px;
  border: none;
  outline: none;
  margin: 0;
`;

const Icon = styled.span`
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
  &:after {
    content: attr(data-symbol);
    font-family: Material-Design-Iconic-Font;
    color: #adadad;
    font-size: 22px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    height: calc(100% - 20px);
    bottom: 0;
    left: 0;
    padding-left: 13px;
    padding-top: 3px;
  }
  &:before {
    content: "";
    display: block;
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 0;
    height: 2px;
    background: #7f7f7f;
    -webkit-transition: all 0.4s;
    -o-transition: all 0.4s;
    -moz-transition: all 0.4s;
    transition: all 0.4s;
  }
`;

按我的预期工作。我的问题是我不知道如何翻译这个特定规则:

.input100:focus + .focus-input100::after {
  color: #a64bf4;
}

我在文档中找到了这个,但老实说,我不明白

& + & {
    background: lime; // <Thing> next to <Thing>
  }

我尝试了类似的方法:

&:focus + &Icon{

}

但这没有用

【问题讨论】:

    标签: css reactjs css-selectors styled-components


    【解决方案1】:

    您可以在Input 中使用样式化的Icon,使用${Icon}

    const Icon = styled.span`
      position: absolute;
      // ...
    `;
    
    const Input = styled.input`
      font-family: Poppins-Medium;
      // ...
    
      &:focus + ${Icon}::after {
       color: #a64bf4;
      }
    `;
    

    【讨论】:

    • 感谢@Ramesh Reddy 成功了!
    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 2020-05-25
    • 2010-12-31
    • 2016-11-16
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    相关资源
    最近更新 更多