用情感js样式化占位符文本

时间:2019-02-28 08:28:58

标签: javascript html css emotion

当前,placeholder="Your best email"的样式不是inputText

在检查元素上,user agent stylesheet用于input, textarea, select, button {}

下面是我的代码:

import React from "react";
import {css} from "@emotion/core";

const inputText = css`
  ::placeholder {
    float: left;
    height: 35px;
    width: 230px;
    color: #FFF;
    font-family: "Work Sans";
    font-size: 30px;
    font-weight: 700;
    line-height: 35px;
  }
`

const inputRound = css`
  box-sizing: border-box;
  height: 85px;
  width: 460px;
  border-radius: 100px;
  border: 5px solid #FFF;
  background-color: transparent;
  outline: none;
`

const Subscribe = () => (
   <div css={{
     display: 'flex'
   }}>
      <h2>Stay tuned</h2>
      <form name="subscribe" method="POST" data-netlify="true">
        <p css={inputText}>
          <input placeholder="Your best email" type="email" name="email" css={inputRound}/>
        </p>
        <p >
          <button type="submit"><h3>Subscribe</h3></button>
        </p>
      </form>
    </div>
)

export default Subscribe

是什么导致无法应用inputText样式?

这是开发人员工具的屏幕截图(建议将PS inputText更改为& > ::placeholder

Chrome Dev Tools

2 个答案:

答案 0 :(得分:2)

您必须将inputText样式直接应用于输入:

// Change this
- <p css={inputText}>
-   <input css={inputRound}/>
- </p>

//to this
+ <p>
+   <input css={[inputText, inputRound]}/>
+ </p>

或者,也可以定位来自inputText的子输入:

const inputText = css`
  & > input::placeholder {
    ...
  }
`

答案 1 :(得分:1)

如果要为输入的占位符设置样式,则可以这样做(此代码仅作为示例):

input: css`
      width: 100px
      border: 1px solid rgba(0, 0, 0, 0.1);
      border-radius: 3px;

      &::placeholder {
        color: red;
      }
    `,