如何将div放在容器的底部?

Dón*_*nal 707 html css

给出以下HTML:

<div id="container">
  <!-- Other elements here -->
  <div id="copyright">
    Copyright Foo web designs
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我想#copyright坚持到底#container.

我可以不使用绝对定位来实现这一目标吗 如果float属性支持'bottom'的值,那么似乎可以做到这一点,但不幸的是,它没有.

Use*_*ser 876

可能不是.

分配position:relative#container,然后分配position:absolute; bottom:0;#copyright.


#container {
    position: relative;
}
#copyright {
    position: absolute;
    bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container">
  <!-- Other elements here -->
  <div id="copyright">
    Copyright Foo web designs
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 一个绝对定位的元素寻找它的绝对或相对位置的最近父母,以确定它的位置.如果全部失败则转向身体(窗口).因此父母需要相对. (6认同)
  • 如果您的容器上的position:relative会破坏您的设计,请记住您可以在容器内部包含100%高度的包装div,并添加相对于该位置的位置: (5认同)
  • 下面的 Flexbox 方法要好得多。 (4认同)
  • 那还差得远。它位于最新浏览器可见屏幕的底部,但我不知道哪个浏览器会位于容器 div 的底部。 (3认同)
  • 在#container中添加margin-bottom以防止页面内容的版权 (2认同)

小智 337

实际上,@ User接受的答案只有在窗口很高且内容很短的情况下才有效.但是,如果内容是高大的窗口很短,它将把版权信息在页面上的内容,然后向下滚动看到的内容将让你有一个浮动的版权声明.这使得这个解决方案对大多数页面都没用(实际上就像这个页面一样).

最常见的方法是演示"CSS粘性页脚"方法,或略微变细.这种方法效果很好 - 如果你有一个固定的高度页脚.

如果你需要一个可变高度的页脚,如果内容太短,它将出现在窗口的底部,如果窗口太短,你需要在内容的底部,你会怎么做?

吞下你的骄傲,并使用一张桌子.

例如:

* {
    padding: 0;
    margin: 0;
}

html, body {
    height: 100%;
}

#container {
    height: 100%;
    border-collapse: collapse;
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<body>
    <table id="container">
        <tr>
            <td valign="top">
                <div id="main">Lorem ipsum, etc.</div>
            </td>
        </tr>
        <tr>
            <td valign="bottom">
                <div id="footer">Copyright some evil company...</div>
            </td>
        </tr>
    </table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

试试看.这适用于任何窗口大小,任何数量的内容,任何大小的页脚,在每个浏览器...甚至IE6.

如果您想要使用表格进行布局,请花一点时间问自己为什么.CSS本来应该让我们的生活变得更轻松 - 而且整体而言 - 但事实是,即使经过这么多年,它仍然是一个破碎的,反直觉的混乱.它无法解决所有问题.它不完整.

表格并不酷,但至少目前,它们有时是解决设计问题的最佳方法.

  • 似乎对我来说是最好的答案.此外,您始终可以使用"css tables"(display:table [-row/-cell])而不是html表.这提供了相同的布局而没有语义. (78认同)
  • 骄傲是无关紧要的.由于可访问性原因,不应将表用于布局.如果您曾经使用过屏幕阅读器,您就会知道为什么表格只应用于表格数据.使用css表作为@chiccodoro状态. (55认同)
  • 阅读这些评论的其他人:这与"骄傲"或"酷"无关.使用表格进行布局实际上更加痛苦,特别是对于大量内容.确保你不会错过td并在添加内容时筛选如此多的无关标记是地狱.这是一个痛苦的原因我们正在创建HTML*文档*而不仅仅是布局,如果我们文档的大多数内容不是表格数据,那么为什么我们将它放在表格中呢?如果我们不喜欢以他们的方式使用网络技术,那么为什么要使用它们呢?使用桌面/移动应用程序来发布内容 (18认同)
  • 使用表格进行布局并不是 Web 最佳实践。HTML 用于语义定义,而不是样式。表格用于表格数据。布局和样式最好留给CSS。 (3认同)
  • 如果您有一个 PHP 脚本/其他脚本来生成您的页面,您可以复制页脚,使用一个作为隐藏的“间隔符”,另一个使用绝对定位。间隔页脚可确保无论页脚中有多少内容,它都不会出现在页面上。 (2认同)

