我们现在想做一个头部的导航栏,又想用fixed把它固定在上方,又想用弹性盒布局去设置它内部的样式,可是发现其中的弹性盒布局已经失效了。 看下面例子

html部分:

<ul>
    <li>协会简介</li>
    <li>协会章程</li>
    <li>协会架构</li>
    <li>资料下载</li>
</ul>

css部分

ul {
	position: fixed;
  	display: flex;
    justify-content: space-between;
    margin: 0 15px;
    background: pink;
}
li {
    flex: 1;
    list-style: none;
    height: 100px;
    line-height: 100px;
    text-align: center;
    font-size:  30px;
    border: 1px solid #fff;
}

效果如下

发现弹性盒布局已经失效 ,那么该怎么做呢?

其实很简单,只需要在ul的外面再套一个盒子就可以了。然后外层盒子使用定位,内层盒子正常使用弹性盒布局 ,如下:

html部分:

<div class="nav-box">    
    <ul>
        <li>协会简介</li>
        <li>协会章程</li>
        <li>协会架构</li>
        <li>资料下载</li>
    </ul>
</div>

css部分:

.nav-box {
    width: 100%; //记得加宽度
    position: fixed; 
}
ul {
    display: flex;
    justify-content: space-between;
    margin: 0 15px;
    background: pink;
}
li {
    flex: 1;
    list-style: none;
    height: 100px;
    line-height: 100px;
    text-align: center;
    font-size:  30px;
    border: 1px solid #fff;
}

效果如下

 posted on 2021-02-15 21:29  kly99  阅读(64)  评论(0)    收藏  举报