html - 定位一个伪元素 li :after beyond li

标签 html css pseudo-element

我有一个带有 ul 列表的菜单。

我需要在每个 <li> 下面添加一个小文本

不幸的是,我无法添加 html 元素,因为它是 native CMS 菜单(我可以进行覆盖,但我想找到其他方式)。

所以这是我所做的:

演示:http://jsfiddle.net/Vinyl/6dcnztax/

我申请了 position: relative;<li>和一个 position: absolute;li:after

HTML

<ul>
    <li class="item-131">text 1</li>
    <li class="item-132">text 2</li>
    <li class="item-133">text 3</li>
</ul>

CSS

ul {
    font-size: 16px;
}
ul li {
    height: 15px;
    line-height: 15px;
    margin: 0 auto;
    position: relative;
    z-index: 1;
    padding: 5px 20px 5px 5px;
}
.item-131 {
    position: relative;
}
.item-131:after {
    content:"small text 1";
    font-size: 13px;
    color: #88857d;
    position: absolute;
    top:15px;
    left:5px;
    width: 100%;
}
.item-132:after {
    content:"small text 2";
    font-size: 13px;
    color: #88857d;
    width: 100%;
}
.item-133:after {
    content:"small text 3";
    font-size: 13px;
    color: #88857d;
}

你知道有没有更好的方法?

最佳答案

这样试试:http://jsfiddle.net/xyr1b29q/1/

ul {
    font-size: 16px;
}

ul li {
    min-height: 15px;
    line-height: 15px;
    margin: 0 auto;
    padding: 5px 20px 5px 5px;
}

ul li:after {
    font-size: 13px;
    color: #88857d;
    width: 100%;
    display: block;
}


.item-131:after { content:"small text 1"; }
.item-132:after { content:"small text 2"; }
.item-133:after { content:"small text 3"; }

没有真正需要使用relative/absolute 位置来将文本放在新行中。

对列表项使用min-height 而不是height。还为 :after 伪元素定义所有样式属性一次。


结果截图:

enter image description here

关于html - 定位一个伪元素 li :after beyond li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621368/

相关文章:

css - 开发中样式表的渲染速度非常慢

java - 如何在jsoup中找到带有pseudoElement::before的HTML标签

html - 使用 iframe 时,嵌入式 base64 pdf 不会在 IE 中显示

html - 仅将媒体查询添加到特定设备 ipad pro

javascript - 固定位置侧边栏不适应不同的分辨率

html - 使立方体尺寸变小

css - 有没有办法欺骗 IE7 使用伪元素 :after and :before?

css - 为什么不能将特定于供应商的伪元素/类组合到一个规则集中?

HTML 电子邮件 - 为链接的一部分着色

javascript - 根据不包括年份的实际日期显示/隐藏 div