Jos*_*ier 145

flexbox方法!

支持的浏览器中,您可以使用以下内容:

这里的例子

.parent {
  display: flex;
  flex-direction: column;
}
.child {
  margin-top: auto;
}
Run Code Online (Sandbox Code Playgroud)

.parent {
  height: 100px;
  border: 5px solid #000;
  display: flex;
  flex-direction: column;
}
.child {
  height: 40px;
  width: 100%;
  background: #f00;
  margin-top: auto;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <div class="child">Align to the bottom</div>
</div>
Run Code Online (Sandbox Code Playgroud)


上面的解决方案可能更灵活,但是,这里有一个替代解决方案:

这里的例子

.parent {
  display: flex;
}
.child {
  align-self: flex-end;
}
Run Code Online (Sandbox Code Playgroud)

.parent {
  height: 100px;
  border: 5px solid #000;
  display: flex;
}
.child {
  height: 40px;
  width: 100%;
  background: #f00;
  align-self: flex-end;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <div class="child">Align to the bottom</div>
</div>
Run Code Online (Sandbox Code Playgroud)


作为旁注,您可能需要添加供应商前缀以获得其他支持.

  • 父级上的“ column-reverse”怎么样,所以您根本不需要为子级添加任何内容? (2认同)

Has*_*own 111

是的,你可以做到这一点,没有absolute定位,也没有使用tables(带有标记的螺丝等).

演示
经过测试,可以在IE> 7,chrome,FF上进行测试,这是一个非常容易添加到现有布局的东西.

<div id="container">
    Some content you don't want affected by the "bottom floating" div
    <div>supports not just text</div>

    <div class="foot">
        Some other content you want kept to the bottom
        <div>this is in a div</div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)
#container {
    height:100%;
    border-collapse:collapse;
    display : table;
}

.foot {
    display : table-row;
    vertical-align : bottom;
    height : 1px;
}
Run Code Online (Sandbox Code Playgroud)

float:bottom即使考虑到@Rick Reilly的回答中指出的问题,它也能有效地做到这一点!

  • 你也可以使用flexbox - http://stackoverflow.com/questions/526035/html-css-positioning-float-bottom/27812717#27812717 (3认同)
  • 好的!请注意,IIRC 您需要设置 `&lt;!DOCTYPE&gt;` 才能使其在 IE 上运行(至少 &lt;= 8);这些旧版本仅支持“标准”模式下的“display:table-XXX”。但标准模式也会破坏许多为 IE6 等设计的页面。 (2认同)
  • 我发现`.foot {display:table-footer-group}`有更多的运气.我不需要`vertical-align`,`height`或`border-collapse`. (2认同)

avr*_*ool 19

这是一个古老的问题,但这是一种独特的方法,可以在几种情况下提供帮助.

纯CSS,无绝对定位,无需修复任何高度,跨浏览器(IE9 +)

看看工作小提琴

因为正常的流量是"从上到下",我们不能简单地要求#copyrightdiv坚持他父母的底部而没有绝对的某种定位,但是如果我们想让#copyrightdiv坚持他父母的顶部,那么将非常简单 - 因为这是正常的流动方式.

所以我们将利用这个优势.我们将改变divHTML中s 的顺序,现在#copyrightdiv位于顶部,内容立即跟随它.我们还使内容div一直延伸(使用伪元素和清除技术)

现在只是在视图中反转该顺序的问题.这可以通过CSS转换轻松完成.

我们将容器旋转180度,现在:向上 - 向下.(我们将内容反转回来看起来正常)

如果我们想在内容区域中有一个scroolbar,我们需要应用更多的CSS魔法.可以显示在这里 [在那个例子中,内容低于标题 - 但它的想法相同]

* {
  margin: 0;
  padding: 0;
}

html,
body,
#Container {
  height: 100%;
  color: white;
}

#Container:before {
  content: '';
  height: 100%;
  float: left;
}

