html - 无法使柔性端正常工作

标签 html css flexbox

考虑基本的引导导航栏标记:

 <div class="navbar-header">
    <div class="navbar-brand-left">
        <div class="brand-img">Logo here</div>
        <div class="brand-logos">more stuff here</div>
    </div>
    <div class="navbar-brand-right">
        <div id="phone1">phone number 123</div>
        <div id="phone2">phone number 123</div>
    </div>

在这里,我希望 brand-imgbrand logo 保持在左侧,phone1phone2 保持在右侧彼此相邻。 为此,我将 display:flex 应用于主容器 navbar,同时也应用于左侧和右侧的两个容器 navbar-brand-leftnavbar-brand-right 以便它们内部的元素也会受到影响。

我得到的是所有元素都分散开来。

这是CSS:

     .navbar-header{
    display:flex; 
    align-items:center; 
    justify-content:space-between;
    flex-direction: row;
    }

    .navbar-brand-left{
    flex: 1;
    height:42px;
    display:flex;
    align-self:flex-start;
    align-items:center;
    justify-content:space-between;
    flex-direction: row;
    }

    .brand-img{
    flex:1;
    }

   .brand-logos{
    flex:1; 
    margin-left:1em;
    }

    .navbar-brand-right{
    font-size:1.2em;
    height:42px;
    flex:1;
    display:flex;
    align-items:center;
    justify-content:flex-end;
    flex-direction: row;
    align-self:flex-end;
    }

    #phone1{
    flex:1;
    text-align:right;
    justify-content:flex-end;
    } 

    #phone2{
    flex:1;
    text-align:right;
    justify-content:flex-end;
    }

我添加了比我需要的更多的 flex 指令,因为我正在努力使这项工作正常进行。我知道理论上它应该用更少的钱工作。

这是jsFiddle .

如您所见,元素不会停留在末尾或开头,而是倾向于导航到中间

space- Between flex-end 就像它们不存在一样?它们可以被完全删除。

最佳答案

您可能必须这样做:

  1. navbar-brand-left更改为justify-content: flex-start

  2. brand-imgbrand-logosphone-1 中删除 flex: 1phone-2

请参阅下面的演示:

.navbar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-direction: row;
}
.navbar-brand-left {
  flex: 1;
  height: 42px;
  display: flex;
  align-self: flex-start;
  align-items: center;
  justify-content: flex-start;/*CHANGED*/
  flex-direction: row;
}
.brand-img {
  /*flex: 1;*/
}
.brand-logos {
  /*flex: 1;*/
  margin-left: 1em;
}
.navbar-brand-right {
  font-size: 1.2em;
  height: 42px;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-direction: row;
  align-self: flex-end;
}
#phone1 {
  /*flex: 1;*/
  text-align: right;
  justify-content: flex-end;
}
#phone2 {
  /*flex: 1;*/
  text-align: right;
  justify-content: flex-end;
}
<div class="navbar-header">
  <div class="navbar-brand-left">
    <div class="brand-img">Logo here</div>
    <div class="brand-logos">more stuff here</div>
  </div>
  <div class="navbar-brand-right">
    <div id="phone1">phone number 123</div>
    <div id="phone2">phone number 123</div>
  </div>
</div>

关于html - 无法使柔性端正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41609850/

相关文章:

html - 拉伸(stretch) content-div 的高度,使 vertical-navi 也被拉伸(stretch)

jquery - IE7 中的自定义(ish)jQuery Accordion 菜单错误

html - Bootstrap Flex 使 div 溢出

javascript - 当所选行是最后一行的前一行时自动滚动表格

javascript - <li> html 条目内的复选框不可点击 - 与用于折叠/展开列表项的 javascript 结合使用

javascript - 停用上一个命令

css - 无法移除渐变

css - 使用 flexbox 并保持纵横比,即使内容大小不同

javascript - flex 元素是否有可能与它们上方的元素紧密对齐?

java - 如何在java中将通知菜单与数据库连接