2015-11-01 66 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”。 –