简体   繁体   English

将图标添加到菜单项-图标无法正常显示,CSS问题

[英]Add icon to menu items - icon not showing properly, css issue

I am trying to add icons to my menu items. 我正在尝试向菜单项添加图标。 I have read many different tutorials on it, but the css that all tutorials recommend will not work for me, and I'm not sure what to do. 我已经阅读了许多不同的教程,但是所有教程推荐的css都不适合我,而且我不确定该怎么做。

Here is my html and css: 这是我的HTML和CSS:

<div class="hike-menu">
  <ul>
    <li class="icon">Menu Item 1</li>
    <li class="icon">Menu Item 2</li>
    <li class="icon">Menu Item 3</li>
    <li class="icon">Menu Item 4</li>
  </ul>
</div>
ul li {
  list-style-type: none;
  display: inline-block;
}

.icon {
  background-image: url("https://i.postimg.cc/ZWW7qTmm/calendar-yellow-copy.png");
  background-repeat: no-repeat;
  background-position: top;
}

And I have also uploaded in onto a jfiddle if you would like to take a look. 如果您想看一看,我也将其上传到jfiddle中。 You can see the exact problem I am having with the icon not showing. 您可以看到我不显示该图标的确切问题。 https://jsfiddle.net/katherinekonn/g1e4cok3/5/ https://jsfiddle.net/katherinekonn/g1e4cok3/5/

I should also note that it is absolutely necessary I use background-image for this, I am unable to place an img link directly into the html. 我还应注意,为此绝对需要使用background-image,因为我无法将img链接直接放置到html中。 Does anyone know how I can fix it? 有谁知道我该如何解决? I would like the icon to be above the list items text. 我希望图标位于列表项文本上方。

Here is an image of what I am trying to create: 这是我要创建的图像: 在此处输入图片说明

The main issue is related to the size of the icon you used. 主要问题与您使用的图标的大小有关。 The actual size is much bigger than you required. 实际大小比您所需的大得多。 So either you resize the image or apply background image size in css. 因此,您可以调整图像大小或在CSS中应用背景图像大小。 Then apply some padding-top for the <LI> to avoid overlapping of text and icon. 然后为<LI>应用一些填充顶部,以避免文本和图标重叠。

I tried modifying your css in fiddle and it worked perfect for me. 我尝试在小提琴中修改您的CSS,它对我来说非常完美。 please see it below 请在下面查看

ul li {
  list-style-type: none;
  display: inline-block;
}

.icon {
  background-image: url("https://i.postimg.cc/ZWW7qTmm/calendar-yellow-copy.png");
  background-size: 20px;
  background-repeat: no-repeat;
  background-position: top;
  padding-top: 30px;
}

Please try this. 请尝试这个。 It will work. 它会工作。

HTML HTML

<div class="hike-menu">
  <ul>
    <li class="element">
      <div class="icon">
      </div>
       <div class="text">
         Menu Item 1
       </div>
     </li>
    <li class="element">
      <div class="icon">
      </div>
       <div class="text">
         Menu Item 2
       </div>
     </li>
     <li class="element">
      <div class="icon">
      </div>
       <div class="text">
         Menu Item 2
       </div>
     </li>
     <li class="element">
      <div class="icon">
      </div>
       <div class="text">
         Menu Item 2
       </div>
     </li>
  </ul>
</div>

CSS CSS

ul li {
  list-style-type: none;
  display: inline-block;
}

.icon {
  background-image: url("https://i.postimg.cc/ZWW7qTmm/calendar-yellow-copy.png");
  background-repeat: no-repeat;
  background-position: top;
  background-size: cover;
  display: block;
  width: 24px;
  height: 24px;
  margin: 0 auto;
}

.text {
  display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM