灵活的盒子,导航菜单和响应

时间:2014-01-12 14:47:18

标签: html css flexbox

我有一个导航菜单,可以调整特定窗口大小的大小。工作得很好,但我有一个问题,导航列表居中在它的父div,但同一div中的徽标没有。以下是代码:

HTML

<nav id="top_menu">
    <div id="logo"> <img src="images/logo.svg"> </div>
    <ul class="navigation">
        <li data-slide="1">Home</li>
        <li data-slide="2">Skills</li>
        <li data-slide="3">Over mij</li>
        <li data-slide="4">Portfolio</li>
        <li data-slide="5">Contact</li>
    </ul>
</nav>

CSS(Sass with Bourbon mixins)

// NAVIGATION

#top_menu {
@include display(flex);
@include flex-flow(row wrap);
@include justify-content(flex-start);
@include align-content(center);
position: fixed;
width: 100%;
top: 48px;
left: 0;
background-color: white;
height: 80px;
z-index: 1;

@media screen and (max-width:960px) {
    height: 60px;
    top: 36px;
}

@media screen and (max-width:640px) {
    top: 0;
    height: auto;
    @include flex-flow(column wrap);
    @include justify-content(center);
    @include align-content(center);
}
}

#logo {
display: block;
margin-left: 3%;
width: 12%;
max-width: 150px;
min-width: 100px;

@media screen and (max-width:640px) {
    margin: 0;
    width: 100%;
}

img {
    position: relative;
    width: 100%;
    max-width: 150px;
    min-width: 100px;
}

}

.navigation{
width: 82%;
list-style: none;
margin: 0;

@include display(flex);
@include flex-flow(row wrap);
@include justify-content(flex-start);
@include align-content(center);

//@media all and (max-width:800px) {
//  @include justify-content(space-around);
//}

@media all and (max-width:960px) {
    width: 75%;
    @include justify-content(space-around);
}

@media all and (max-width:640px) {
    width: auto;
    @include flex-flow(column wrap);
    padding: 0;
}

li {
color: black;
text-decoration: none;
display: block;
margin: 0 20px 0 20px;
padding: 10px;

font-family: 'PT Sans', sans-serif;
font-size: 20px;
text-transform: uppercase;
line-height: 40px;

@media all and (max-width:960px) {
    margin: 0;
    font-size: 18px;
    line-height: 30px;
}

@media all and (max-width:640px) {
    text-align: center;
    padding: 10px;
}

    &:hover,&.active {
        box-shadow: 0px 10px 0px 0px rgba(245, 86, 78, 1);
        color: #333;
        font-weight: bold;
        cursor:pointer;

        @media screen and (max-width: 960px) {
            box-shadow: 0px 6px 0px 0px rgba(245, 86, 78, 1);
        }
    }
}
}

到目前为止我发现的是它并没有真正把两个div放在彼此之下;将导航的宽度更改为更低或更高的位置会更改徽标的位置。然而,我无法找到如何解决这个问题,并将徽标粘贴在导航上方的中心位置。我喜欢弹性盒子,但有时候有点难以理解。我在这里做错了什么?

您可以在此处查看问题:http://de-kleijn.com/test/ndk/

2 个答案:

答案 0 :(得分:2)

问题是当您需要使用align-items时,您正在使用align-content属性。 Align-items用于控制项目在同一行上的对齐方式。 Align-content用于控制行相对于其他行的对齐方式。

http://codepen.io/cimmanon/pen/yAIKs

.navigation{
  width: 82%;
  list-style: none;
  margin: 0;
  border: 1px solid;

  @include display-flex;
  @include flex-flow(row wrap);
  @include justify-content(flex-start);
  @include align-items(center);
}

答案 1 :(得分:0)

flex-box很安静但不是那么坚固,我会使用来自IE8的display:table。  这是一个例子,修改了图像和li字体大小的行为。 http://codepen.io/anon/pen/fktCL

#top_menu {
  display:table;
  width:100%;
  text-align:center;
  background:gray linear-gradient(to bottom, #555 , #eee);
  padding:5px;
  box-shadow:0 0 5px;
}
#logo , .navigation{
  display:table-cell;
  vertical-align:middle;
  margin:0;
  padding:0;
  text-align:justify;
  font-size: 20px;
  line-height:0;
  width:18%;
}
.navigation {
  width:82%;
}
.navigation li {
  display:inline-block;
  color: black;
  text-decoration: none;
  margin: 0 20px 0 20px;
  padding: 10px;
  font-family: 'PT Sans', sans-serif;
  font-size: 1em;
  text-transform: uppercase;
  line-height: 2em;
}
.navigation:after {
  content:'';
  display:inline-block;
  width:100%;
  vertical-align:top;
}
img {
  width:100%;
  min-height:100px;
  max-height:250px;;
  max-width: 250px;
  min-width: 100px;
  vertical-align:top;
  display:block;
  margin: 2px auto;
}
@media all and (max-width:960px) {
  #logo , .navigation
  {
    display:block;
    font-size:0.8em;
    width:100%;
  }
  #logo img {
    width:100px;}
}

更新代码以调整图像大小,直到菜单下拉