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

时间:2016-12-20 16:27:20

标签: html css css3 vertical-alignment

我有一些导航链接,我想要对齐标题的右下角。内容是

          <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/。我错过了什么?

3 个答案:

答案 0 :(得分:1)

使用 CSS Flexbox 。的 Updated 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;
}

查看下面的工作代码段:

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

希望这有帮助!

答案 1 :(得分:0)

您可以使用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语法所需的前缀;)

答案 2 :(得分:0)

尝试使用此代码:

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

将40px替换为标题的高度。在某些情况下,这会强制垂直对齐。