YaoRaoLov 2009-02-25 13:15 采纳率: 50%
浏览 1274
已采纳

如何将 div 的内容对齐到底部?

Say I have the following CSS and HTML code:

#header {
  height: 150px;
}
<div id="header">
  <h1>Header title</h1>
  Header content (one or multiple lines)
</div>

The header section is fixed height, but the header content may change. I would like to content of the header to be vertically aligned to the bottom of the header section, so the last line of text "sticks" to the bottom of the header section.

So if there is only one line of text it would be like:

-----------------------------
| Header title
|
|
|
| header content (resulting in one line)
-----------------------------

And if there were three lines:

-----------------------------
| Header title
|
| header content (which is so
| much stuff that it perfectly
| spans over three lines)
-----------------------------

How can this be done in CSS?

</div>

转载于:https://stackoverflow.com/questions/585945/how-to-align-content-of-a-div-to-the-bottom

  • 写回答

24条回答 默认 最新

  • 撒拉嘿哟木头 2009-02-25 13:20
    关注

    Relative+absolute positioning is your best bet:

    #header {
      position: relative;
      min-height: 150px;
    }
    
    #header-content {
      position: absolute;
      bottom: 0;
      left: 0;
    }
    
    #header, #header * {
      background: rgba(40, 40, 100, 0.25);
    }
    <div id="header">
      <h1>Title</h1>
      <div id="header-content">Some content</div>
    </div>

    But you may run into issues with that. When I tried it I had problems with dropdown menus appearing below the content. It's just not pretty.

    Honestly, for vertical centering issues and, well, any vertical alignment issues with the items aren't fixed height, it's easier just to use tables.

    Example: Can you do this HTML layout without using tables?

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(23条)

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn