为什么我的悬停背景会延伸到容器之外

时间:2014-03-07 14:07:57

标签: css

我对CSS比较陌生,并试图创建一个CSS DropDown菜单。

经过几个小时的css阅读后,我创建了一个,但是在父母UL上的最后一个孩子在悬停时会流血。

这是JSFiddle链接:JSFiddle Code

<mini-nav>
<ul>
    <li><a href="#">Services</a>
        <ul>
            <li>Service 1</li>
            <li>Service 2</li>
            <li>Service 3</li>
            <li>Service 4</li>
        </ul>
    </li>
    <li><a href="#">Security</a>
        <ul>
            <li>Security 1</li>
            <li>Security 2</li>
            <li>Security 3</li>
        </ul>
    </li>
    <li><a href="#">Centres</a>
        <ul>
            <li>Centres 1</li>
            <li>Centres 2</li>
            <li>Centres 3</li>
        </ul>
    </li>
</ul>

mini-nav ul 
{
    width: 237px;
    text-align: center;
    display: inline;
    margin: 0;
    padding: 5px 0 5px 0px;
    list-style-type: none;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    background: #58585A;
    box-shadow: 3px 3px 5px #828282;
}
mini-nav ul li 
{
    font-size: 13px;
    display: inline-block;
    margin-right: -4px;
    position: relative;
    width: 78px;
    padding: 5px 0 5px 0px;
    border-right: 1px solid white;
    cursor: pointer;
    -webkit-transition: all 0.2s;
    -moz-transition: all 0.2s;
    -ms-transition: all 0.2s;
    -o-transition: all 0.2s;
    transition: all 0.2s;
}
mini-nav ul > li:last-child 
{ 
    border-right: none; 
}
mini-nav ul li:hover 
{
    background: #E8E8E8; 
    color: black; 
    background-image: url(../mes-images/menu-topper.png);
    background-position: top left;
    background-repeat: repeat-x;
    display: inline-block;
}

mini-nav ul li:hover:first-child 
{
    -moz-border-radius: 5px 0 0 0;
    -webkit-border-radius: 5px 0 0 0;
    border-radius: 5px 0 0 0;
    -webkit-background-clip: padding-box; 
    -moz-background-clip:    padding; 
    background-clip:         padding-box;
}
mini-nav ul li:hover:last-child 
{
    -moz-border-radius: 0 5px 0 0;
    -webkit-border-radius: 0 5px 0 0;
    border-radius: 0 5px 0 0;
    -webkit-background-clip: padding-box; 
    -moz-background-clip:    padding; 
    background-clip:         padding-box;
}

mini-nav ul li:hover a 
{ 
    color: black; 
}

mini-nav ul li:hover ul 
{
  display: block;
  opacity: 0.8;
  visibility: visible;
}

mini-nav ul li a 
{ 
    color: White; text-decoration: none; 
}

mini-nav ul li ul 
{
    text-align: left;
    padding:0;
    position: absolute;
    top: 27px;
    left: 0;
    width: auto;
    display: none;
    visibility: hidden;
    -webkit-transiton: opacity 0.2s;
    -moz-transition: opacity 0.2s;
    -ms-transition: opacity 0.2s;
    -o-transition: opacity 0.2s;
    -transition: opacity 0.2s;
    -moz-border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    border-radius: 0 0 5px 5px;
}
mini-nav ul ul > li:last-child 
{ 
    border-bottom: none; 
}
mini-nav ul li ul li:hover 
{
    background: #E8E8E8; 
    color: black;
    width: 110px;
}
mini-nav ul li ul li 
{  
  display: block; 
  color: #fff;
  width: 110px;
  border-bottom: solid 1px silver;
  padding-left: 12px;
  border-right: none;
  margin-right: 0;
}
mini-nav ul li ul li:hover:last-child 
{
    -moz-border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    border-radius: 0 0 5px 5px;
}

我为我的卑鄙的css道歉!!!

2 个答案:

答案 0 :(得分:1)

在您的Css上,找到此代码

mini-nav ul > li:last-child { border-right: none; }

并在其中添加“margin:0”,如下所示:

mini-nav ul > li:last-child { border-right: none; margin:0; }

最后,这就是你想要的?

DEMO

链接断开时的工作代码:

    /* Parent CSS */
mini-nav ul {
    width: 237px;
    text-align: center;
    display: inline;
    margin: 0;
    padding: 5px 0 5px 0px;
    list-style-type: none;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    background: #58585A;
    box-shadow: 3px 3px 5px #828282;
}
mini-nav ul li {
    font-size: 13px;
    display: inline-block;
    margin-right:-4px;
    position: relative;
    width: 78px;
    padding: 5px 0 5px 0px;
    border-right: 1px solid white;
    cursor: pointer;
    -webkit-transition: all 0.2s;
    -moz-transition: all 0.2s;
    -ms-transition: all 0.2s;
    -o-transition: all 0.2s;
    transition: all 0.2s;
}
mini-nav ul > li:last-child { border-right: none; margin:0; }
mini-nav ul li:hover 
{
    background: #E8E8E8; 
    color: black; 
    background-image: url(../mes-images/menu-topper.png);
    background-position: top left;
    background-repeat: repeat-x;
    display: inline-block;
}

mini-nav ul li:hover:first-child {
    -moz-border-radius: 5px 0 0 0;
    -webkit-border-radius: 5px 0 0 0;
    border-radius: 5px 0 0 0;
    -webkit-background-clip: padding-box; 
    -moz-background-clip:    padding; 
    background-clip:         padding-box;
}
mini-nav ul li:hover:last-child {
    -moz-border-radius: 0 5px 0 0;
    -webkit-border-radius: 0 5px 0 0;
    border-radius: 0 5px 0 0;
    -webkit-background-clip: padding-box; 
    -moz-background-clip:    padding; 
    background-clip:         padding-box;
}

mini-nav ul li:hover a { color: black; }

mini-nav ul li:hover ul {
  display: block;
  opacity: 0.8;
  visibility: visible;
}

mini-nav ul li a { color: White; text-decoration: none; }

/* Child CSS */
mini-nav ul li ul {
    text-align: left;
    padding:0;
    position: absolute;
    top: 27px;
    left: 0;
    width: auto;
    display: none;
    visibility: hidden;
    -webkit-transiton: opacity 0.2s;
    -moz-transition: opacity 0.2s;
    -ms-transition: opacity 0.2s;
    -o-transition: opacity 0.2s;
    -transition: opacity 0.2s;
    -moz-border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    border-radius: 0 0 5px 5px;
}
mini-nav ul ul > li:last-child { border-bottom: none; }
mini-nav ul li ul li:hover {
    background: #E8E8E8; 
    color: black;
    width: 110px;
}
mini-nav ul li ul li {  
  display: block; 
  color: #fff;
  width: 110px;
  border-bottom: solid 1px silver;
  padding-left: 12px;
  border-right: none;
  margin-right: 0;
}
mini-nav ul li ul li:hover:last-child {
    -moz-border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    border-radius: 0 0 5px 5px;
}

答案 1 :(得分:0)

这是因为.mini-nav ul li

position:relative;
margin-right:-4px;

你已经让它向右移动了4个像素并将其丢弃。