html - 在中心和中间对齐 div 内的跨度

标签 html css

我有一个 div,里面有很多 span。 div 相当大,跨度不会填满它。我希望跨度如此对齐,以至于生成的文本看起来位于 div 区域的中心 - 垂直和水平。我不想每次必须正确设置时都必须计算像素数。我可以将它们垂直居中对齐,但不能水平对齐。

<div class="Container">
    <div class="InnerContainer">
        <b><span class="large">Hi</span><span class="normal">How're you doing</span>
        <br/>
        <span style="font-family: Arial; font-size: medium">Answer Here</span>
    </div>
</div>

.Container {
            /*Set surroundings of the div*/
            padding: 0;

            /* Set size of the div */
            width: 400px;
            height: 350px;

            /* Set position of the div */
            position: relative;
            margin: 0 auto;
            margin-left: -200px;
            margin-top: -175px;
            left: 50%;
            top: 50%;

            align-content: center;
            vertical-align: middle;
        }

        .InnerContainer{
            /*Set surroundings of the div*/
            padding: 0;

            /* Set size of the div */
            width: 400px;
            height: 350px;

            /* Set position of the div */
            position: relative;
            margin: 0 auto;
            margin-left: -250px;
            margin-top: -175px;
            left: 50%;
            top: 50%;

            /*align-content: center;*/
            vertical-align: middle;

            display: table-cell;
        }

最佳答案

您应该将 display:table-cell 添加到 .Container 并将 display:inline-block 添加到 .InnerContainer(因为除非指定宽度,否则 div 往往会占据所有可用的水平空间)。

然后添加 text-align:center; vertical-align:middle;.Container.

像这样:http://jsfiddle.net/fd5rV/

关于html - 在中心和中间对齐 div 内的跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21648961/

相关文章:

javascript - 仅在第二次操作后添加 animate.css 动画工作

css - 无点引擎解析器返回空字符串

html - 固定页眉和页脚

html - Internet Explorer 重定向到 Paypal 不工作

php - 我如何根据 while 循环中 3 的差异计数给 div 背景颜色

css - 禁用 IE8 图标周围的蓝色边框

css - 有时 Cassette 不会重写背景图像规则

javascript - 在 iOS Safari 中禁用链接上的默认拖动行为

html - 如何检查输入文本是否具有值(value)而不仅通过java脚本触发其中的提交

C# 使用递归从 List 创建 HTML 无序列表