html - 相对文本居中。垂直和水平

标签 html css

<分区>

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

如何在不指定宽度的情况下水平和垂直居中文本?

现在我有:

<div class="center">
<p id="logo"><span>Coming soon!</span></p>
</div>

CSS 是:

span {
    color:#ff0000;
    font-size: 14px;
    font-weight:normal;
    font-family:sans-serif;
    border-bottom-color: currentColor;
    border-bottom-style: dotted;
    border-bottom-width: 1px;
    line-height: 1.1;
}
.center {
    width:150px;
    height:100px;
    position:absolute;
    left:50%;
    top:50%;
    margin-left:-100px;
    margin-top:-50px;
}

因为文本的宽度会不同,所以我需要另一种解决方案。

最佳答案

为您的中心类提供以下 CSS:

text-align:center;
line-height: 100px;

请注意,这仅适用于单行文本。请参阅 jsFiddle 中的示例.

关于html - 相对文本居中。垂直和水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8961643/

上一篇:html - 如何将div背景置于其内容之上

下一篇:css - 当父元素使用像素(px)时,为子元素定义相对的字体大小不是可以的吗?

相关文章:

javascript - 在粘贴到任何文本编辑器和 Chrome/IE 时,如何防止过多的换行符?

css - 如何在不使用代码的情况下将元素设置为窗口宽度?

javascript - CircleType 文本在 rotateY 之后删除空格

html - 无法将 ul Dropdown 转换为 Select DropDown

html - 如何在 CSS 中正确设置宽度 - 需要修复 Mac 和 iPad 上 Safari 的错误

html - CSS - 在文本框旁边对齐图片的最佳方式?

css - 将超棒的字体图标居中对齐

javascript - 图像顶部的响应式文本叠加

javascript - 如何在js中读取表格中单击行的第一列?

html - div 没有从 body 继承的字体大小