ChatGPT解决这个技术问题 Extra ChatGPT

将 DIV 与底部或基线对齐

我正在尝试将子 div 标签与父 div 标签的底部或基线对齐。

我想要做的就是让子 Div 位于父 Div 的基线,这就是它现在的样子:

HTML

<div id="parentDiv">
<div class="childDiv"></div>
</div>

CSS

#parentDiv
{
  width:300px;
  height:300px;
  background-color:#ccc;
  background-repeat:repeat
}
#parentDiv .childDiv
{
  height:100px;
  width:30px;
  background-color:#999;
}

笔记

我将有多个高度不同的 childDiv,并且我需要它们都与基线/底部对齐。

甚至没有思考!我刚刚删除了 parentDiv 的高度,所有 childDiv 现在都位于基线上。我只是一个愚蠢的莎莉!
但是,如果您希望它具有特定的高度 - 您应该根据父级使用 absolute 定位。

c
calvinf

你需要添加这个:

#parentDiv {
  position: relative;
}

#parentDiv .childDiv {
  position: absolute;
  bottom: 0;
  left: 0;
}

声明 absolute 元素时,它根据其最近的不是 static 的父元素定位(它必须是 absoluterelativefixed)。


我怀疑这是否符合他的要求,目前这会将所有(如果它是动态条形图,看起来好像会有多个子元素)放在彼此之上。
加 1,更多解释会很有用...@anyone !!
g
gcbenison

七年后搜索垂直对齐仍然会提出这个问题,所以我将发布另一个我们现在可以使用的解决方案:flexbox 定位。只需在父 div 上设置 display:flex; justify-content: flex-end; flex-direction: column(在此 fiddle 中也有演示):

#parentDiv
{
  display: flex;
  justify-content: flex-end;
  flex-direction: column;
  width:300px;
  height:300px;
  background-color:#ccc;
  background-repeat:repeat
}

简单的。正是我想要的。如果您不想拉伸项目,请使用 align-items: flex-start;
这是这几天最好的答案!
justify-content: flex-end 让我的滚动条消失
这仅在您希望所有子元素都底部对齐时才有效。
S
Sean256

使用 table 和 table-cell 的 CSS 显示值:

HTML

<html>
<body>

  <div class="valign bottom">
    <div>

      <div>my bottom aligned div 1</div>
      <div>my bottom aligned div 2</div>
      <div>my bottom aligned div 3</div>

    </div>
  </div>

</body>
</html>

CSS

html,body {
    width: 100%;
    height: 100%;
}
.valign {
    display: table;
    width: 100%;
    height: 100%;
}
.valign > div {
    display: table-cell;
    width: 100%;
    height: 100%;
}
.valign.bottom > div {
    vertical-align: bottom;
}

我在这里创建了一个 JSBin 演示:http://jsbin.com/INOnAkuF/2/edit

该演示还有一个示例,如何使用相同的技术进行垂直居中对齐。


对于尽可能多的桌子,有时这是最好的解决方案
这对我来说是最好的解决方案
这确实是最好的解决方案,因为使用绝对定位的元素通常会改变父元素的高度
W
Will Richardson

这有效(我只测试了 ie & ff):

<html>
<head>
    <style type="text/css">
        #parent {
            height: 300px;
            width: 300px;
            background-color: #ccc;
            border: 1px solid red;
            position: relative;
        }
        #child  {
            height: 100px;
            width: 30px;
            background-color: #eee;
            border: 1px solid green;
            position: absolute;
            bottom: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div id="parent">parent
        <div id="child">child</div>
    </div>
    outside
</body>
</html>

希望有帮助。


N
Nick Gillard

Y. Shoham 发布的答案(使用绝对定位)在大多数情况下容器是固定高度的情况下似乎是最简单的解决方案,但是如果父 DIV 必须包含多个 DIV 并根据动态内容自动调整其高度,那么可能有问题。我需要有两个动态内容块;一个与容器的顶部对齐,一个与底部对齐,虽然我可以让容器调整到顶部 DIV 的大小,但如果与底部对齐的 DIV 更高,它不会调整容器的大小,但会延伸到外面. romiem 上面概述的使用表格样式定位的方法虽然有点复杂,但在这方面更加稳健,并且允许与底部对齐并纠正容器的自动高度。

CSS

#container {
        display: table;
        height: auto;
}

#top {
    display: table-cell;
    width:50%;
    height: 100%;
}

#bottom {
    display: table-cell;
    width:50%;
    vertical-align: bottom;
    height: 100%;
}

HTML

<div id=“container”>
    <div id=“top”>Dynamic content aligned to top of #container</div>
    <div id=“bottom”>Dynamic content aligned to botttom of #container</div>
</div>

https://i.stack.imgur.com/ypMfh.jpg

我意识到这不是一个新的答案,但我想对这种方法发表评论,因为它引导我找到我的解决方案,但作为一个新手,我不允许发表评论,只能发帖。


Y
Yi Jiang

您可能必须将子 div 设置为具有 position: absolute

将您的孩子样式更新为

#parentDiv .childDiv
{
  height:100px;
  width:30px;
  background-color:#999;
  position:absolute;
  top:207px;
}

我试过了,但我需要的是 childDiv 的底部坐在 parentDiv 的底部,图表是动态的,并且每个 childDiv 到下一个的高度会有所不同......定位可能有效但我没有退出想出了一些可靠的东西......
将顶部设置为 207(一些任意数字)不是一个好主意,最好设置底部:0
佚名

一个旧帖子,但我想我会为任何寻找的人分享一个答案。而且因为当您使用 absolute 时,事情可能会出错。我会做的是

HTML

<div id="parentDiv"></div>
<div class="childDiv"></div>

Css

parentDiv{

}
childDiv{
    height:40px;
    margin-top:-20px;
}

这只会让底部 div 回到父 div 中。对于大多数浏览器也是安全的


这将使 childDiv 一半位于 parentDiv 的底部。您需要在边距顶部至少使用 -40px。也是使用 position:relative 的最佳实践;在这种情况下,对于两个 div。
c
cellepo

我有类似的东西,并通过有效地为孩子添加一些 padding-top 来让它工作。

我确信这里的其他一些答案会得到解决方案,但是经过很长时间后我无法让它们轻松工作;相反,我最终得到了 padding-top 解决方案,它简洁优雅,但对我来说似乎有点 hackish(更不用说它设置的像素值可能取决于父级高度)。


k
kenan238

您还可以使用垂直对齐

td { 高度:150px;宽度:150px;文本对齐:居中; } b { 边框:1px 纯黑色; } .child-2 { 垂直对齐:底部; } .child-3 { 垂直对齐:顶部; }

child 1 child 2 child 3


当问题特别提到DIV时,为什么要使用表格?
N
NAUMAN QAMAR Mujtaba

<body>
<!-- CSS styles-->

<style>
.parent{
  background-color:gray;
  height:100vh;
  display:flex;
  flex-direction:column;
  justify-content:space-between;
}
</style>




<!-- HTML -->

<div class="parent">
    <div class="child1">
        hello world! this is first child aligned at the top of parent div
    </div>
    <div class="child2">
        hello world! this is first child aligned in the space between first and last child
    </div>
    <div class="child3">
        hello world! this is last child aligned at the bottom of parent div
    </div>
</div>

</body>



P
Pawan

如果您的父 Div 中有多个子 Div,则可以使用如下所示的垂直对齐属性:

.Parent div
{
  vertical-align : bottom;
}

为什么这么多反对票没有评论?它不能解决问题吗?在线研究表明应该这样做。