【问题标题】:Why did my CSS for replacing a list item bullet with a background icon stop working?为什么我用背景图标替换列表项项目符号的 CSS 停止工作?
【发布时间】:2021-12-25 16:35:16
【问题描述】:

这个 CSS 用于替换列表项项目符号的社交媒体图标,但现在它给了我一个显示项目符号、图标和文本的列表。

为什么有“list-style: none;”停止工作?

.social-follow {
  margin: 0.2em 0 2em 3em;
}

.social-follow ul.follow-background {
  list-style: none outside !important;
  margin: 0 0 0 0.6em;
  padding: 0 0 0 0.6em;
  overflow: hidden;
}

.social-follow ul.follow-background + li.follow {
  line-height: 32;
  background: no-repeat left 1px;
  /** fixed vertical position **/
  margin: 0.3em 0 0 -0.6em;
  padding: 0 0 0 2.7em;
  overflow: hidden;
}

li.follow.chapicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/chapter_logo_54pxtype-32x32-28x28.png') no-repeat left 1px;
}

li.follow.facebookicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/facebook-icon-28x28.png') no-repeat left 1px;
}

li.follow.flickricon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/fluid-flickr-logo-28x28.png') no-repeat left 1px;
}

li.follow.instaicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/instagram-logo-28x28.png') no-repeat left 1px;
}

li.follow.linkedinicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/linkedin-logo-28x28.png') no-repeat left 1px;
}

li.follow.pinteresticon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/pinterest-logo-28x28.png') no-repeat left 1px;
}

li.follow.rssblueicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/rss-logo-blue-28x28.png') no-repeat left 1px;
}

li.follow.skylineicon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/logo-skyline_only-28x28.png') no-repeat left 1px;
}

li.follow.twittericon {
  background: url('https://wdcb.stcwdc.org/wp-content/uploads/twitter-logo-28x28.png') no-repeat left 1px;
}
<div class="social-follow">
  <ul class="follow-background">
    <li class="follow facebookicon"><a href="https://www.facebook.com/STCWDCMB/" rel="nofollow noopener noreferrer">Facebook page</a></li>
    <li class="follow flickricon"><a href="https://www.flickr.com/photos/stcwdc/sets" rel="nofollow noopener noreferrer">Flickr Photo Albums</a></li>
    <li class="follow instaicon"><a href="https://www.instagram.com/stc_wdcb/" rel="nofollow noopener noreferrer">Instagram</a></li>
    <li class="follow linkedinicon"><a href="https://www.linkedin.com/groups?gid=156478" rel="nofollow noopener noreferrer">LinkedIn Group</a></li>
    <li class="follow pinteresticon"><a href="https://www.pinterest.com/stcwdcmb/" rel="nofollow noopener noreferrer">Pinterest page</a></li>
    <li class="follow twittericon"><a href="https://twitter.com/stcwdc" rel="nofollow noopener noreferrer">Twitter @stcwdc</a></li>
  </ul>
</div>

Opera、Firefox、Safari 和 Google Chrome 中的当前结果:

【问题讨论】:

  • 拼写错误!重要;
  • .social-follow ul.follow-background+li.follow必须是.social-follow ul.follow-background li.follow
  • @Madhuri 是的,谢谢。在发布这个问题之前,我以为我已经解决了这个问题。
  • @connexo 谁说line-height: 32' 将不起作用。请如果您不了解 CSS,请不要说您不知道的内容。 CSS 在没有单位的情况下大部分时间都可以正常工作,事实上, line-height 就是这样使用的!
  • 你可以说这样使用它违反了规范。但是像这样使用它是有效的。 padding: 0 也有效!

标签: css background-image bulletedlist


【解决方案1】:

这里有几个问题,但首先要注意一个问题已经消失。问题中的图像显示了项目符号和图标,但问题中当前显示的代码不显示项目符号。我相信这是在 cmets 中处理的(!important 上有一个错字)。

现在还有几件事。对除 0 以外的行高进行无单位设置是不可行的(对于大多数情况来说,这可能是无单位的),因此这个 sn-p 应用了一个它假设是想要的 px。 [请注意问题中的 sn-p,因为它所在的类从未执行过,因此不会被拾取,请参阅下一点。我相信有些浏览器可能是“善良的”并假设为 px,但不要依赖它]。

