CSS 垂直对齐:文本底部;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13586171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:53:13  来源:igfitidea点击:

CSS vertical-align: text-bottom;

css

提问by Wizard

Hi there I'm trying to position text to the bottom of the <div>. Neither vertical-align:text-bottom;or vertical-align:bottom;

嗨,我正在尝试将文本定位到<div>. 无论是vertical-align:text-bottom;vertical-align:bottom;

The trouble is below it is my navigation buttons and if I push it down then they go out of alignment.

问题在于它下面是我的导航按钮,如果我按下它,它们就会失去对齐。

Is there a way I can work around this with my CSS?

有没有办法用我的 CSS 解决这个问题?

a:link {color:#452809}
a:hover {text-decoration:underline;color:#f00}
a:visited {color:#3886e0}
.fleft {float:left}
.fright {float:right}
.clear {clear:both}
* {border:0;margin:0}
**body {font:12px Tahoma,Arial,Helvetica,sans-serif;color:#666;background:#3cb40d}
#main {margin:0 auto;width:780px;background:#fff url(images/vitalbodyhealth.gif) no-repeat center top}
#header {width:780px;margin:10px; height:210px}
#logo {padding-right:10px;text-align:right;padding-bottom:9px;height:150px;vertical-align:text-bottom} 
#logo a {text-decoration:none;text-transform:lowercase;font-style:italic;font-size:16px;color:#fff;font-weight:bold}
#logo H2 a {font-size:10px}
#buttons {padding-top:0px;height:40px;width:780px}
#buttons li {display:inline}
#buttons a {display:block;float:left;width:80px;height:26px;text-align:center;text-decoration:none;color:#fff;font-weight:bold;font-size:14px;padding-top:0px;margin-left:12px}
#buttons a:hover {width:80px;height:36px;text-decoration:underline}
#content {width:720px;margin:0 auto;padding:20px}**
.inner_copy {border:0;color:#f00;float:right;width:50%!important;margin:-100% 0 0 0;overflow:hidden;line-height:0;padding:0;font-size:11px}
#left {width:450px;padding:10px; background:#EFEFEF}
#left H1, #left H2 {color:#3cb40d;margin:0}
#left H1 {font-size:24px;padding:0}
#left H2 {font-size:18px;padding-top:10px}
#left a {color:#3886e0}
#left a:hover {text-decoration:none;color:#f00}
#left a:visited {color:#3886e0}
#right {float:right;width:240px; background:#EFEFEF}
#sidebar {width:240px;background:#EFEFEF}
#sidebar ul {margin:0;padding:0;list-style:none}
#sidebar li {margin-bottom:15px}
#sidebar li ul {padding:10px;border-top:none}
#sidebar li li {margin:0;padding:3px 0}
#sidebar li h2 {height:36px;margin:0;padding:10px 0 0 20px;background:#47872B;font-size:18px;color:#fff}
#sidebar a:link, #sidebar a:visited {text-decoration:none}
#sidebar a:hover {text-decoration:underline}
#sidebar li a {padding-left:10px;background:url(images/img09.gif) no-repeat 1px 5px}
#footer {background:#452809;height:47px;width:780px;margin:0 auto;font-size:10px;color:#fff;padding-top:23px;text-align:center;}
#footer div {padding:10px 38px}
#footer a {color:#fff;font-size:10px;text-decoration:none}
.padding {padding:10px;color:#f00;font-weight:bold}

Any help would be greatly appreciated.

任何帮助将不胜感激。

:)

:)

回答by totymedli

Modern solution

现代解决方案

Flexbox was created for exactly these kind of problems:

Flexbox 正是为解决这些问题而创建的:

#container {
    height: 150px;/*Only for the demo.*/
    background-color:green;/*Only for the demo.*/
    display: flex;
    justify-content: center;
    align-items: flex-end;
}
<div id="container">
    <span>Text align to center bottom.</span>
</div>

Old school solution

老派解决方案

If you don't want to mess with table displays, then you can create a <div>inside a relatively positioned parent container, place it to the bottom with absolute positioning, then make it 100% wide, so you can text-alignit to the center:

如果你不想弄乱表格显示,那么你可以<div>在一个相对定位的父容器内部创建一个,将它放在绝对定位的底部,然后让它100%宽,这样你就可以text-align把它放在中心:

#container {
    height: 150px;/*Only for the demo.*/
    background-color:green;/*Only for the demo.*/
    position: relative;
}

#text {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
}
<div id="container">
    <span id="text">Text align to center bottom.</span>
</div>

回答by aspirinemaga

To use vertical-alignproperly, you should do it on tabletag. But there is a way to make other html tags to behave as a table by assigning them a css of display:tableto your parent, and display:table-cellon your child. Then vertical-align:bottomwill work on that child.

vertical-align正确使用,您应该在table标签上进行。但是有一种方法可以通过display:table为您的父母和display:table-cell您的孩子分配一个 css 来使其他 html 标签表现得像一个表格。然后vertical-align:bottom将在那个孩子身上工作。

HTML:

HTML:

??????<div class="parent">
    <div class="child">
        This text is vertically aligned to bottom.    
    </div>
</div>????????????????????????

CSS:

CSS:

?.parent {
    width: 300px;
    height: 50px;
    display:? table;
    border: 1px solid red;
}
.child { 
    display: table-cell;
    vertical-align: bottom;
}?

Here is a live example: link demo

这是一个活生生的例子:链接演示

回答by rcorrie

if your text doesn't spill over two rows then you can do line-height: ;in your CSS, the more line-height you give, the lower on the container it will hold.

如果你的文本没有溢出两行,那么你可以line-height: ;在你的 CSS 中做,你给出的行高越多,它容纳的容器就越低。

回答by Levi Botelho

Vertical align only works in some select cases. The easiest way to make it function is to set display: tablein the parent element's CSS and display: table-cell;to the child element and then apply your vertical align attribute.

垂直对齐仅适用于某些特定情况。使其发挥作用的最简单方法是display: table在父元素的 CSS 和display: table-cell;子元素中设置,然后应用垂直对齐属性。

回答by user2060451

Sometimes you can play with padding and margin top, add line-height, etc.

有时您可以使用填充和边距顶部,添加行高等。

See fiddle.

小提琴

Style and text forked from @aspirinemaga

从@aspirinemaga 分叉的样式和文本

.parent
{
    width:300px;
    line-height:30px;
    border:1px solid red;
    padding-top:20px;
}