【问题标题】:how to add image icon inside input field in bootstrap?如何在引导程序的输入字段中添加图像图标?
【发布时间】:2019-07-08 23:21:14
【问题描述】:

我正在尝试使用引导程序在输入文本字段中添加图像图标。我在成功实现的实例的输入标签中使用了 font-awesome 图标。

您可以在下面给出的示例中看到:

上面例子的代码:

     <form  id="main-form" method="post" action="index.php">
        <div class="main-form">
                <div class="row" id="single">
                    <div class="col-md-9 shortfieldz">
                           <i class="zmdi zmdi-link"></i>

                             <input type="text" class="form-control main-input"  id='myInput' name="url" value="" placeholder="Paste a long url" /> 

                    </div>
                    <div class="col-md-3 shortbtnz">
                        <input class="btn btn-default btn-block main-button"  type="submit"   name="Shorten" value="Shorten">
                        <!--<button class="btn btn-primary btn-block main-button" id="copyurl" type="button">Copy</button>-->
                    </div>


                </div>
        </div>
      </form>

但现在我想用 img 标记替换字体真棒图标,以便为图像添加自定义:

这是我尝试过的:

   <form  id="main-form" method="post" action="index.php">
        <div class="main-form">
                <div class="row" id="single">
                    <div class="col-md-9 shortfieldz">
                            <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive" alt="Responsive image" width="30" height="24" /> 

                             <input type="text" class="form-control main-input"  id='myInput' name="url" value="" placeholder="Paste a long url" /> 

                    </div>
                    <div class="col-md-3 shortbtnz">
                        <input class="btn btn-default btn-block main-button"  type="submit"   name="Shorten" value="Shorten">
                        <!--<button class="btn btn-primary btn-block main-button" id="copyurl" type="button">Copy</button>-->
                    </div>


                </div>
        </div>
      </form>

输出:

参考可以看:http://www.bigto.in/

我正在寻找专门在引导程序中的解决方案。任何建议或方向都值得赞赏。

【问题讨论】:

标签: html css bootstrap-4


【解决方案1】:

使用 Bootstrap 的输入组。然后在 span 元素中添加图像。 https://getbootstrap.com/docs/4.0/components/input-group/

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon1">
      <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive" alt="Responsive image" width="30" height="24" />
    </span>
  </div>
  <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
</div>

工作码笔link

【讨论】:

  • 感谢您的回答,但这不是我要找的,而是提供一个想法。
【解决方案2】:

如果您只看检查器,您添加的链接将向您展示如何做到这一点。

shortfieldz 类的位置设置为相对。这告诉它相对于它的边界定位它的孩子

#main-form .main-form .shortfieldz {
    padding-right: 0px;
    position: relative; <--
}

然后图像是绝对定位的。我们使用 top 和 left 相对于父级,在本例中为 shortfieldz

#main-form .main-form .shortfieldz .zmdi-link {
    position: absolute; <---
    font-size: 28px;
    top: 15px; <---
    left: 25px; <---
    color: #8c8c8c;
    pointer-events: none;
}

然后您只需在左侧的输入中添加一个填充以说明其顶部的图像。

#main-form .main-form .main-input {
    padding: 0px 55px; <----
    border-width: 0;
    font-size: 19px;
    font-family: roboto;
    background: #f2f2f2;
    height: 55px;
    color: #151720;
    border-radius: 29px 0px 0px 29px;
    transition: all 0.2s linear;
    -webkit-transition: all 0.2s linear;
    -o-transition: all 0.2s linear;
    -ms-transition: all 0.2s linear;
    -moz-transition: all 0.2s linear;
}

【讨论】:

  • 非常感谢您突出显示 CSS 属性。
猜你喜欢
  • 1970-01-01
  • 2021-12-16
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多