2016-10-07 35 views
0

我有一个带有按钮的导航栏作为背景图像,当我将鼠标悬停在上面时会发生变化。但是,我似乎无法将按钮居中。如何将NAV栏与背景图像对齐?

nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: blue; 
 
} 
 
.nave { 
 
    width: 100%; 
 
    max-width: 1200px; 
 
    margin: 0 auto; 
 
    padding: 0 30px; 
 
    float: left; 
 
} 
 
.nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: #333; 
 
    height: 35px; 
 
    margin: 0; 
 
    border: solid; 
 
    margin-bottom: 5px; 
 
    margin-top: 5px; 
 
    text-align: center; 
 
    width: 800px; 
 
} 
 
nav ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    text-align: center; 
 
} 
 
nav li { 
 
    display: inline; 
 
} 
 
.btnInicio { 
 
    background-image: url("Imagenes/btnInicio.png"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnInicio:hover { 
 
    background-image: url("Imagenes/btnInicio_Hover.png"); 
 
} 
 
.btnHistoria { 
 
    background-image: url("Imagenes/btnHistoria.png"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnHistoria:hover { 
 
    background-image: url("Imagenes/btnHistoria_Hover.png"); 
 
} 
 
.btnLogros { 
 
    background-image: url("Imagenes/btnLogros.png"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnLogros:hover { 
 
    background-image: url("Imagenes/btnLogros_Hover.png"); 
 
} 
 
.btnAct { 
 
    background-image: url("Imagenes/btnAct.png"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnAct:hover { 
 
    background-image: url("Imagenes/btnAct_Hover.png"); 
 
} 
 
.btnCon { 
 
    background-image: url("Imagenes/btnCon.png"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnCon:hover { 
 
    background-image: url("Imagenes/btnCon_Hover.png"); 
 
} 
 
section.main section.container { 
 
    padding: 10px 360px 20px 30px; 
 
    position: relative; 
 
    overflow: hidden; 
 
    border: solid; 
 
} 
 
section.content { 
 
    float: left; 
 
    width: 100%; 
 
    background-color: #FFFF00; 
 
    border: solid; 
 
} 
 
section.sidebar { 
 
    float: right; 
 
    margin-right: -330px; 
 
    width: 300px; 
 
    background-color: #fff; 
 
    border: solid; 
 
} 
 
section.content:after, 
 
section.sidebar:after { 
 
    display: block; 
 
    position: absolute; 
 
    height: 100%; 
 
    background-color: #fff; 
 
} 
 
section.content:after { 
 
    left: 30px; 
 
    right: 360px; 
 
} 
 
section.sidebar:after { 
 
    right: 30px; 
 
    width: 300px; 
 
} 
 
section.left, 
 
aside.right { 
 
    padding: 20px; 
 
} 
 
footer { 
 
    background-color: #428BCA; 
 
    float: right; 
 
    height: 35px; 
 
    width: 99.85%; 
 
    text-align: center; 
 
    font-size: 30px; 
 
    border: solid; 
 
    margin-top: 5px; 
 
} 
 
@media all and (max-width: 840px) { 
 
    section.main section.container { 
 
    padding: 10px 30px; 
 
    overflow: visible; 
 
    } 
 
    section.content { 
 
    float: none; 
 
    margin-bottom: 30px; 
 
    } 
 
    section.sidebar { 
 
    float: none; 
 
    margin-right: 0; 
 
    width: 100%; 
 
    } 
 
    section.content:after, 
 
    section.sidebar:after { 
 
    display: none 
 
    } 
 
}
<nav> 
 
    <div class="nave"> 
 
    <ul> 
 
     <li class="btnInicio"></li> 
 
     <li class="btnHistoria"></li> 
 
     <li class="btnLogros"></li> 
 
     <li class="btnAct"></li> 
 
     <li class="btnCon"></li> 
 
    </ul> 
 
    </div> 
 

 
</nav> 
 

 
<section class="main"> 
 
    <section class="container"> 
 
    <section class="content"> 
 
     <section class="left"> 
 
     </section> 
 
    </section> 
 
    <section class="sidebar"> 
 
     <aside class="right"> 
 
     <h2>This is a sidebar</h2> 
 
     Sign up to the newsletter! 
 
     </aside> 
 
    </section> 
 
    </section> 
 
</section> 
 

 
<footer> 
 

 
</footer> 
 

 
</body> 
 

 
</html>

代码我已经看到了处理这件事都没有使用背景图片的按钮。如果可能,我想继续使用它们。

+0

我不知道你的HTML结构的其余部分,所以我最好关可以尝试'保证金:0汽车;''在nav' – Franco

+0

它什么都不做。我可以发布我的整个结构。 –

+0

请做。所以我可以看看它。你'css'也需要一些改动,但是当你的代码发布时我可以更好地看到它。 – Franco

回答

0

nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: blue; 
 
} 
 
.nave { 
 
    width: 100%; 
 
    max-width: 1200px; 
 
    margin: 0 auto; 
 
    padding: 0 30px; 
 
    float: left; 
 
} 
 
.nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: #333; 
 
    height: 35px; 
 
    margin: 0; 
 
    border: solid; 
 
    margin-bottom: 5px; 
 
    margin-top: 5px; 
 
    text-align: center; 
 
    width: 800px; 
 
} 
 
nav ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    text-align: center; 
 
} 
 
nav li { 
 
    display: inline; 
 
    background-size: cover; 
 
    background-position: center; 
 
} 
 
.btnInicio { 
 
    background-image: url("http://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnInicio:hover { 
 
    background-image: url("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"); 
 
} 
 
.btnHistoria { 
 
    background-image: url("http://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnHistoria:hover { 
 
    background-image: url("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"); 
 
} 
 
.btnLogros { 
 
    background-image: url("http://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnLogros:hover { 
 
    background-image: url("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"); 
 
} 
 
.btnAct { 
 
    background-image: url("http://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnAct:hover { 
 
    background-image: url("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"); 
 
} 
 
.btnCon { 
 
    background-image: url("http://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg"); 
 
    background-repeat: no-repeat; 
 
    float: left; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
.btnCon:hover { 
 
    background-image: url("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"); 
 
} 
 
section.main section.container { 
 
    padding: 10px 360px 20px 30px; 
 
    position: relative; 
 
    overflow: hidden; 
 
    border: solid; 
 
} 
 
section.content { 
 
    float: left; 
 
    width: 100%; 
 
    background-color: #FFFF00; 
 
    border: solid; 
 
} 
 
section.sidebar { 
 
    float: right; 
 
    margin-right: -330px; 
 
    width: 300px; 
 
    background-color: #fff; 
 
    border: solid; 
 
} 
 
section.content:after, 
 
section.sidebar:after { 
 
    display: block; 
 
    position: absolute; 
 
    height: 100%; 
 
    background-color: #fff; 
 
} 
 
section.content:after { 
 
    left: 30px; 
 
    right: 360px; 
 
} 
 
section.sidebar:after { 
 
    right: 30px; 
 
    width: 300px; 
 
} 
 
section.left, 
 
aside.right { 
 
    padding: 20px; 
 
} 
 
footer { 
 
    background-color: #428BCA; 
 
    float: right; 
 
    height: 35px; 
 
    width: 99.85%; 
 
    text-align: center; 
 
    font-size: 30px; 
 
    border: solid; 
 
    margin-top: 5px; 
 
} 
 
@media all and (max-width: 840px) { 
 
    section.main section.container { 
 
    padding: 10px 30px; 
 
    overflow: visible; 
 
    } 
 
    section.content { 
 
    float: none; 
 
    margin-bottom: 30px; 
 
    } 
 
    section.sidebar { 
 
    float: none; 
 
    margin-right: 0; 
 
    width: 100%; 
 
    } 
 
    section.content:after, 
 
    section.sidebar:after { 
 
    display: none 
 
    } 
 
}
<nav> 
 
    <div class="nave"> 
 
    <ul> 
 
     <li class="btnInicio"></li> 
 
     <li class="btnHistoria"></li> 
 
     <li class="btnLogros"></li> 
 
     <li class="btnAct"></li> 
 
     <li class="btnCon"></li> 
 
    </ul> 
 
    </div> 
 

 
</nav> 
 

 
<section class="main"> 
 
    <section class="container"> 
 
    <section class="content"> 
 
     <section class="left"> 
 
     </section> 
 
    </section> 
 
    <section class="sidebar"> 
 
     <aside class="right"> 
 
     <h2>This is a sidebar</h2> Sign up to the newsletter! 
 
     </aside> 
 
    </section> 
 
    </section> 
 
</section>

我已经在你的CSS添加background-size: cover;background-position: center;nav li(也把一些猫展现真实图像)。

顺便说一句,你的布局是相当混乱的移动大小。

我会创建一个通用的.button或.nav-button类来添加这些背景属性,而不是将它们添加到nav li。然后你可以将这些重复的样式(浮点,宽度,高度)移动到这个新类中。

+0

谢谢,它工作。我也从按钮类中取出了float:left。 我不熟悉创建按钮,因此我采用了这种方法。 –

+0

其实我的解决方案与'按钮'无关 - 它只是使用背景属性。背景大小:封面将填充任何图像(不关心图像大于或小于容器),而背景位置:中心确保图像居中。 PS - 如果有帮助,请接受并提出答案 – mizurnix

+0

悬停时怎么样?它应该改变图像。 –

0

LiveFiddle更好地在导航li上使用displa:inline-flex,并且还可以从按钮背景部分和.nav中删除那些float:left。同样从.nave中删除填充。祝你好运 [N.B:我不使用图像这里只是背景颜色,例如你可以用你的按钮url来代替它]。

nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: blue; 
 
} 
 
    
 
.nave { 
 
    width: 100%; 
 
    max-width: 1200px; 
 
    margin: 0 auto; 
 
    
 
} 
 
    
 
.nav { 
 
    padding: 10px 0 25px 0; 
 
    background-color: #333; 
 
    height: 35px; 
 
    margin: 0; 
 
    border: solid; 
 
    margin-top: 5px; 
 
    width:800px; 
 
} 
 
    
 
nav ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    text-align: center; 
 
} 
 
    
 
nav li { 
 
    display: inline-flex; 
 
} 
 
    
 
.btnInicio { 
 
    background: #fff; 
 
    background-repeat: no-repeat; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
    
 
.btnInicio:hover { 
 
    background: #bbb; 
 
} 
 
    
 
.btnHistoria { 
 
    background: red; 
 
    background-repeat: no-repeat; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
    
 
.btnHistoria:hover { 
 
    background: #000; 
 
} 
 
    
 
.btnLogros { 
 
    background: yellow; 
 
    background-repeat: no-repeat; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
    
 
.btnLogros:hover { 
 
    background: #fff; 
 
} 
 
    
 
.btnAct { 
 
    background: green; 
 
    background-repeat: no-repeat; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
    
 
.btnAct:hover { 
 
    background: lightblue; 
 
} 
 
    
 
.btnCon { 
 
    background: #000; 
 
    background-repeat: no-repeat; 
 
    height: 56px; 
 
    width: 138px; 
 
} 
 
    
 
.btnCon:hover { 
 
    background: #bbb; 
 
} 
 
    
 
section.main section.container { 
 
    padding: 10px 360px 20px 30px; 
 
    position: relative; 
 
    overflow: hidden; 
 
    border: solid; 
 
} 
 
    
 
section.content { 
 
    float: left; 
 
    width: 100%; 
 
    background-color: #FFFF00; 
 
    border: solid; 
 
} 
 
    
 
section.sidebar { 
 
    float: right; 
 
    margin-right: -330px; 
 
    width: 300px; 
 
    background-color: #fff; 
 
    border: solid; 
 
} 
 
    
 
section.content:after, 
 
section.sidebar:after { 
 
    display: block; 
 
    position: absolute; 
 
    height: 100%; 
 
    background-color: #fff; 
 
} 
 
    
 
section.content:after { 
 
    left: 30px; 
 
    right: 360px; 
 
} 
 
    
 
section.sidebar:after { 
 
    right: 30px; 
 
    width: 300px; 
 
} 
 
    
 
section.left, 
 
aside.right { 
 
    padding: 20px; 
 
} 
 
    
 
footer { 
 
    background-color: #428BCA; 
 
    float: right; 
 
    height: 35px; 
 
    width: 99.85%; 
 
    text-align: center; 
 
    font-size: 30px; 
 
    border: solid; 
 
    margin-top: 5px; 
 
} 
 
    
 
@media all and (max-width: 840px) { 
 
    section.main section.container { 
 
    padding: 10px 30px; 
 
    overflow: visible; 
 
    } 
 
    section.content { 
 
    float: none; 
 
    margin-bottom: 30px; 
 
    } 
 
    section.sidebar { 
 
    float: none; 
 
    margin-right: 0; 
 
    width: 100%; 
 
    } 
 
    section.content:after, 
 
    section.sidebar:after { 
 
    display: none 
 
    } 
 
}
<nav> 
 
    <div class="nave"> 
 
<ul> 
 
    <li class="btnInicio"></li> 
 
    <li class="btnHistoria"></li> 
 
    <li class="btnLogros"></li> 
 
    <li class="btnAct"></li> 
 
    <li class="btnCon"></li> 
 
</ul> 
 
    </div> 
 

 
</nav> 
 

 
<section class="main"> 
 
    <section class="container"> 
 
<section class="content"> 
 
    <section class="left"> 
 
    </section> 
 
</section> 
 
<section class="sidebar"> 
 
    <aside class="right"> 
 
    <h2>This is a sidebar</h2> Sign up to the newsletter! 
 
    </aside> 
 
</section> 
 
    </section> 
 
</section> 
 

 
<footer> 
 

 
</footer>