无法让 DIV 内容与 DIV 的底部对齐

Having trouble getting DIV contents to align to the bottom of the DIV

我有一些导航链接想要对齐到 header 的右侧和底部。内容是

          <ul class="nav navbar-nav navbar-right">
            <li><a href="/dashboard">My Subscriptions</a></li>
            <li><a href="#">Help</a></li>
            <li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
          </ul>

我应用的样式是

header ul {
  float: right;
  list-style: none;
  margin-right: 15px;
  vertical-align: text-bottom;
}

虽然这些排列在右边没问题,但它们没有与 div — https://jsfiddle.net/cujz8tye/ 的底部对齐。我错过了什么?

您可以使用 Flexbox 以更简单的方式垂直对齐项目。

这是一个updated Fiddle

相关修改:列表不再浮动并且:

.container {
  display: flex;
    justify-content: space-between; /* The 2 flex items a#logo and nav will be aligned leftmost and rightmost */
  height: 100%; /* flex container will occupy the full height of its parent (73px) */
}
.container > nav {
  align-self: flex-end; /* this flex item (nav children of .container, not ul directly) will align itself vertically at the bottom. */
                        /*  Because the flex container is by default flex-direction: row thus align-self is for the other axis, vertical */
}

编辑:使用 Autoprefixer(如果还没有)与 IE10+ 兼容。它将负责为您添加所有 3 个版本的 flexbox 语法所需的前缀;)

使用CSS FlexboxUpdated Fiddle.

将容器内的所有东西都包裹在 .content-holder 下,例如:

<div class="content-holder">
  <a id="logo" href="#">sample app</a>
  <nav>
    <ul class="nav navbar-nav navbar-right">
      <li><a href="/dashboard">My Subscriptions</a></li>
      <li><a href="#">Help</a></li>
      <li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
    </ul>
  </nav>
</div>

然后,应用 CSS:

.content-holder {
  display: flex;
  height: 60px;
  align-items: center;
}

nav {
  flex: 1;
  align-self: flex-end;
}

nav ul {
  margin-top: 0;
  margin-bottom: 0;
}

看看下面的工作片段:

header {
  box-shadow: none;
  background: #44505d;
  border-bottom: none;
  height: 73px;
}

#branding {
  height: 60px;
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 100001;
}

.content-holder {
  display: flex;
  height: 60px;
  align-items: center;
}

#logo {
  font-size: 1.7em;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: -1px;
  font-weight: bold;
}

nav {
  flex: 1;
  align-self: flex-end;
}

#logo:hover {
  color: #fff;
  text-decoration: none;
}    

header ul {
  float: right;
  list-style: none;
  margin: 0 15px 0 0;
}

header ul li {
  float: left;
  margin-left: 15px;
}

body {
  background: #eee;
  padding-top: 73px;
  line-height: 1.4;
  font-family: "Karla", "Helvetica Neue", Arial, Helvetica, sans-serif;
  color: #333;
  font-size: 14px;
  margin: 0;
}

a, .clickable {
  color: #53a3c2;
  text-decoration: none;
}
<header id="branding" class="clearFix">
  <div class="container clearFix">
    <div class="content-holder">
      <a id="logo" href="#">sample app</a>
      <nav>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="/dashboard">My Subscriptions</a></li>
          <li><a href="#">Help</a></li>
          <li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
        </ul>
      </nav>
    </div>
  </div>
</header>

希望对您有所帮助!

尝试使用此代码:

header {
    height:40px;
    line-height: 40px;
}

将 40px 替换为 header 的高度。这会强制 vertical-align 在某些情况下起作用。