#Copyright {
  background-color: green;
}

#Stretch {
  background-color: blue;
}

#Stretch:after {
  content: '';
  display: block;
  clear: both;
}

#Container,
#Container>div {
  -moz-transform: rotateX(180deg);
  -ms-transform: rotateX(180deg);
  -o-transform: rotate(180deg);
  -webkit-transform: rotateX(180deg);
  transform: rotateX(180deg);
}
Run Code Online (Sandbox Code Playgroud)
<div id="Container">
  <div id="Copyright">
    Copyright Foo web designs
  </div>
  <div id="Stretch">
    <!-- Other elements here -->
    <div>Element 1</div>
    <div>Element 2</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 请不要,看看现在的渲染效果如何https://i.stack.imgur.com/ZP9Hw.png (2认同)

Man*_*mar 12

CSS 网格

由于 CSS Grid 的使用越来越多,我想建议align-self使用网格容器内的元素。

align-self可以包含任何值:end, self-end,flex-end对于以下示例。

#parent {
  display: grid;
}

#child1 {
  align-self: end;
}

/* Extra Styling for Snippet */

#parent {
  height: 150px;
  background: #5548B0;
  color: #fff;
  padding: 10px;
  font-family: sans-serif;
}

#child1 {
  height: 50px;
  width: 50px;
  background: #6A67CE;
  text-align: center;
  vertical-align: middle;
  line-height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="parent">
  <!-- Other elements here -->
  <div id="child1">
    1
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)


Gar*_*een 6

试试这个;

<div id="container">
  <div style="height: 100%; border:1px solid #ff0000;">
  <!-- Other elements here -->
  </div>
</div>
<div id="copyright" style="position:relative;border:1px solid #00ff00;top:-25px">
   Copyright Foo web designs
</div>
Run Code Online (Sandbox Code Playgroud)


Ráp*_*rás 6

div为上面的元素创建另一个容器#copyright.在版权之上添加一个新的div: <div style="clear:both;"></div> 它将强制页脚在其他所有内容之下,就像使用相对定位(bottom:0px;)的情况一样.


jac*_*kno 5

你可以确实align的方框bottom不使用position:absolute,如果你知道height#container使用文本对齐的特征inline-block元素。

在这里你可以看到它的实际效果。

这是代码:

#container {
    /* So the #container most have a fixed height */
    height: 300px;
    line-height: 300px;
    background:Red;
}

#container > * {
    /* Restore Line height to Normal */
    line-height: 1.2em;
}

#copyright {
    display:inline-block;
    vertical-align:bottom;
    width:100%; /* Let it be a block */
    background:green;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

CodePen 链接在这里

html, body {
    width: 100%;
    height: 100%;
}

.overlay {
    min-height: 100%;
    position: relative;
}

.container {
    width: 900px;
    position: relative;
    padding-bottom: 50px;
}

.height {
    width: 900px;
    height: 50px;
}

.footer {
    width: 900px;
    height: 50px;
    position: absolute;
    bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="overlay">
    <div class="container">
        <div class="height">
            content
        </div>
    </div>

    <div class="footer">
        footer
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Ski*_*rou 5

尽管此处提供的答案似乎都不适用于我的特定情况,但我遇到了提供此整洁解决方案的这篇文章

#container {
  display: table;
}

#copyright {
  display: table-footer-group;
}
Run Code Online (Sandbox Code Playgroud)

我发现这对于将响应式设计应用于移动显示非常有用,而不必重新排序网站的所有html代码,body而是将自身设置为表格。

请注意,只有第一个table-footer-grouptable-header-group会这样渲染:如果有多个,则其他将被渲染为table-row-group


小智 5

使用 translateY 和 top 属性

只需将元素子元素设置为 position: relative 然后将其移动到 top: 100%(即父级的 100% 高度)并通过转换粘贴到父级的底部:translateY(-100%)(即高度的 -100%孩子)。

好处

  • 没有从页面流中获取元素
  • 它是动态的

但仍然只是解决方法:(

.copyright{
   position: relative;
   top: 100%;
   transform: translateY(-100%);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记旧浏览器的前缀。