CSS - 无序列表中元素符号之间的垂直线

标签 css list html-lists

我将如何在无序列表中的元素符号之间绘制一条垂直线,如下所示:

enter image description here

请注意,该行停在最后一个列表元素符号处。我正在使用 list-style:none; 和图像作为元素符号。 HTML 看起来像这样:

<ul class="experiences">

  <!-- Experience -->
  <li class="green">
    <div class="where">New York Times</div>
    <h3 class="what green">Senior Online Editor</h3>
    <div class="when">2012 - Present</div>

    <p class="description">Jelly-o pie chocolate cake...</p>
   </li>

   ...

请求的 CSS 代码:

/* Experiences */
ul.experiences {
    padding-left: 15px;
    margin-top: -1px;
}
ul.experiences li {
    padding-left: 33px;
    margin-bottom: 2.5em;
    list-style: none;
    background: url('../img/misc/list-bullet-darkgray.png') no-repeat;
}
ul.experiences li.green {
    background: url('../img/misc/list-bullet-green.png') no-repeat;
}
ul.experiences li.blue {
    background: url('../img/misc/list-bullet-blue.png') no-repeat;
}
ul.experiences li.pink {
    background: url('../img/misc/list-bullet-pink.png') no-repeat;
}
.where {
    font-size: 1.2857em; /* 18/16 -> 18px */
    font-weight: 300;
    display: inline;
    margin-right: 0.5em;
}
.what {
    font-size: 0.75em; /* 12/16 -> 12px */
    font-weight: 700;
    text-transform: uppercase;
    color: #fff;
    background-color: #444444;
    display: inline-block;
    padding: 0 12px;
    margin: -5px 0.5em 0 0 !important;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}
.what.green {
    background-color: #c4df9b;
}
.what.blue {
    background-color: #6dcff6;
}
.what.pink {
    background-color: #f06eaa;
}
.when {
    float: right;
    color: #b9b9b9;
    font-style: italic;
}
.description {
    display: block;
    margin-top: 0.5em;
}

最佳答案

我怀疑这是否可以像其他人建议的那样仅使用边框和“摆弄边距”来实现,至少我没有运气这样做。

此解决方案使用 CSS 生成的内容(:before:after)来绘制元素符号和线条。它允许 高度自定义并保持标记干净,但请注意 browser support .

JSFiddle (滚动 CSS 直到 /* BORDERS AND BULLETS */ 注释)

ul.experiences li {
    position:relative; /* so that pseudoelements are positioned relatively to their "li"s*/
    /* use padding-bottom instead of margin-bottom.*/ 
    margin-bottom: 0; /* This overrides previously specified margin-bottom */
    padding-bottom: 2.5em;
}

ul.experiences li:after {
    /* bullets */
    content: url('http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/RedDisc.svg/20px-RedDisc.svg.png');
    position: absolute;
    left: -26px; /*adjust manually*/
    top: 0px;
}

ul.experiences li:before {
    /* lines */
    content:"";
    position: absolute;
    left: -16px; /* adjust manually */
    border-left: 1px solid black;
    height: 100%;
    width: 1px;
}

ul.experiences li:first-child:before {
   /* first li's line */
   top: 6px; /* moves the line down so that it disappears under the bullet. Adjust manually */
}

ul.experiences li:last-child:before {
    /* last li's line */
   height: 6px; /* shorten the line so it goes only up to the bullet. Is equal to first-child:before's top */
}

注意:如果线条的 border-color 指定了 alpha channel ,则第一个和第二个元素的边框之间的重叠将很明显。

关于CSS - 无序列表中元素符号之间的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17629286/

相关文章:

python - Python 中的算术序列切片

html - 节点中持续元素的CSS选择器

javascript - 如何使用 jquery 和 css3 在移动设备上实现 *SMOOTH* 淡入淡出动画?

c# - 尝试根据字符串值对列表进行排序

html - css border-bottom 失控

Java List.add() UnsupportedOperationException

html - :active only briefly works (during mouse click)

jquery - 将 UL 设置为移动设备上的下拉列表和桌面上的全宽

javascript - 使用本地存储来记住 css 显示值

html - 用覆盖整个背景的图像写在容器内