简体   繁体   English

使用 html 和 css 将图标添加到输入

[英]add icon to input with html and css

i need to create a input with html and css.我需要使用 html 和 css 创建一个输入。

it must have icon, placeholder, icon in that.它必须有图标,占位符,图标。

like this:像这样:

在此处输入图像描述

i try to create this by this html code:我尝试通过这个 html 代码创建这个:

 <div class="username">
     <i class="fa fa-envelope icon"></i>
      <input type="text" placeholder="Username">
</div>
<div class="password">
     <i class="fa fa-envelope icon"></i>
      <input type="password" placeholder="Password">
</div>

and this css code:而这个 css 代码:

   .username input[type="text"]{
    padding: 5px;
    position: relative;
    border-radius: 2px;
    border:1px solid #ECF0EF;
    height: 35px;
    -webkit-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    -moz-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);

}
.username i{
    position: absolute;
    z-index: 9999;
    height: 10px;

    line-height: 2;
}
.password input[type="password"]{
    padding: 5px;
    position: relative;
    border-radius: 2px;
    border:1px solid #ECF0EF;
    height: 30px;
    -webkit-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    -moz-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);

}
.password input::after{
    content: '<i class="fa fa-envelope icon"> ';
}
.password  i{
    position: absolute;
    z-index: 9999;
    height: 10px;

    line-height: 2;
}

but it not worked.但它没有奏效。

this is Demo这是演示

how can i do create like that??我怎么能这样创作??

I made a couple of changes to the CSS and HTML (added another element for the other image).我对 CSS 和 HTML 进行了一些更改(为另一个图像添加了另一个元素)。

app.component.css app.component.css

.loginInput label{
    color: #A7AAB3;
}

/* EDITS START HERE */

.username,
.password {
  display: flex;
  align-items: center;
  position: relative;
  border-radius: 2px;
    border:1px solid #ECF0EF;
    height: 35px;
    -webkit-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    -moz-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
    box-shadow: 0px 0px 28px -15px rgba(0,0,0,1);
}

.username input[type="text"],
.password input[type="password"]{
    padding: 5px 27.5px;
    position: relative;
    border: none;
    background-color: unset;
}

.username i:first-child,
.password i:first-child {
  left: 7.5px;
    position: absolute;
    z-index: 9999;
}
.username i:last-child,
.password i:last-child {
    right: 7.5px;
    position: absolute;
    z-index: 9999;
}
.password input::after{
    content: '<i class="fa fa-envelope icon"> ';
}

/* EDITS END HERE */

.checkbox{
    float: left;
}

app.component.html app.component.html

<div class="loginInput">
  <label>Or sign in with credentional </label>
    <div class="inputs">
      <div class="username">
        <i class="fa fa-envelope icon"></i>
        <input id="text" type="text" placeholder="Username">
        <!-- Added this line. -->
        <i class="fa fa-envelope icon"></i> 
      </div>
      <div class="password">
        <i class="fa fa-envelope icon"></i>
        <input type="password" placeholder="Password">
        <!-- Added this line. -->
        <i class="fa fa-envelope icon"></i>
      </div>
    <div class="rememberMe">
      <input class="styled-checkbox" id="styled-checkbox-1" type="checkbox" value="value1">
      <label for="styled-checkbox-1">Checkbox</label>
    </div>
  </div>
</div>

Here is a fork of your code with the changes made: https://stackblitz.com/edit/angular-nq6j8u .这是您的代码的一个分支,其中进行了更改: https://stackblitz.com/edit/angular-nq6j8u

Hope it helps!希望能帮助到你!

Make sure you have linked bootstrap and fontawesome to you head tag, then you proceed by coding these:确保您已将 bootstrap 和 fontawesome 链接到您的 head 标签,然后您继续编写这些代码:

<div class="input-group mb-3">
    <div class="input-group-append">
        <span class="input-group-text"><i class="fas fa-user"></i></span>
    </div>
    <input type="text" name="" class="form-control input_user" value="" placeholder="username">
</div>
<div class="input-group mb-2">
    <div class="input-group-append">
        <span class="input-group-text"><i class="fas fa-key"></i></span>
    </div>
    <input type="password" name="" class="form-control input_pass" value="" placeholder="password">
</div>

then you continue with your styling然后你继续你的造型

The CSS content attribute does not support HTML, it only supports plain text, this is why the icon in ::after is not showing. CSS content属性不支持 HTML,它只支持纯文本,这就是::after中的图标不显示的原因。

As you need to insert HTML content in this case, a possible solution could be to use Javascript (and jQuery in this example) to add the <i> element before and after the text inputs.由于您需要在这种情况下插入 HTML 内容,因此可能的解决方案是使用 Javascript(和本示例中的 jQuery)在文本输入之前和之后添加<i>元素。

As for the CSS, for positioning the icons next to the input, you can use display: inline-block and possibly some margin, instead of position: absolute .至于 CSS,为了将图标定位在输入旁边,您可以使用display: inline-block和可能的一些边距,而不是position: absolute

 $(".username input, .password input").before("<i class='fa fa-envelope icon'></i>"); $(".username input, .password input").after("<i class='fa fa-envelope icon'></i>");
 .username input[type="text"], .password input[type="password"]{ padding: 5px; position: relative; border-radius: 2px; border:1px solid #ECF0EF; height: 35px; -webkit-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1); -moz-box-shadow: 0px 0px 28px -15px rgba(0,0,0,1); box-shadow: 0px 0px 28px -15px rgba(0,0,0,1); }.username i, .password i{ display:inline-block; z-index: 9999; height: 10px; line-height: 2; margin: 0 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' /> <div class="username"> <input type="text" placeholder="Username" /> </div> <div class="password"> <input type="password" placeholder="Password" /> </div>

Edit: And if you can only use HTML and CSS, there is not much to do, other than hard-coding the icons before and after each input:编辑:如果你只能使用 HTML 和 CSS,除了在每次输入之前和之后对图标进行硬编码之外,没有什么可做的:

<i class="fa fa-envelope icon"></i>
<input type="text" placeholder="Username" />
<i class="fa fa-envelope icon"></i>

Am confused as to what exactly didn't work对到底什么不起作用感到困惑

But you can use this as a guide https://www.w3schools.com/howto/howto_css_login_form.asp但是您可以将其用作指南https://www.w3schools.com/howto/howto_css_login_form.asp

To add an icon to an input, the tag is usually used.要将图标添加到输入,通常使用标签。 Eg例如

<style> 


    .input-icons { 

        width: 100%; 

        margin-bottom: 10px; 

    } 



    .icon { 

        padding: 10px; 

        min-width: 40px; 

    } 



    .input-field { 

        width: 100%; 

        padding: 10px; 

        text-align: center; 

    } 

</style> 

<h3> 

  Icons inside the input element 

<div style="max-width:400px;margin:auto"> 

    <div class="input-icons">  

        <i class="fa fa-envelope icon"></i> 

        <input class="input-field" type="text"> 

        <i class="fa fa-padlock icon"></i> 

        <input class="input-field" type="password"> 

    </div> 

Please be sure that fontawesome is tagged in your code, with that, the code will work fine.请确保在您的代码中标记了 fontawesome,这样代码就可以正常工作。

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

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