【问题标题】:HTML custom (not Font-Awesome) icon class not workingHTML 自定义(非 Font-Awesome)图标类不起作用
【发布时间】:2023-03-06 15:21:01
【问题描述】:

我正在尝试将自定义图像包含为图标,而不是通常情况下来自 Font Awesome 的图像。因此,在我的 HTML 按钮中,我引用了 CSS 类,其中包含我想用作图标的本地图像的背景图像 url。但是,它没有显示在浏览器中。请注意,所有代码都经过全面验证。

HTML:

<div id="search">
<input type="text" class="searchTerm" placeholder="What are you looking for?">
  <button type="submit" class="searchButton">
    <i class="icon"></i>
 </button>

CSS:

.icon {
background: url('../imgs/search-icon.png');
height: 20px;
width: 20px;
}

小提琴:

#search {
  width: 100%;
  position: relative;
}

.searchTerm {
  float: left;
  width: 100%;
  border: 3px solid #00B4CC;
  padding: 5px;
  height: 20px;
  border-radius: 5px;
  outline: none;
  color: #9DBFAF;
}

.searchTerm:focus {
  color: #00B4CC;
}

.searchButton {
  position: absolute;
  right: -50px;
  width: 40px;
  height: 36px;
  border: 1px solid #00B4CC;
  background: #00B4CC;
  text-align: center;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  font-size: 20px;
}

.icon {
  background: url('https://postimg.org/image/bfac47qav/');
}
<div id="search">
  <input type="text" class="searchTerm" placeholder="What are you looking for?">
  <button type="submit" class="searchButton">
        <i class="icon"></i>
     </button>
</div>

【问题讨论】:

  • 你能不能把它上传给一些在线编辑,至少我们可以看到结构。
  • 您是否尝试过指定background-size,例如:background-size: contain?另外,您能否确认相关图标的文件路径有效?
  • 我会上传给编辑@PannaDas
  • @mts396 请分享链接
  • @UncaughtTypeError 我可以确认文件路径是正确的。我尝试了 background-size: contains 和其他尺寸,仍然没有出现

标签: html css icons


【解决方案1】:

简单的方法是,使用content。不要使用background

#search {
  width: 100%;
  position: relative;
}

.searchTerm {
  float: left;
  width: 100%;
  border: 3px solid #00B4CC;
  padding: 5px;
  height: 20px;
  border-radius: 5px;
  outline: none;
  color: #9DBFAF;
}

.searchTerm:focus {
  color: #00B4CC;
}

.searchButton {
  position: absolute;
  right: -50px;
  width: 40px;
  height: 36px;
  border: 1px solid #00B4CC;
  background: #00B4CC;
  text-align: center;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  font-size: 20px;
}

.icon {
  content: url("https://media.giphy.com/media/PAbELXsl2P7mE/giphy.gif");
  height: 40px;
  width: 40px;
}
<div id="search">
  <input type="text" class="searchTerm" placeholder="What are you looking for?">
  <button type="submit" class="searchButton">
        <i class="icon"></i>
     </button>
</div>

【讨论】:

  • 最简单的修复方法,将“背景”替换为“内容”即可。
【解决方案2】:

你可以试试这个

BUTTON {
 padding: 8px 8px 8px 32px;
 font-family: Arial, Verdana;   
background: #f0f0f0 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABfUlEQVQ4y42TP2tUQRTFf3PnPYiLIAiKiKhIsLbxS9hZaykWNmKjgqT0A6SxslRIFRsRv0QwdZB1YyGipImbzWbfnHst3sZdZF92Bw7D/LmH37nDpL1NAyAlHgDXWW18j+A9QBXTnQhu3H704TXS2aU5s/f2/qvTZeXx78goDdE0Z9anusYDmxn4zMAlYglBMsN9zkAzgxwrGGCGnLzIwELCVZbUG+oicDWoLOtBhZwcAQFUZd6gCC/dBPW5Hr+/7VPaCHci2DU5TJVDBe+Q5cyvwT4ucfdleS7noQdURfMEDd4Rwc04OvzDtfWbjCYTitpG/hehoI4IZhmpjTgcj09jUGmeQAVXB4EypbRxjk5O0JTAvgygOBSnap9xsUJC0/nx4BLFW3p78c7Z6SeOJ2ktvL2wUC7G4zHh4uBgxPEkre30Ewb0NrZ0+XDEeUtGXdULZckYDodYMp59vLD9eZftjS31EnARuPr0Xn6yfoVbq/zlrz/pb37SG+DHXyrMGdFXikHpAAAAAElFTkSuQmCC);
    background-position: 8px 8px;
    }

fiddle http://jsfiddle.net/EFsU8/ 并在 using images inside <button> element 中解决,你也可以检查一下

【讨论】:

    【解决方案3】:

    试试这个

    将display: block 添加到类图标中,根据@fbuchlak 建议的答案,但您还必须检查图像路径。

    这不是图片的正确路径

    https://postimg.org/image/bfac47qav/

    这是正确的路径

    https://s7.postimg.org/5ecn753or/search-icon.png

    下面是例子

    #search{
      width:100%;
      position:relative;
    }
    .searchTerm {
      float: left;
      width: 100%;
      border: 3px solid #00B4CC;
      padding: 5px;
      height: 20px;
      border-radius: 5px;
      outline: none;
      color: #9DBFAF;
    }
    .searchTerm:focus{
      color: #00B4CC;
    }
    .searchButton {
      position: absolute;  
      right: -50px;
      width: 40px;
      height: 36px;
      border: 1px solid #00B4CC;
      background: #00B4CC;
      text-align: center;
      color: #fff;
      border-radius: 5px;
      cursor: pointer;
      font-size: 20px;
    }
    .icon{
       background: url('https://s7.postimg.org/5ecn753or/search-icon.png');
       width:20px;
        height: 20px;
        background-position: center;
        background-size:cover; 
        display:block;
    }
    <div id="search">
      <input type="text" class="searchTerm" placeholder="What are you looking for?">
          <button type="submit" class="searchButton">
            <i class="icon"></i>
         </button>
      </div>

    【讨论】:

    • 酷!快乐编码。
    【解决方案4】:
        .searchButton {
        background: #fff url(http://pixsector.com/cache/e7836840/ave6d43d85eb121839a10.png) no-repeat;
        background-size: contain;
        width: 4%;
        height: 21px;
    }
    
    
    
     <div id="search">
    <input type="text" class="searchTerm" placeholder="What are you looking for?">
     <button type="submit" class="searchButton">
       <span class="icon"></span> 
       </button>
       </div>
    

    https://jsfiddle.net/5tm168uk/13/

    【讨论】:

      猜你喜欢
      • 2022-06-28
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2014-05-10
      • 1970-01-01
      • 2023-04-09
      • 2017-07-13
      相关资源
      最近更新 更多