css - 使用伪元素 :before in UL to create a vertical line

标签 css pseudo-element

我正在创建一个可以有子菜单的垂直菜单。对于这些,我尝试使用 CSS 伪元素 ::beforeborder 添加一条垂直线。

我面临的问题是 CSS 应用于整个菜单而不是特定的子菜单。

我认为问题在于 position: absolute; 的使用,但没有它,边框永远不会显示。

下面是代码,您可以在这个 JsFiddle 中查看问题.

<ul id="test-ul">
    <li><a>one</a></li>
    <li>
        <a>two</a>
        <ul class="submenu">
            <li><a>sub one</a></li>
            <li><a>sub two</a></li>
            <li><a>sub three</a></li>
        </ul>
    </li>
    <li><a>three</a></li>
    <li><a>four</a></li>
    <li><a>five</a></li>
</ul>

<style>
    /* reset defaults */
    ul { list-style: none; }

    /* apply style to menu */
    #test-ul {
        background-color: #eee; 
        border-color: #ccc;
        position: absolute;
    }

    /* style links */
    #test-ul > li a {
        color: #2b7dbc;
        border-top-color: #e4e4e4;
        background-color: #fff;
        display: block;
        padding: 7px 0 9px 20px;
        border-top-width: 1px;
        border-top-style: dotted;
    }

    /* do CSS3 magic and show a vertical border on the left of each submenu item */
    #test-ul > li > ul::before {
        content: "";
        display: block;
        position: absolute;
        z-index: 1;
        left: 18px;
        top: 0;
        bottom: 0;
        border: 1px dotted;
        border-width: 0 0 0 1px;
    }
</style>

最佳答案

简单地给.submenu一个relative的位置

.submenu{
    position: relative;
}

关于css - 使用伪元素 :before in UL to create a vertical line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166438/

相关文章:

javascript - 单击以将 div 移动到鼠标坐标

html - 让我的 h2 边框底部只是强调文本而不是 div

CSS :after not adding content to certain elements

javascript - 如何创建自动隐藏侧边栏

javascript - 为什么 Twitter 的关注按钮小部件不记得我?

css - 更改伪元素中数据 URI SVG 路径的填充颜色

javascript - :first-letter in Chrome 的奇怪行为

css - 结合 CSS 属性和伪元素选择器?

html - 如何删除 jquery UI datepicker 右侧的额外宽度?

php - 如何使图像的背景透明度为0?