无法使图标停留在输入中

时间:2019-04-14 01:57:13

标签: html css

我正在学习React,但是我有一个简单的html / css问题。我以前有过这种事情,但是由于某种原因我被困住了。

我正在尝试制作一个简单的登录表单,但是图标在输入下方而不是内部。如果我尝试移动带有边距的图标,该图标将隐藏在输入后。

我有一个呈现的React组件:

<Input icon="register-user" onChange={this.handleChange} type='text' placeholder='Username'   />

输入看起来像这样:

<div className="control has-icon input-text">
  <input {...this.props} ref='input' className={inputClassNames}/>
  <i className={icon}/>
</div>

我的CSS看起来像:

.register {
  background-size: cover;
  .container {
    width: 736px;
    height: 370px;
    clear: both;
    display: block;
    .form {
      background-color: white;
      padding: 48px 48px 20px 48px;
      height: 400px;
      width: 392px;
      border-radius: 8px;
      box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.2);

      .control {
        margin-bottom: 16px;
      }

      .control:last-child {
        margin-top: 21px;
      }

      .has-icon {
        background-color: pink;
      }
    } //end .form

    input {
      outline: none;
      box-shadow: none;
      text-align: left;
      height: 32px;
      width: 280px;
      border: 1px solid #006bac;
      border-radius: 4px;
      background-color: #f2f7fa;
    }
  } // end .container

  input {
    outline: none;
    box-shadow: none;
    border-radius: 0px;
    height: 40px;
    color: $admin-register-input-text;
    font-size: 16px;
    background: #2c3c44;
    border: none;
  }

  .form,
  .image {
    float: right;
    width: 50%;
    height: 100%;
  }
  .title {
    color: blue;
  }
} //end .register

.register-user {
  background: url($login_user_icon) no-repeat 50%;
  height: 16px;
  width: 13px;
  color: #006bac;
  font-size: 14px;
  line-height: 14px;
  text-align: center;
  float: right;
}

1 个答案:

答案 0 :(得分:0)

这是CSS而不是React相关的问题,当涉及重叠元素时,请考虑使用位置而不是边距。

演示

.container {
  position: relative;
  /* let the input assign the width of the parent */
  display: inline-block;
}

.icon {
  position: absolute;
  top: 0;
  right: 0;
}
<div class="container">
  <input type="text" class="input" />
  <i class="icon">☺</i>
</div>