如何使一个div的内容,用CSS的底部?内容、div、CSS

2023-09-11 23:32:11 作者:机智如我萌哒哒

说我有下面的CSS和HTML code:

的#header {   高度:150像素; }

<身体GT;   < D​​IV ID =头>     < H1>标题标题< / H1>     头内容(一个或多个行)   < / DIV>   < D​​IV ID =内容>     唧唧歪歪   < / DIV> < /身体GT;

标题部分是固定的高度,而是在报头内容可能会改变。 我想的标题的内容,以垂直对齐到头部部分的底部,所以文枝中的最后一行到标题部分的底部。

所以,如果只有一行文字,这将是这样的:

  -----------------------------
|标题标题
|
|
|
|头内容(导致一行)
-----------------------------

唧唧歪歪
 
css中怎么样才能使内容在div的底部中间

如果有三行:

  -----------------------------
|标题标题
|
|头内容(这是如此
|太多的东西,它完美
|跨越三线)
-----------------------------

唧唧歪歪
 

如何在CSS中这样做?

解决方案

相对+绝对定位是你最好的选择:

的#header {     位置:亲属;     最小高度:150像素;   }   #标题内容{     位置:绝对的;     底部:0;     左:0;   }

< D​​IV ID =头>   < H1>标题< / H1>   < D​​IV ID =头内容>有的含量小于/ DIV> < / DIV>

不过,您可能会遇到与该问题。当我尝试它,我有问题,出现下面的内容下拉菜单。它只是没有pretty的。

老实说,对于垂直居中的问题,那么,随着项目的垂直对齐的问题是不固定的高​​度,它更容易只是使用表。

例如:Can你这样做HTML布局而无需使用表?

Say I have the following CSS and HTML code:

#header {
  height: 150px;
}

<body>
  <div id="header">
    <h1>Header title</h1>
    header content (one or multiple lines)
  </div>
  <div id="content">
    bla bla bla
  </div>
</body>

The header section is of 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)
-----------------------------

bla bla bla

And if there were three lines:

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

bla bla bla

How can this be done in CSS?

解决方案

Relative+absolute positioning is your best bet:

  #header {
    position: relative;
    min-height: 150px;
  }
  #header-content {
    position: absolute;
    bottom: 0;
    left: 0;
  }

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