带有图片的HTML下拉菜单

3
我正在尝试构建一个像图像上那样的HTML菜单。我不知道该从何开始。我考虑使用最常见的ul,但是如何绘制线条呢?
我知道如何构建菜单。问题在于图片和水平线。
1个回答

2

演示 Fiddle

您可以使用 :before:after 伪元素来绘制线条和箭头,以下是一个基本示例:

HTML

<ul>
    <li>
        <img src='http://openclipart.org/people/TzeenieWheenie/TzeenieWheenie_Small_blue_i_info_button.svg' />Item
        <ul>
            <li>Sub Item</li>
        </ul>
    </li><li>
        <img src='http://openclipart.org/people/TzeenieWheenie/TzeenieWheenie_Small_blue_i_info_button.svg' />Item
        <ul>
            <li>Sub Item</li>
        </ul>
    </li>
</ul>

CSS

ul, li {
    list-style:none;
    margin:0;
    padding:0;
}
ul {
    background: rgb(238, 238, 238);
    /* Old browsers */
    /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;
    base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2VlZWVlZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNjY2NjY2MiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-linear-gradient(top, rgba(238, 238, 238, 1) 0%, rgba(204, 204, 204, 1) 100%);
    /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(238, 238, 238, 1)), color-stop(100%, rgba(204, 204, 204, 1)));
    /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(238, 238, 238, 1) 0%, rgba(204, 204, 204, 1) 100%);
    /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(238, 238, 238, 1) 0%, rgba(204, 204, 204, 1) 100%);
    /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(238, 238, 238, 1) 0%, rgba(204, 204, 204, 1) 100%);
    /* IE10+ */
    background: linear-gradient(to bottom, rgba(238, 238, 238, 1) 0%, rgba(204, 204, 204, 1) 100%);
    /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#cccccc', GradientType=0);
    /* IE6-8 */
    padding:0 20px;
    border-bottom:1px solid green;
}
li {
    display:inline-block;
    position:relative;
    padding:40px;
    cursor:pointer;
    font-family:arial;
}
li img {
    width:30px;
    height:30px;
    display:block;
    margin:-30px 0 40px 0;
}
li:before {
    display:inline-block;
    content:'';
    top:50px;
    left:0;
    width:100%;
    border-top:1px solid #808080;
    position:absolute;
}
li:after {
    display:none;
}
li:hover:after {
    display:inline-block;
    content:'';
    top:50px;
    left:40%;
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 10px 10px 0 10px;
    border-color: green transparent transparent transparent;
    position:absolute;
}
ul li:hover ul {
    display:block;
}
ul ul {
    display:none;
    position:absolute;
    top:140px;
    left:0px;
    background:rgba(255, 255, 255, .5);
    border:0;
    padding:0;
}
ul ul li:before, ul ul li:after {
    display:none;
}
ul ul li {
    display:block;
    font-family:tahoma;
    font-size:12px;
    color:green;
    padding:10px 30px;
    width:100px;
    font-weight:bold;
    border-bottom:1px solid #c0c0c0;
    text-transform:uppercase;
}

我认为他在询问如何将图像作为父链接包含在内。然后添加一行来区分子链接和父链接。 - leigero
+1 现在这是一个漂亮的小提琴!这个例子做得很好。 - leigero
谢谢。我会尝试一下。需要做些小调整。之后我会接受你的答案。 - user3070462
@SW4,是否可以设置图像和文本的链接,以便菜单项的整个区域都可以点击? - user3070462

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接