2015-11-01 74 views
0

我找不到一種方式來正確地對齊浮動元素中的一個按鈕。css中的棘手的水平中心:浮動元素只有

button { 
 
    float:left; 
 
} 
 

 

 
header { 
 
    overflow: hidden; 
 
    background: #222; 
 
} 
 

 
header a, header label { 
 
    display: block; 
 
    padding: 20px; 
 
    color: #fff; 
 
    text-decoration: none; 
 
    line-height: 20px; 
 
} 
 

 
header a:hover, header label:hover { color: #aaa; } 
 

 
header label { 
 
    float: right; 
 
    padding: 18px 20px; 
 
    cursor: pointer; 
 
} 
 

 
header label:after { 
 
    content: "\2261"; 
 
    font-size: 1.8em; 
 
} 
 

 
.logo { 
 
    float: left; 
 
    font-weight: bold; 
 
    font-size: 1.5em; 
 
} 
 
    
 
nav { 
 
    float: right; 
 
    max-height: 0; 
 
    width: 100%; 
 
    -webkit-transition: max-height 0.3s; 
 
    -moz-transition: max-height 0.3s; 
 
     -o-transition: max-height 0.3s; 
 
      transition: max-height 0.3s; 
 
} 
 

 
nav ul { 
 
    margin: 0; 
 
    padding: 0; 
 
    padding-bottom: 10px; 
 
} 
 
    
 
nav li { 
 
    display: block; 
 
    text-align: center; 
 
} 
 
    
 
nav a { 
 
    padding: 10px; 
 
    width: 100%; 
 
} 
 

 
#nav { display: none; } 
 

 
#nav:checked ~ nav { 
 
    max-height: 200px; /* This can be anything bigger than your nav height. The transition duration works with this */ 
 
} 
 

 
@media only screen and (min-width: 700px) { 
 
    
 
    header label { display: none; } 
 
    
 
    nav { 
 
    width: auto; 
 
    max-height: none; 
 
    } 
 
    
 
    nav ul { 
 
    padding: 0; 
 
    padding-right: 10px; 
 
    } 
 
    
 
    nav li { 
 
    display: inline-block; 
 
    text-align: left; 
 
    } 
 
    
 
    header nav a { 
 
    display: inline-block; 
 
    padding: 20px 10px; 
 
    width: auto; 
 
    } 
 
    
 
}
<header> 
 

 
    <a class="logo" href="http://minimaldev.com">Minimal Menu</a> 
 
    
 
    <input id="nav" type="checkbox"> 
 
    <label for="nav"></label> 
 
    
 
    <button>centered button ?</button> 
 
    
 
    <nav> 
 
    <ul> 
 
     <li><a href="#">One</a></li> 
 
     <li><a href="#">Two</a></li> 
 
     <li><a href="#">Three</a></li> 
 
     <li><a href="#">Four</a></li> 
 
    </ul> 
 
    </nav> 
 

 
</header>

見例子也codepen

我試圖顯示錶伎倆,但它打破了小設備(響應)導航欄行爲。

我也試過this technique,但我有同樣的問題。

有什麼想法?我想過使用calc(),但我找不到合適的公式。

+0

你真的需要的'浮動:對'button' left'?你有沒有嘗試'轉換'技巧? – Harry

+0

老實說,我沒有看到你想把按鈕居中的地方。 –

回答

2

我做了幾個修改的:

  • 加入text-align: center到撞着鍵關閉用手動像素調整頂部邊緣按鈕
  • 的父元素(假設設計在標誌方面保持剛性大小,因爲字體和填充大小的非響應性質,它不是一個響應式解決方案)。

這是在CodePen

HTML:

<header> 

    <a class="logo" href="http://minimaldev.com">Minimal Menu</a> 

    <input id="nav" type="checkbox"> 
    <label for="nav"></label> 

    <button>centered button</button> 

    <nav> 
    <ul> 
     <li><a href="#">One</a></li> 
     <li><a href="#">Two</a></li> 
     <li><a href="#">Three</a></li> 
     <li><a href="#">Four</a></li> 
    </ul> 
    </nav> 

</header> 

CSS:

button{ 
    margin-top: 18px; 
} 

header { 
    text-align: center; 
    overflow: hidden; 
    background: #222; 
} 

header a, header label { 
    display: block; 
    padding: 20px; 
    color: #fff; 
    text-decoration: none; 
    line-height: 20px; 
} 

header a:hover, header label:hover { color: #aaa; } 

header label { 
    float: right; 
    padding: 18px 20px; 
    cursor: pointer; 
} 

header label:after { 
    content: "\2261"; 
    font-size: 1.8em; 
} 

.logo { 
    float: left; 
    font-weight: bold; 
    font-size: 1.5em; 
} 

nav { 
    float: right; 
    max-height: 0; 
    width: 100%; 
    -webkit-transition: max-height 0.3s; 
    -moz-transition: max-height 0.3s; 
     -o-transition: max-height 0.3s; 
      transition: max-height 0.3s; 
} 

nav ul { 
    margin: 0; 
    padding: 0; 
    padding-bottom: 10px; 
} 

nav li { 
    display: block; 
    text-align: center; 
} 

nav a { 
    padding: 10px; 
    width: 100%; 
} 

#nav { display: none; } 

#nav:checked ~ nav { 
    max-height: 200px; /* This can be anything bigger than your nav height. The transition duration works with this */ 
} 

@media only screen and (min-width: 700px) { 

    header label { display: none; } 

    nav { 
    width: auto; 
    max-height: none; 
    } 

    nav ul { 
    padding: 0; 
    padding-right: 10px; 
    } 

    nav li { 
    display: inline-block; 
    text-align: left; 
    } 

    header nav a { 
    display: inline-block; 
    padding: 20px 10px; 
    width: auto; 
    } 

} 
+1

如果它也正確地處理了漢堡包,這個答案會好得多... –

+1

不錯的努力,在響應模式上仍然有點不妥:點擊漢堡時按鈕會移動。 +1爲好的嘗試。 – bdavidxyz

+0

我改變了垂直對齊使其相對於標題的頂部,所以在漢堡菜單的情況下按鈕不會跳下來。否則,它仍然只是水平地執行技巧的父級中的「text-align:center」。 –