假设我有以下 CSS 和 HTML 代码:
#header { 高度:150px; }
标题部分的高度是固定的,但标题内容可能会发生变化。
我希望标题的内容与标题部分的底部垂直对齐,因此最后一行文本“粘”到标题部分的底部。
所以如果只有一行文本,它会像:
----------------------------- | Header title | | | | header content (resulting in one line) -----------------------------
如果有三行:
----------------------------- | Header title | | header content (which is so | much stuff that it perfectly | spans over three lines) -----------------------------
如何在 CSS 中做到这一点?
相对+绝对定位是您最好的选择:
#header { 位置:相对;最小高度:150px; } #header-content { 位置:绝对;底部:0;左:0; } #header, #header * { 背景:rgba(40, 40, 100, 0.25); }
但是你可能会遇到问题。当我尝试它时,我遇到了出现在内容下方的下拉菜单的问题。它只是不漂亮。
老实说,对于垂直居中问题,以及项目的任何垂直对齐问题都不是固定高度,使用表格更容易。
示例:Can you do this HTML layout without using tables?
如果您不担心旧版浏览器,请使用 flexbox。
父元素需要将其显示类型设置为 flex
div.parent {
display: flex;
height: 100%;
}
然后将子元素的 align-self 设置为 flex-end。
span.child {
display: inline-block;
align-self: flex-end;
}
这是我以前学习的资源:http://css-tricks.com/snippets/css/a-guide-to-flexbox/
使用 CSS 定位:
/* Creates a new stacking context on the header */
#header {
position: relative;
}
/* Positions header-content at the bottom of header's context */
#header-content {
position: absolute;
bottom: 0;
}
作为 cletus noted,您需要确定标头内容才能使其工作。
<span id="header-content">some header content</span>
<div style="height:100%; position:relative;">
<div style="height:10%; position:absolute; bottom:0px;">bottom</div>
</div>
width = '100%'
<span>
元素默认具有 display: inline;
,它不会占用容器的整个宽度。最合适的解决方法是将跨度更改为 <div>
,默认情况下它是一个块元素。
我使用这些属性并且它有效!
#header {
display: table-cell;
vertical-align: bottom;
}
table-cell
破坏了很多东西。就像 Bootstrap 网格系统一样。 col-md-12
不会再给你一个 100% 宽的 div。
在同一个问题苦苦挣扎了一段时间后,我终于找到了一个满足我所有要求的解决方案:
不需要我知道容器的高度。
与相对+绝对解决方案不同,内容不浮动在自己的层中(即,它通常嵌入到容器 div 中)。
跨浏览器(IE8+)工作。
实施简单。
解决方案只需要一个 <div>
,我称之为“对齐器”:
CSS
.bottom_aligner {
display: inline-block;
height: 100%;
vertical-align: bottom;
width: 0px;
}
html
<div class="bottom_aligner"></div>
... Your content here ...
这个技巧通过创建一个又高又瘦的 div 来实现,它将文本基线推到容器的底部。
这是一个完整的示例,可以实现 OP 的要求。我将“bottom_aligner”变厚变红,仅用于演示目的。
CSS:
.outer-container {
border: 2px solid black;
height: 175px;
width: 300px;
}
.top-section {
background: lightgreen;
height: 50%;
}
.bottom-section {
background: lightblue;
height: 50%;
margin: 8px;
}
.bottom-aligner {
display: inline-block;
height: 100%;
vertical-align: bottom;
width: 3px;
background: red;
}
.bottom-content {
display: inline-block;
}
.top-content {
padding: 8px;
}
HTML:
<body>
<div class="outer-container">
<div class="top-section">
This text
<br> is on top.
</div>
<div class="bottom-section">
<div class="bottom-aligner"></div>
<div class="bottom-content">
I like it here
<br> at the bottom.
</div>
</div>
</div>
</body>
https://i.stack.imgur.com/8Eks6.jpg
::before
规则,这也有效,这消除了对额外元素的需要。我们最后使用了 flex,但是当你不能使用时,这很有用。
height
和 设置(设置为 px 或 % 或其他)时才有效。
执行此操作的现代方法是使用 flexbox。请参见下面的示例。您甚至不需要将 Some text...
包装到任何 HTML 标记中,因为直接包含在弹性容器中的文本被包装在匿名弹性项目中。
header { 边框:1px 纯蓝色;高度:150px;显示:弯曲; /* 定义 flexbox */ flex-direction: column; /* 从上到下 */ justify-content: space-between; /* 第一项在开始,最后在结束 */ } h1 { margin: 0; } 标题标题
一些文本与底部对齐
如果只有一些文本并且您想垂直对齐到容器的底部。
部分{边框:1px纯蓝色;高度:150px;显示:弯曲; /* 定义 flexbox */ align-items: flex-end; /* 框的底部 */ }
position:absolute;
时是不可能的。该解决方案非常适合我的情况!
justify-content: center;
使文本底部居中。
display: flex;
align-items: flex-end;
<h1>
下的内容在 <p>
或 <div>
内,我会使用 justify-content: space-between
。
如果父/块元素的行高大于内联元素的行高,则内联或内联块元素可以与块级元素的底部对齐。*
标记:
<h1 class="alignBtm"><span>I'm at the bottom</span></h1>
CSS:
h1.alignBtm {
line-height: 3em;
}
h1.alignBtm span {
line-height: 1.2em;
vertical-align: bottom;
}
*确保您处于标准模式
你可以简单地实现 flex
header { 边框:1px 纯蓝色;高度:150px;显示:弯曲; /* 定义 flexbox */ flex-direction: column; /* 从上到下 */ justify-content: space-between; /* 第一项在开始,最后在结束 */ } h1 { margin: 0; } 标题标题
一些文本与底部对齐
我多次遇到这个问题,有很好的解决方案,但也不是很好。因此,您可以使用 flexbox、网格系统或显示表格以不同的方式实现这一点。我首选的变体是 flex 和 'margin-bottom: auto' 的混合。这是我个人收集的文本底部可能性:
1. Flex/margin-top:自动;
.parent { 最小高度:200px;背景:绿色;显示:弯曲; } .child { 边距顶部:自动;背景:红色;填充:5px;白颜色; }
2.flex/align-self: flex-end
.parent { 显示:弹性;最小高度:200px;背景:绿色; } .child { 对齐自我:flex-end;背景:红色;填充:5px;白颜色; }
3.flex/align-items:flex-end;
.parent { 最小高度:200px;背景:绿色;显示:弯曲;对齐项目:flex-end; } .child { 填充:5px;背景:红色;白颜色; }
4.grid/align-self:end;
.parent { 最小高度:200px;背景:绿色;显示:网格; } .child { 对齐自我:结束;背景:红色;填充:5px;白颜色; }
5.表格/垂直对齐:底部;
个人我不喜欢这种使用表格的方法。
.parent { 最小高度:200px;背景:绿色;显示:表格;宽度:100%; } .child { 显示:表格单元格;垂直对齐:底部;背景:红色;填充:5px;白颜色; }
带垫片
6. 弯曲; /弹性:1;
.parent { 最小高度:200px;背景:绿色;显示:弯曲;弹性流:列; } .spacer { 弹性:1; } .child { 填充:5px;背景:红色;白颜色; }
7.flex/flex-grow:1个;
.parent { 最小高度:200px;背景:绿色;显示:弯曲;弹性方向:列; } .spacer { 弹性增长:1; } .child { 填充:5px;背景:红色;白颜色; }
8. 内联块/PseudoClass::before
.parent { 最小高度:200px;背景:绿色; } .child::before { display:inline-block;内容:'';高度:100%;垂直对齐:底部; } .child { 高度:200px;填充:5px;背景:红色;白颜色; }
❤️ 我个人喜欢的版本是:1., 2. 和 3.
您可以使用以下方法:
.header-parent { 高度:150px;显示:网格; } .header-content { 对齐自我:结束; }
这是另一种使用 flexbox 但不使用 flex-end 进行底部对齐的解决方案。想法是将 h1 上的 margin-bottom
设置为 auto 以将剩余内容推到底部:
#header { 高度:350px;显示:弹性;弹性方向:列;边框:1px 实心; } #header h1 { 边距底部:自动; }
我们也可以对文本使用 margin-top:auto
执行相同的操作,但在这种情况下,我们需要将其包装在 div
或 span
中:
#header { 高度:350px;显示:弹性;弹性方向:列;边框:1px 实心; } #header span { 边距顶部:自动; }
overflow-y: auto
(其他解决方案不支持)。
如果您有多个动态高度项,请使用表格和表格单元格的 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
该演示还有一个示例,如何使用相同的技术进行垂直居中对齐。
将 div 移动到底部的最佳解决方案如下。基本上,您需要做的是将 display flex 和 flex-direction 设置为父列,并为其子项添加一个“margin-top:auto”,该子项需要浮动到容器底部注意:我用过引导程序及其类。
.box-wrapper { 高度:400px;边框:1px 实心#000;边距:20px;显示:弯曲; // 添加仅用于表示目的。 Bootstrap 默认类已经添加 flex-direction: column; } .link-02 { 边距顶部:自动; }
Labore consectetur doloribus qui ab et qui aut facere quos。
所有这些答案都对我有用……我不是 flexbox 专家,但这很容易弄清楚,它简单易懂和易于使用。要将某些内容与其余内容分开,请插入一个空 div 并让它增长以填充空间。
https://jsfiddle.net/8sfeLmgd/1/
.myContainer {
display: flex;
height: 250px;
flex-flow: column;
}
.filler {
flex: 1 1;
}
<div class="myContainer">
<div>Top</div>
<div class="filler"></div>
<div>Bottom</div>
</div>
当底部内容的大小不固定时,即使容器大小不固定,这也会按预期做出反应。
为此,您不需要绝对+相对。很有可能使用容器和数据的相对位置。这就是你的做法。
假设您的数据高度为 x
。您的容器是相对的,页脚也是相对的。您所要做的就是添加到您的数据中
bottom: -webkit-calc(-100% + x);
您的数据将始终位于容器的底部。即使您有具有动态高度的容器也可以使用。
HTML会是这样的
<div class="container">
<div class="data"></div>
</div>
CSS会是这样的
.container{
height:400px;
width:600px;
border:1px solid red;
margin-top:50px;
margin-left:50px;
display:block;
}
.data{
width:100%;
height:40px;
position:relative;
float:left;
border:1px solid blue;
bottom: -webkit-calc(-100% + 40px);
bottom:calc(-100% + 40px);
}
希望这可以帮助。
bottom: -webkit-calc(-100% + x);
在做什么吗?
这是灵活的方法。当然,IE8 不支持它,因为用户在 7 年前就需要它。根据您需要支持的内容,其中一些可以取消。
尽管如此,如果有一种方法可以在没有外部容器的情况下做到这一点,那就太好了,只需让文本在它自己的内部对齐即可。
#header {
-webkit-box-align: end;
-webkit-align-items: flex-end;
-ms-flex-align: end;
align-items: flex-end;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
height: 150px;
}
一个非常简单的单行解决方案是在 div 中添加 line-heigth,记住所有 div 的文本都会放在底部。
CSS:
#layer{width:198px;
height:48px;
line-height:72px;
border:1px #000 solid}
#layer a{text-decoration:none;}
HTML:
<div id="layer">
<a href="#">text at div's bottom.</a>
</div>
请记住,这是一个实用且快速的解决方案,当您只希望 div 中的文本下降时,如果您需要组合图像和内容,您将不得不编写更复杂和响应式的 CSS
除了提到的其他 flex-box 解决方案之外:
您可以在第一个 div 上使用 flex-grow: 1
。这样,您的第二个 div 将与底部对齐,而第一个 div 将覆盖所有剩余空间。
在父 div 上,您必须使用 display: flex
和 flex-direction: column
。
https://i.stack.imgur.com/cphU7.png
/* parent-wrapper div */
.container {
display: flex;
flex-direction: column;
}
/* first-upper div */
.main {
flex-grow: 1;
}
检查小提琴:https://jsfiddle.net/1yj3ve05/
如果您可以设置内容的包装div的高度(#header-content如其他回复所示),而不是整个#header,也许您也可以尝试这种方法:
HTML
<div id="header">
<h1>some title</h1>
<div id="header-content">
<span>
first line of header text<br>
second line of header text<br>
third, last line of header text
</span>
</div>
</div>
CSS
#header-content{
height:100px;
}
#header-content::before{
display:inline-block;
content:'';
height:100%;
vertical-align:bottom;
}
#header-content span{
display:inline-block;
}
我发现这个解决方案基于默认的引导启动模板
/* HTML */
<div class="content_wrapper">
<div class="content_floating">
<h2>HIS This is the header<br>
In Two Rows</h2>
<p>This is a description at the bottom too</p>
</div>
</div>
/* css */
.content_wrapper{
display: table;
width: 100%;
height: 100%; /* For at least Firefox */
min-height: 100%;
}
.content_floating{
display: table-cell;
vertical-align: bottom;
padding-bottom:80px;
}
#header {
height: 150px;
display:flex;
flex-direction:column;
}
.top{
flex: 1;
}
<div id="header">
<h1 class="top">Header title</h1>
Header content (one or multiple lines)
</div>
#header { 高度:250px;显示:弹性;弹性方向:列;背景颜色:黄色; } .top{ 弹性:1; }
我设计了一种比上面提到的方法简单得多的方法。
设置标题 div 的高度。然后在其中设置 H1
标记的样式,如下所示:
float: left;
padding: 90px 10px 11px
我正在为客户开发一个网站,设计要求文本位于某个 div 的底部。我已经使用这两行实现了结果,并且效果很好。此外,如果文本确实扩展,填充将保持不变。
尝试:
div.myclass { margin-top: 100%; }
尝试更改 % 来修复它。示例:120% 或 90% ...等。
我刚刚为客户做的网站要求页脚文本是一个高框,底部的文本我通过简单的填充实现了这一点,应该适用于所有浏览器。
<div id="footer">
some text here
</div>
#footer {
padding: 0 30px;
padding-top: 60px;
padding-bottom: 8px;
}
padding: 0 30px
有点多余,您不妨将 padding: 30px
放在其他 2 个声明之后。
padding: 60px 30px 8px;
、padding: 60px 30px 8px 30px;
、四个明确的 padding-
规则,甚至 @TheWaxMann 的建议都是优越的 - 我愿意并且能够争辩说一个 ;-)
似乎正在工作:
#content {
/* or just insert a number with "px" if you're fighting CSS without lesscss.org :) */
vertical-align: -@header_height + @content_height;
/* only need it if your content is <div>,
* if it is inline (e.g., <a>) will work without it */
display: inline-block;
}
使用 less 使解决 CSS 谜题更像是编码而不是像……我只是喜欢 CSS。当您只需更改一个参数即可更改整个布局(而不破坏它:),这是一种真正的乐趣。
一个完美的跨浏览器示例可能是这里:
http://www.csszengarden.com/?cssfile=/213/213.css&page=0
这个想法是既要在底部显示 div 又要让它粘在那里。通常,简单的方法会使粘性 div 与主要内容一起向上滚动。
以下是一个完全工作的最小示例。请注意,不需要 div 嵌入技巧。许多BR只是为了强制出现滚动条:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
}
#floater {
background: yellow;
height: 200px;
width: 100%;
position: fixed;
bottom: 0px;
z-index: 5;
border-top: 2px solid gold;
}
</style>
</head>
<body>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div id="floater"></div>
</body>
</html>
如果您想知道您的代码可能无法在 IE 上运行,请记住在顶部添加 DOCTYPE 标记。这在 IE 上工作是至关重要的。此外,这应该是第一个标签,其上方不应出现任何内容。
<br />
标记...
content-type
header 提供上述文档会导致浏览器抛出 xml 解析错误。
2015年解决方案
<div style='width:200px; height:60px; border:1px solid red;'>
<table width=100% height=100% cellspacing=0 cellpadding=0 border=0>
<tr><td valign=bottom>{$This_text_at_bottom}</td></tr>
</table>
</div>
http://codepen.io/anon/pen/qERMdx
别客气
不定期副业成功案例分享
p
元素,或者至少是一个span
display: table-cell
、table-row
或table
。您再也不需要使用表格进行纯布局。