码迷,mamicode.com
首页 > 其他好文 > 详细

[转]Add Bootstrap Glyphicon to Input Box

时间:2017-05-09 12:44:08      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:get   when   date   load   comm   png   tag   inpu   int   

本文转自:http://stackoverflow.com/questions/18838964/add-bootstrap-glyphicon-to-input-box

 

How can I add a glyphicon to a text type input box? For example I want to have ‘icon-user‘ in a username input, something like this:

技术分享

 

576down voteaccepted

Without Bootstrap:

We‘ll get to Bootstrap in a second, but here‘s the fundamental CSS concepts in play in order to do this yourself.  As beard of prey points out, you can do this with CSS by absolutely positioning the icon inside of the input element.  Then add padding to either side so the text doesn‘t overlap with the icon.

So for the following HTML:

<div class="inner-addon left-addon">
    <i class="glyphicon glyphicon-user"></i>
    <input type="text" class="form-control" />
</div>

You can use the following CSS to left and right align glyphs:

/* enable absolute positioning */
.inner-addon { 
    position: relative; 
}

/* style icon */
.inner-addon .glyphicon {
  position: absolute;
  padding: 10px;
  pointer-events: none;
}

/* align icon */
.left-addon .glyphicon  { left:  0px;}
.right-addon .glyphicon { right: 0px;}

/* add padding  */
.left-addon input  { padding-left:  30px; }
.right-addon input { padding-right: 30px; }

Demo in Plunker

技术分享

Note: This presumes you‘re using glyphicons, but works equally well with font-awesome.   For FA, just replace .glyphicon with .fa


With Bootstrap:

As buffer points out, this can be accomplished natively within Bootstrap by using Validation States with Optional Icons.  This is done by giving the .form-group element the class of .has-feedback and the icon the class of .form-control-feedback.

The simplest example would be something like this:

<div class="form-group has-feedback">
    <label class="control-label">Username</label>
    <input type="text" class="form-control" placeholder="Username" />
    <i class="glyphicon glyphicon-user form-control-feedback"></i>
</div>

Pros:

  • Includes support for different form types (Basic, Horizontal, Inline)
  • Includes support for different control sizes (Default, Small, Large)

Cons:

  • Doesn‘t include support for left aligning icons

To overcome the cons, I put together this pull-request with changes to support left aligned icons.  As it is a relatively large change, it has been put off until a future release, but if you need these features today, here‘s a simple implementation guide:

If you‘re using less, just include these changes in your project. If you‘re not using less, grab the file and convert it to CSS with any online LESS Compiler.

Then, all you have to do is include the class .has-feedback-left on any group that has the class .has-feedback in order to left align the icon.

Since there are a lot of possible html configurations over different form types, different control sizes, different icon sets, and different label visibilities, I created a test page that shows the correct set of HTML for each permutation along with a live demo.

Here‘s a demo in Plunker

技术分享

P.S. frizi‘s suggestion of adding pointer-events: none; has been added to bootstrap

Didn‘t find what you were looking for? Try these similar questions:

 

[转]Add Bootstrap Glyphicon to Input Box

标签:get   when   date   load   comm   png   tag   inpu   int   

原文地址:http://www.cnblogs.com/freeliver54/p/6829788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!