Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何将div放置在容器底部?_Html_Css - Fatal编程技术网

Html 如何将div放置在容器底部?

Html 如何将div放置在容器底部?,html,css,Html,Css,给定以下HTML: 网页设计版权所有 如果你想让它“粘”到底部,而不管容器的高度,那么绝对定位就是最好的选择。当然,如果版权元素是容器中的最后一个元素,那么无论如何它都将位于底部 你能详细谈谈你的问题吗?准确地解释你想做什么(以及为什么你不想使用绝对定位)?可能不会 将位置:相对分配给#容器,然后位置:绝对;底部:0至#版权 #容器{ 位置:相对位置; } #版权所有{ 位置:绝对位置; 底部:0; } 网页设计版权所有 试试这个 <div id="container">

给定以下HTML:


网页设计版权所有
如果你想让它“粘”到底部,而不管容器的高度,那么绝对定位就是最好的选择。当然,如果版权元素是容器中的最后一个元素,那么无论如何它都将位于底部

你能详细谈谈你的问题吗?准确地解释你想做什么(以及为什么你不想使用绝对定位)?

可能不会

位置:相对
分配给
#容器
,然后
位置:绝对;底部:0
#版权


#容器{
位置:相对位置;
}
#版权所有{
位置:绝对位置;
底部:0;
}

网页设计版权所有
试试这个

<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>

网页设计版权所有

实际上,@User接受的答案只有在窗口高而内容短的情况下才有效。但是如果内容很高,窗口很短,它会将版权信息放在页面内容上,然后向下滚动查看内容,会留下浮动版权通知。这使得这个解决方案对于大多数页面(实际上像这个页面)都是无用的

最常见的方法是演示的“CSS sticky footer”方法,或者是稍微精简的变体。如果有固定高度的页脚,这种方法非常有效

如果您需要一个可变高度的页脚,如果内容太短,页脚将显示在窗口底部,如果窗口太短,页脚将显示在内容底部,您会怎么做

放下你的骄傲,用一张桌子

例如:

*{
填充:0;
保证金:0;
}
html,正文{
身高:100%;
}
#容器{
身高:100%;
边界塌陷:塌陷;
}

等。
版权所有一些邪恶的公司。。。

同样,如果有使用
位置的规定:绝对或<代码>位置:相对
,您始终可以尝试填充父级
或在页边空白处放置一个
边距:x。在大多数情况下,这不是一个很好的方法,但在某些情况下,它可能会派上用场。

CSS中没有所谓的
float:bottom
。最好的方法是在以下情况下使用定位:

position:absolute;
bottom:0;

样式的Div,
位置:绝对;底部:5px;宽度:100%正在工作,
但在这种情况下,它需要更多的滚动

window.onscroll = function() {
    var v = document.getElementById("copyright");
    v.style.position = "fixed";
    v.style.bottom = "5px";
}

如果使用
内联块
元素的文本对齐功能知道
#容器的高度
,则确实可以
将框对齐到
底部
,而无需使用
位置:绝对

你可以在行动中看到它

代码如下:

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

仅仅因为这一点没有被提及,在你这样的情况下,通常效果良好的是:

将版权div放在容器div之后

您只需以与另一个容器类似的方式格式化版权div(相同的总宽度、居中等),就可以了

CSS:

HTML:


网页设计版权所有
唯一可能不理想的情况是当容器div声明为
高度:100%
,用户需要向下滚动查看版权。但即使如此,您也可以进行变通(例如,当版权元素的高度为20px时,
margintop:-20px

  • 无绝对定位
  • 没有表格布局
  • 没有疯狂的css,这在其他浏览器中看起来都不一样(至少IE,你知道)
  • 简单明了的格式
旁白:我知道OP要求一个解决方案“……粘在‘container’div的底部……”,而不是它下面的什么东西,但是拜托,人们正在这里寻找好的解决方案,这就是一个

html,正文{
宽度:100%;
身高:100%;
}
.覆盖{
最小高度:100%;
位置:相对位置;
}
.集装箱{
宽度:900px;
位置:相对位置;
填充底部:50px;
}
.身高{
宽度:900px;
高度:50px;
}
.页脚{
宽度:900px;
高度:50px;
位置:绝对位置;
底部:0;
}

内容
页脚

为上述元素创建另一个容器
div
#版权
。在版权上方添加一个新的
div

这将迫使页脚位于其他所有项下,就像使用相对定位(
底部:0px;
)一样。

以下是一种方法,旨在使具有已知高度和宽度(至少大约)的元素向右浮动并停留在底部,作为其他元素的内联元素。它聚焦在右下角,因为您可以通过其他方法轻松地将其放置在任何其他角落

我需要制作一个导航栏,在右下角有实际的链接和随机的兄弟元素,同时确保该栏本身能够正确拉伸,而不会破坏布局。我使用了一个“shadow”元素来占据导航栏的链接空间,并将其添加到容器子节点的末尾



网页设计版权所有
填料
#版权所有{
显示:内联块;
位置:绝对位置;
底部:0;
右:0;
}
#版权所有-s{
浮动:对;
可见性:隐藏;
宽度:20em;/*~#版权所有.style.width*/
高度:3em;/*~#版权所有.style.height*/
}

您可以不使用
绝对定位,也不使用
表格
s(与标记和
#container, #copyright {
    width: 1000px;
    margin:0 auto;
}
<div id="container">
    <!-- Other elements here -->
</div>

<div id="copyright">
    Copyright Foo web designs
</div>
<!DOCTYPE html>
<div id="container">
  <!-- Other elements here -->
  <div id="copyright">
    Copyright Foo web designs
  </div>
  <span id="copyright-s">filler</span>
</div>

<style>
  #copyright {
    display:inline-block;
    position:absolute;
    bottom:0;
    right:0;
  }
  #copyright-s {
    float:right;
    visibility:hidden;
    width:20em; /* ~ #copyright.style.width */
    height:3em; /* ~ #copyright.style.height */
  }
</style>
<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>
#container {
    height:100%;
    border-collapse:collapse;
    display : table;
}

.foot {
    display : table-row;
    vertical-align : bottom;
    height : 1px;
}
<div id="container" style="background-color: #ccc; padding-bottom: 30px;">
  Hello!
</div>
<div id="copyright" style="margin-top: -20px;">
  Copyright Foo web designs
</div>
#container {
  display: table;
}

#copyright {
  display: table-footer-group;
}
.copyright{
   position: relative;
   top: 100%;
   transform: translateY(-100%);
}