如何在引导程序的输入字段内添加图像图标?

时间:2019-02-14 20:41:51

标签: html css bootstrap-4

我正在尝试使用引导程序在输入文本字段内添加图像图标。对于成功实现的实例,我在输入标签中使用了超棒的字体图标。

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

here

上面示例的

代码:

     <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>

输出here

供参考,您可以看到:http://www.bigto.in/

我正在寻找专门用于引导的解决方案。任何建议或指示都很好。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您添加的链接向您展示了如何仅查看检查器即可执行的操作。

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;
}

答案 2 :(得分:0)

class =“ zmdi zmdi-link” 标签添加到 img 标签中,然后替换标签,例如:

 <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive zmdi zmdi-link" alt="Responsive image " width="30" height="24" />

最终代码为:

<div class="col-md-9 shortfieldz">
                         <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive zmdi zmdi-link" 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>

应用html代码后,更改某些CSS属性,例如:

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

结果: show