将 div 内未知长度的文本垂直居中

发布于 2024-12-27 11:41:21 字数 1554 浏览 2 评论 0原文

我想将未知长度的文本(好吧,它限制在一定的大小,但我不知道它是否需要一行或两行)在div内居中。因此我无法使用line-height。我尝试使用 display: table-cell,但布局被破坏了。我无法对 top 使用一些技巧,因为我需要它来定位。

这是一个示例

样机:

Mockup

原始代码:

HTML:

<div class="background">&nbsp;</div>
<div class="TileText">MyText - Another long, long, long text</div>

CSS :

.background{
    background-color:#000;
    height: 400px;
    width: 100%;   
}

.TileText{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #black;
    clear: both;
    padding-left: 10px;  
}

当前状态:

HTML:

<img src="images/castle.png" width="300" height="333" title="Castle" alt="Castle" />
<div class="TileTextWrapper">
    <div class="TileText">Text</div>
</div>

CSS:

.TileTextWrapper{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #57abe9;
    clear: both;
    padding-left: 10px;
    display: table;
}

.TileText{
    display: table-cell;
    vertical-align: middle;
}

更新:

现在文本垂直对齐。但恐怕 display: table 只能在现代浏览器中使用。

I want to center an unknown length of text (ok it's limited to a certain size, but I don't know if it needs one line or two lines) inside a div. Therfore I cannot use line-height. I tried to use display: table-cell, but than the layout is destroyed. I cannot use some tricks with top because I need this for positioning.

Here is an example.

Mockup:

Mockup

Original code:

HTML:

<div class="background"> </div>
<div class="TileText">MyText - Another long, long, long text</div>

CSS:

.background{
    background-color:#000;
    height: 400px;
    width: 100%;   
}

.TileText{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #black;
    clear: both;
    padding-left: 10px;  
}

Current state:

HTML:

<img src="images/castle.png" width="300" height="333" title="Castle" alt="Castle" />
<div class="TileTextWrapper">
    <div class="TileText">Text</div>
</div>

CSS:

.TileTextWrapper{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #57abe9;
    clear: both;
    padding-left: 10px;
    display: table;
}

.TileText{
    display: table-cell;
    vertical-align: middle;
}

Update:

Now the text is vertically aligned. But I'm afraid display: table would only work with modern browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独难免 2025-01-03 11:41:21

我已经更新了你的小提琴:

它似乎可以工作,

请参阅: http://jsfiddle.net/hSCtq/11/供参考。

我会将 .TileText 放在 .background 内。为了背景图像而创建一个空 div 在语义上是不正确的,并且在大多数情况下没有必要。单独背景 div 的一种情况是,您需要将背景图像的不透明度与内容分开设置。

<div class="background">
    <div class="TileText">MyText - Another long, long, long text</div>
</div>


.background {
    background-color:#000;
    height: 400px;
    width: 100%;   
    display: table
}

.TileText{
    max-width: 200px;
    display: table-cell;
    vertical-align: middle;
    color: #fff;
    clear: both;
    padding-left: 10px;  
}

其他问题同答案。

水平和垂直居中不使用表格的预标记?

旧的中心图像在 div 问题中(图像大小可变 - div 大小固定)

此外,谷歌搜索“center Vertical text css”的第一个结果将为您提供与我在我的文章中发布的相同的代码结构JS小提琴

I've updated your fiddle:

It seems to work

see: http://jsfiddle.net/hSCtq/11/ for reference.

I would put .TileText inside of .background. Creating an empty div for the sake of a background image is not semantically correct, and not necessary in most situations. One situation for a separate background div would be if you need to set the opacity of a background image separately from the content.

<div class="background">
    <div class="TileText">MyText - Another long, long, long text</div>
</div>


.background {
    background-color:#000;
    height: 400px;
    width: 100%;   
    display: table
}

.TileText{
    max-width: 200px;
    display: table-cell;
    vertical-align: middle;
    color: #fff;
    clear: both;
    padding-left: 10px;  
}

Other questions with the same answer.

Horizontally and vertically center a pre tag without the use of tables?

The old center a image in a div issue ( image size variable - div size fixed )

Also, the first result from googling "center vertical text css" will give you the same code structure I posted in my JSfiddle

流年已逝 2025-01-03 11:41:21

在 TileText div 中添加

标签:

HTML

<div class="background">   </div>
<div class="TileText"><p>MyText - Another long, long, long text</p></div>

CSS

.background{
    background-color:#000;
    height: 400px;
    width: 100%;   
}

.TileText{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #black;
    clear: both;
    padding-left: 10px; 
}

.TileText p { 
    position:absolute;
    display: table-cell;
    vertical-align: middle

  }

示例:http://jsbin.com/ubixux/2/edit

Add a <p> tag inside your TileText div :

HTML

<div class="background">   </div>
<div class="TileText"><p>MyText - Another long, long, long text</p></div>

CSS

.background{
    background-color:#000;
    height: 400px;
    width: 100%;   
}

.TileText{
    position: relative;
    top: -135px;
    max-width: 200px;
    height: 80px;
    background-color: #FFF;
    color: #black;
    clear: both;
    padding-left: 10px; 
}

.TileText p { 
    position:absolute;
    display: table-cell;
    vertical-align: middle

  }

Example here: http://jsbin.com/ubixux/2/edit

反话 2025-01-03 11:41:21

您必须添加 display:table-cell; 并添加 vertical-align:middle;

You must add display:table-cell; and also add vertical-align:middle;

初心未许 2025-01-03 11:41:21

要实现垂直对齐,必须使用表格。你可以这样写你的代码:

 <div style="width:600px; height:600px; border:#000000 1px solid;">

    <table style="width:100%; height:100%; vertical-align:middle">
        <tr>
            <td>
    MyText - Another long, long, long text
            </td>
        </tr>
    </table>
</div>

To achieve vertical alignment, you have to use table. You can write your code as this:

 <div style="width:600px; height:600px; border:#000000 1px solid;">

    <table style="width:100%; height:100%; vertical-align:middle">
        <tr>
            <td>
    MyText - Another long, long, long text
            </td>
        </tr>
    </table>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文