首页 文章

文本输入元素内的字体真棒图标

提问于
浏览
102

我试图在用户名输入字段中插入用户图标 .

我已经尝试了similar question中的一个解决方案,知道 background-image 属性不起作用,因为Font Awesome是一个字体 .

以下是我的方法,我无法显示图标 .

.wrapper input[type="text"] {
    position: relative;
}

.wrapper input[type="text"]:before {
    font-family: 'FontAwesome';
    position: absolute;
    top: 0px;
    left: -5px;
    content: "\f007";
}

我在默认字体awesome css中声明了字体,所以我不确定上面添加font-family是否是正确的方法 .

@font-face {
     font-family: 'FontAwesome';
     src: url('../Font/fontawesome-webfont.eot?v=3.2.1');
     src: url('../Font/fontawesome-webfont.eot?#iefix&v=3.2.1') format('embedded-opentype'), url('../Font/fontawesome-webfont.woff?v=3.2.1') format('woff'), url('../Font/fontawesome-webfont.ttf?v=3.2.1') format('truetype'), url('../Font/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1') format('svg');
 }

14 回答

  • -4

    要使用unicode或fontawesome来处理此问题,您应该添加 spanclass ,如下所示:

    在HTML中:

    <span class="button1 search"></span>
    <input name="username">
    

    在CSS中:

    .button1 {
        background-color: #B9D5AD;
        border-radius: 0.2em 0 0 0.2em;
        box-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), 2px 0 0 rgba(255, 255, 255, 0.5); 
        pointer-events: none;
        margin:1px 12px;    
        border-radius: 0.2em;    
        color: #333333;
        cursor: pointer;
        position: absolute;
        padding: 3px;
        text-decoration: none;           
    }
    
  • 19
    <!doctype html>
    <html>
      <head>
        ## Heading ##
        <meta charset="utf-8">
          <title>
            Untitled Document
          </title>
          </head>
          <style>
            li {
              display: block;
              width: auto;
            }
            ul li> ul li {
              float: left;
            }
            ul li> ul {
              display: none;
              position: absolute;
            }
            li:hover > ul {
              display: block;
              margin-left: 148px;
              display: inline;
              margin-top: -52px;
            }
            a {
              background: #f2f2ea;
              display: block;
              /*padding:10px 5px;
              */
              width: 186px;
              height: 50px;
              border: solid 2px #c2c2c2;
              border-bottom: none;
              text-decoration: none;
            }
            li:hover >a {
              background: #ffffff;
            }
            ul li>li:hover {
              margin: 12px auto 0px auto;
              padding-top: 10px;
              width: 0;
              height: 0;
              border-top: 8px solid #c2c2c2;
            }
            .bottom {
              border-bottom: solid 2px #c2c2c2;
            }
            .sub_m {
              border-bottom: solid 2px #c2c2c2;
            }
            .sub_m2 {
              border-left: none;
              border-right: none;
              border-bottom: solid 2px #c2c2c2;
            }
            li.selected {
              background: #6D0070;
            }
            #menu_content {
              /*float:left;
              */
    
            }
            .ca-main {
              padding-top: 18px;
              margin: 0;
              color: #34495e;
              font-size: 18px;
            }
            .ca-sub {
              padding-top: 18px;
              margin: 0px 20px;
              color: #34495e;
              font-size: 18px;
            }
            .submenu a {
              width: auto;
            }
            h2 {
              text-align: center;
            }
          </style>
          <body>
            <ul>
              <li>
                <a href="#">
                  <div id="menu_content">
                    <h2 class="ca-main">
                      Item 1
                    </h2>
                  </div>
                </a>
                <ul class="submenu" >
                  <li>
                    <a href="#" class="sub_m">
                      <div id="menu_content">
                        <h2 class="ca-sub">
                          Item 1_1
                        </h2>
                      </div>
                    </a>
                  </li>
                  <li>
    
                    <a href="#" class="sub_m2">
                      <div id="menu_content">
                        <h2 class="ca-sub">
                          Item 1_2
                        </h2>
                      </div>
                    </a>
                  </li>
                  <li >
    
                    <a href="#" class="sub_m">
                      <div id="menu_content">
                        <h2 class="ca-sub">
                          Item 1_3
                        </h2>
                      </div>
                    </a>
    
                  </li>
                </ul>
              </li>
              <li>
    
                <a href="#">
                  <div id="menu_content">
                    <h2 class="ca-main">
                      Item 2
                    </h2>
                  </div>
                </a>
              </li>
              <li>
    
                <a href="#">
                  <div id="menu_content">
                    <h2 class="ca-main">
                      Item 3
                    </h2>
                  </div>
                </a>
    
              </li>
              <li>
    
                <a href="#"  class="bottom">
                  <div id="menu_content">
                    <h2 class="ca-main">
                      Item 4
                    </h2>
                  </div>
                </a>
    
              </li>
            </ul>
          </body>
    </html>
    
  • 0

    Build 在allcaps建议的基础上 . 这是具有最少HTML的字体真棒背景方法:

    <div class="wrapper"><input></div>
    
    .wrapper {
        position: relative; 
    }
    
    input { padding-left: 20px; }
    
    .wrapper:before {
        font-family: 'FontAwesome';
        position: absolute;
        top: 2px;
        left: 3px;
        content: "\f007";
    }
    
  • 94

    我尝试了下面的东西,它确实很好用HTML

    input.hai {
        width: 450px;
        padding-left: 25px;
        margin: 15px;
        height: 25px;
        background-image: url('https://cdn4.iconfinder.com/data/icons/casual-events-and-opinions/256/User-512.png') ;
        background-size: 20px 20px;
        background-repeat: no-repeat;
        background-position: left;
        background-color: grey;
    }
    
    <div >
    
        <input class="hai" placeholder="Search term">
    
    </div>
    
  • 5

    Output:

    enter image description here

    HTML:

    <input name="txtName" id="txtName">
    
    <span class="fa fa-info-circle errspan"></span>
    

    CSS:

    <style type="text/css">
        .errspan {
            float: right;
            margin-right: 6px;
            margin-top: -20px;
            position: relative;
            z-index: 2;
            color: red;
        }
    </style>
    
  • 1

    我确实这样做了

    form i {
        left: -25px;
        top: 23px;
        border: none;
        position: relative;
        padding: 0;
        margin: 0;
        float: left;
        color: #29a038;
      }
    
    <form>
    
      <i class="fa fa-link"></i>
    
      <div class="form-group string optional profile_website">
        <input class="string optional form-control" placeholder="http://your-website.com" type="text" name="profile[website]" id="profile_website">
      </div>
    
      <i class="fa fa-facebook"></i>
      <div class="form-group url optional profile_facebook_url">
        <input class="string url optional form-control" placeholder="http://facebook.com/your-account" type="url" name="profile[facebook_url]" id="profile_facebook_url">
      </div>
    
      <i class="fa fa-twitter"></i>
      <div class="form-group url optional profile_twitter_url">
        <input class="string url optional form-control" placeholder="http://twitter.com/your-account" type="url" name="profile[twitter_url]" id="profile_twitter_url">
      </div>
    
      <i class="fa fa-instagram"></i>
      <div class="form-group url optional profile_instagram_url">
        <input class="string url optional form-control" placeholder="http://instagram.com/your-account" type="url" name="profile[instagram_url]" id="profile_instagram_url">
      </div>
    
      <input type="submit" name="commit" value="Add profile">
    </form>
    

    结果如下:

    result

    旁注

    请注意我正在使用Ruby on Rails,因此我的结果代码看起来有点夸张 . slim中的视图代码实际上非常简洁:

    i.fa.fa-link
    = f.input :website, label: false
    
    i.fa.fa-facebook
    = f.input :facebook_url, label: false
    
    i.fa.fa-twitter
    = f.input :twitter_url, label: false
    
    i.fa.fa-instagram
    = f.input :instagram_url, label: false
    
  • -7

    使可点击图标聚焦在文本输入元素内 .

    CSS

    .myClass {
        font-size:20px;
        position:absolute; top:10px; left:10px;
    }
    

    HTML

    <div>
        <label style="position:relative;">
             <i class="myClass fa fa-address-book-o"></i>
             <input class="w3-input" type="text" style="padding-left:40px;">
        </label>
    </div>
    

    只需在_446431_标签内添加任意图标,从Font Awesome库中添加,即可享受结果 .

  • 1

    无需编写很多代码......只需按照以下步骤操作:

    <input id="input_search" type="text" class="fa" placeholder="&#xf002 Search">
    

    你可以在这里找到Unicode( fontawesome )的链接......

    FontAwesome Unicode for icons

  • 13

    阅读了这个问题的各种版本并进行了搜索,我提出了一个非常干净,无js的解决方案 . 它与@allcaps解决方案类似,但避免了输入字体从主文档字体更改的问题 .

    使用::input-placeholder attribute专门设置占位符文本的样式 . 这允许您使用图标字体作为占位符字体,将您的正文(或其他字体)用作实际输入文本 . 目前,您需要指定特定于供应商的选择器 .

    只要您不需要输入元素中的图标和文本组合,这就可以正常工作 . 如果你这样做,那么你需要把占位符文本作为默认浏览器字体(我的普通衬线)用于单词 .

    例如 .
    HTML

    <p class="wrapper">
        <input class="icon" type="text" placeholder="&#61442;" />
    </p>
    

    CSS

    .wrapper {
        font-family:'arial', sans-serif;
    }
    input.icon::-webkit-input-placeholder {
        font-family:'FontAwesome';
    }
    

    使用浏览器前缀选择器:http://jsfiddle.net/gA4rx/78/

    请注意,您需要将每个特定于浏览器的选择器定义为单独的规则 . 如果将它们组合在一起,浏览器将忽略它 .

  • -1

    我找到了使用bootstrap 4的最简单方法 .

    <div class="input-group mb-3">
        <div class="input-group-prepend">
        <span class="input-group-text"><i class="fa fa-user"></i></span></div>
        <input type="text"/>
    </div>
    
  • 7

    你是对的 . :before和:after伪内容不适用于替换的内容,如 imginput 元素 . 添加包装元素并声明字体系列是其中一种可能性,就像使用背景图像一样 . 或者html5占位符文本可能符合您的需求:

    <input name="username" placeholder="&#61447;">
    

    不支持占位符属性的浏览器将忽略它 .

    更新

    before内容选择器选择输入: input[type="text"]:before . 您应该选择包装器: .wrapper:before . 见http://jsfiddle.net/allcaps/gA4rx/ . 我还添加了占位符建议,其中包装器是多余的 .

    .wrapper input[type="text"] {
        position: relative; 
    }
    
    input { font-family: 'FontAwesome'; } /* This is for the placeholder */
    
    .wrapper:before {
        font-family: 'FontAwesome';
        color:red;
        position: relative;
        left: -5px;
        content: "\f007";
    }
    
    <p class="wrapper"><input placeholder="&#61447; Username"></p>
    

    后备

    Font Awesome使用Unicode专用区(PUA)来存储图标 . 其他字符不存在并回退到浏览器默认值 . 这应该与任何其他输入相同 . 如果在输入元素上定义字体,则在我们使用图标的情况下提供与后备相同的字体 . 像这样:

    input { font-family: 'FontAwesome', YourFont; }
    
  • 2

    你可以使用包装器 . 在包装器中,添加字体awesome元素 iinput 元素 .

    <div class="wrapper">
        <i class="fa fa-icon"></i>
        <input type="button">
    </div>
    

    然后将包装器的位置设置为relative:

    .wrapper { position: relative; }
    

    然后将 i 元素的位置设置为绝对位置,并为其设置正确的位置:

    i.fa-icon { position: absolute; top: 10px; left: 50px; }
    

    (这是一个黑客,我知道,但它完成了工作 . )

  • 93

    如果您需要满足以下条件,则此答案将适用于您(当前答案均未满足这些条件):

    • 图标位于文本框内

    • 当文本输入到输入中时,图标不应消失,输入的文本将显示在图标的右侧

    • 单击该图标应使基础输入成为焦点

    我相信3是满足这些条件的最小HTML元素数:

    .input-icon{
      position: absolute;
      left: 3px;
      top: calc(50% - 0.5em); /* Keep icon in center of input, regardless of the input height */
    }
    input{
      padding-left: 17px;
    }
    .input-wrapper{
      position: relative;
    }
    
    <link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/>
    <div class="input-wrapper">
      <input id="stuff">
      <label for="stuff" class="fa fa-user input-icon"></label>
    </div>
    
  • 1
    <HTML>
    <head>
    <style>
    .inp1{
    color:#2E64FE;
    width:350px;
    height:35px;
    border:solid;
    font-size:20px;
    text-align:left;
    }
    </style>
    </head>
    
    <body>
    
    <div class="inp1">          
    <a href="#" class=""><i class="fa fa-search"></i></a>
    </div>
    

相关问题