正如@connexo 所指出的,有一大块 CSS 从未应用:

.social-follow ul.follow-background + li.follow {
  line-height: 32;
  background: no-repeat left 1px;
  /** fixed vertical position **/
  margin: 0.3em 0 0 -0.6em;
  padding: 0 0 0 2.7em;
  overflow: hidden;
}

MDN:

相邻的兄弟组合符 (+) 分隔两个选择器,并且仅当它紧跟在第一个元素之后,并且它们都是同一个父元素的子元素时才匹配第二个元素。

li 元素是 ul 元素的子元素,而不是兄弟元素,所以这个选择器被忽略了。

但是,在解决这个问题时,我们发现了另一个问题 - 有一个背景设置比另一个更具体。所以这个:

background: no-repeat left 1px;

现在覆盖这个:

  background: url('https://wdcb.stcwdc.org/wp-content/uploads/chapter_logo_54pxtype-32x32-28x28.png') no-repeat left 1px;

请记住,背景是一个复合属性 - 设置几项内容。

为了清楚起见,这个 sn-p 将事物分成单独的组件。

.social-follow {
  margin: 0.2em 0 2em 3em;
}

.social-follow ul.follow-background {
  list-style: none outside !important;
  margin: 0 0 0 0.6em;
  padding: 0 0 0 0.6em;
  overflow: hidden;
}

.social-follow ul.follow-background li.follow {
  line-height: 32px;
  background-repeat: no-repeat;
  background-position: left 1px;
  /** fixed vertical position **/
  margin: 0.3em 0 0 -0.6em;
  padding: 0 0 0 2.7em;
  overflow: hidden;
}

li.follow.chapicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/chapter_logo_54pxtype-32x32-28x28.png');
}

li.follow.facebookicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/facebook-icon-28x28.png');
}

li.follow.flickricon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/fluid-flickr-logo-28x28.png');
}

li.follow.instaicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/instagram-logo-28x28.png');
}

li.follow.linkedinicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/linkedin-logo-28x28.png');
}

li.follow.pinteresticon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/pinterest-logo-28x28.png');
}

li.follow.rssblueicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/rss-logo-blue-28x28.png');
}

li.follow.skylineicon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/logo-skyline_only-28x28.png');
}

li.follow.twittericon {
  background-image: url('https://wdcb.stcwdc.org/wp-content/uploads/twitter-logo-28x28.png');
}
<div class="social-follow">
  <ul class="follow-background">
    <li class="follow facebookicon"><a href="https://www.facebook.com/STCWDCMB/" rel="nofollow noopener noreferrer">Facebook page</a></li>
    <li class="follow flickricon"><a href="https://www.flickr.com/photos/stcwdc/sets" rel="nofollow noopener noreferrer">Flickr Photo Albums</a></li>
    <li class="follow instaicon"><a href="https://www.instagram.com/stc_wdcb/" rel="nofollow noopener noreferrer">Instagram</a></li>
    <li class="follow linkedinicon"><a href="https://www.linkedin.com/groups?gid=156478" rel="nofollow noopener noreferrer">LinkedIn Group</a></li>
    <li class="follow pinteresticon"><a href="https://www.pinterest.com/stcwdcmb/" rel="nofollow noopener noreferrer">Pinterest page</a></li>
    <li class="follow twittericon"><a href="https://twitter.com/stcwdc" rel="nofollow noopener noreferrer">Twitter @stcwdc</a></li>
  </ul>
</div>

【讨论】:

  • 这是它所在页面的 URL。wdcb.stcwdc.org/social-media>
  • 您好,抱歉,我没有看到该页面有问题,您还有问题吗?任何答案都解决了您的问题吗?
  • 在我对上面的新代码建议进行更改以匹配我当前的类名后,它就起作用了。我一直在尝试如此多的变化并从头开始重做所有事情,以至于 sn-p 中使用的类名不再起作用。它现在可以正确显示,除非我尝试使用 aria-labelling 将 WAI Aria 添加到屏幕阅读器的代码中,然后所有图标都消失了。这是我正在研究的另一个问题。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 2013-01-25
相关资源
最近更新 更多