将列表样式图像与文本按比例对齐

新手上路,请多包涵

所以我有

<ul>
  <li>Hello</li>
</ul>

li {
    list-style-image: url(../img/bullet.png); /* 13x13 px */
    /* line-height: 13px; */
    /* vertical-align: middle; */
    padding-left: 5px;
}

看起来像

在此处输入图像描述

如您所见 <li> 的文本和图像元素未垂直对齐(图像高于文本),我尝试对其应用行高和垂直对齐(注释掉代码),但它没有甚至它。有没有办法实现良好的对齐?

原文由 Ilja 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 282
2 个回答

填充只会影响元素内部的任何内容 - 在您的情况下是文本。

唯一可用的位置调整属性是 list-style-position ,但唯一允许的值是 inheritinsideoutside 。最常见的做法是使用:

 list-style: none;

然后应用所需的图像作为 <li> 的背景

li {
    /** the image will be vertically aligned in the center **/
    background: url(../img/bullet.png) left center no-repeat;

    /** move the text to the right **/
    padding-left: 20px;
}

原文由 Teneff 发布,翻译遵循 CC BY-SA 3.0 许可协议

你可以有

<li><span style="top:-5px; position:relative;">Text shifted 5px upper</span></li>

原文由 Fanky 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题