如何将块颜色添加到列表的CSS中?

时间:2014-01-24 14:30:20

标签: html list css3

我正在使用HTML和CSS3制作图表,我能够轻松地整理列表颜色,但我希望只为每个列表的前30px设置样式

这是我的char到目前为止的样子以及我想要实现的目标:

enter image description here

CSS:

body {
    width:auto;
}

* {margin: 0; padding: 0;}

ul {
    line-height:20px;
}

.tree ul {
    padding-top: 20px; position: relative;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}

.tree li {
    float: left; text-align: center;
    list-style-type: none;
    position: relative;
    padding: 20px 5px 0 5px;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}



.tree li::before, .tree li::after{
    content: '';
    position: absolute; top: 0; right: 50%;
    border-top: 1px solid #ccc;
    width: 50%; height: 20px;
}
.tree li::after{
    right: auto; left: 50%;
    border-left: 1px solid #ccc;
}


.tree li:only-child::after, .tree li:only-child::before {
    display: none;
}


.tree li:only-child{ padding-top: 0;}


.tree li:first-child::before, .tree li:last-child::after{
    border: 0 none;
}

.tree li:last-child::before{
    border-right: 1px solid #ccc;
    border-radius: 0 0px 0 0;
    -webkit-border-radius: 0 0px 0 0;
    -moz-border-radius: 0 0px 0 0;
}
.tree li:first-child::after{
    border-radius: 0px 0 0 0;
    -webkit-border-radius: 0px 0 0 0;
    -moz-border-radius: 0px 0 0 0;
}

/*Time to add downward connectors from parents*/
.tree ul ul::before{
    content: '';
    position: absolute; top: 0; left: 50%;
    border-left: 1px solid #ccc;
    width: 0; height: 20px;
}

.tree li a{
    border: 1px solid #ccc;
    padding: 5px 10px;
    text-decoration: none;
    color: #666;
    font-family: arial, verdana, tahoma;
    font-size: 13px;
    display: inline-block;

    border-radius: 0px;
    -webkit-border-radius: 0px;
    -moz-border-radius: 0px;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}


.tree li a:hover, .tree li a:hover+ul li a {
    background: #dae2f2; color: #000; border: 1px solid #94a0b4;
}

.tree li a:hover+ul li::after, 
.tree li a:hover+ul li::before, 
.tree li a:hover+ul::before, 
.tree li a:hover+ul ul::before{
    border-color: #2663A6;
}

HTML:

<div class="tree">
<ul>
    <li>

             <a href="#"><span>Chairman / Owner</span><p>Name</p></a>

             <ul>
             <li>
             <a href="#">Managing Director</a>
             <ul>

             <li>
             <a href="#">Director</a>
             <li>
             <a href="#">Director</a>
             <li>
             <a href="#">Director</a>

             </ul>
             </ul>



    </li>
</ul>   



</div>

http://jsfiddle.net/F3CQk/

2 个答案:

答案 0 :(得分:4)

有很多方法可以解决这个问题。这是一个。

http://jsfiddle.net/isherwood/F3CQk/4/

span.accent {
    background-color: lightblue;
    display: block;
    margin: -5px -10px 5px;
    padding: 5px;
}

<a href="#"><span class="accent">Chairman / Owner</span><span>Name</span></a>

答案 1 :(得分:0)

你可以放两个LI标签而不是一个带有border-bottom的标签:0px border-top:0px。