English 中文(简体)
垂直对齐文本到框底部吗?
原标题:Vertically align text to the bottom of the box?

我做了框,我设置了线条高度, 文本自动垂直居中。 是否有方法或任何手段将文本设置在框的底部?

div {
    width: 100px;
    height: 100px;
    background: #eee;
    color: #333;
    text-align: center;
    line-height: 100px;
    vertical-align: text-bottom;
 }

<div>FoxRox</div>
最佳回答

<强>2020更新

您可以使用 CSS 网格、 弹性框或原方法, 使用 < code> line- leight :

body { display: flex } /* just to prettify */

div {
  margin: .5em;
  width: 6.25em; height: 6.25em;
  background: #eee;
  color: #333;
  text-align: center
}

.grid {
  display: grid;
  align-content: end;
}

.flex {
  display: flex;
  align-items: flex-end;
  justify-content: center
}

.lh { line-height: 11.5 /* 6.25*2 - 1.5 */ }
<div class= grid >Hello</div>

<div class= flex >Hello</div>

<div class= lh >Hello</div>

div line-leight 设置为相同值的 hile ,在您的情况中, <100px 是将文字垂直置于 div 内的一种方法。这是问题所在。

解决方案是将 line-leight 更改为文本的两倍高度减去大小,并删除无效的 垂直对齐

问题回答

display: inline-block 标记连接到 p 标签中的文本。 设置 垂直对称 p 元素 。

<div>
    <p>FoxRox</p>
</div>​

div {
    width: 100px;
    height: 100px;
    background: #eee;
    color: #333;
    text-align: center;
 }
p {
    display: inline-block;
    vertical-align: -80px;
}
​

http://jsfidd.net/Dfx92/6/" rel="noreferrer" >Demo

您可以设置显示到表格单元格, 尝试此 CSS 例如 。

width: 100px; 
height: 100px; 
display: table-cell; 
vertical-align: bottom;

德莫:http://jsfiddle.net/Kawwr/"rel="nreferrer">http://jsfiddle.net/Kawwr/

你可以看看我对>的答复。

以下是上述答案的“强势”表示:http://jsfiddle.net/DotNetScott/7BBC/"rel=“不跟随新人”>demo


关键在于在外容器上使用 display: table- cell 。 这样您就可以在 div 上使用 垂直对齐: bottom display: inline- block;





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...