2016-10-07 31 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>