【问题标题】:Issues with centering text vertically in HTML在 HTML 中垂直居中文本的问题
【发布时间】:2012-12-13 12:23:25
【问题描述】:

我知道此类问题已多次发布,但我似乎无法弄清楚我的问题是什么。我有一块文本,我想将它垂直居中放在页面的左侧。我不想使用沉重的填充来使文本居中。如果访问者随窗口高度变化,我希望它垂直居中。

我有一段文字:

<div class="welcome">
    <p>Testing Stuff</p> <br /> <br />  
    <p>Testing Stuff</p> <br /> <br />
    <p>Testing Stuff</p> <br /> <br />
    <p>Testing Stuff</p>
</div>

这是欢迎

的CSS
.welcome {
position: absolute;
top:50%;
display: table;
vertical-align: middle;
padding-left: 50px;
font-family: 'Helvetica-Light';
font-size: 40px;
}

.welcome p{
display: inline;
vertical-align: middle;
padding: 10px;
background: black;
color: white;
opacity: 0.7;
white-space: nowrap;
}

目前 top:50% 设置文本从 50% 开始,但我需要文本块的中心在 50%,而不是从 50% 开始。

请帮忙。

【问题讨论】:

  • &lt;br&gt; 标签有什么用?
  • 你可以添加 align : center for .welcome 看看它是否有效
  • 段落行之间的间距。我在文本上添加了背景,我需要更多空间,这样背景就不会重叠。有没有比
    标签更好的方法?
  • 好的,它在这个 fiddle 的中心,但它还没有完全到位:jsfiddle.net/userdude/CuUgV
  • 不完全;这不仅仅是发布的内容(“单元格”之间的白色间隙),而且我不太喜欢瞬态div.row 在那里。问题是,display: table 方法有点需要额外的层来让display: table-cell 充当中等(字面上和比喻上的)替身。不过,我还没有想出更优雅、更简洁的方法。

标签: html alignment center


【解决方案1】:

仍在检查这一点,但这是一种方法:

HTML

<div class="welcome">
  <div class="row">
      <p>Testing Stuff</p> 
      <p>Testing Stuff</p>
      <p>Testing Stuff</p>
      <p>Testing Stuff</p>
    </div>
</div>​

CSS

html,
body {
    height: 100%;
    width: 100%;
}
.welcome {
    height: 100%;
    width: 1px;
    display: table;
    padding-left: 50px;
    margin: auto;
    font-family: 'Helvetica-Light';
    font-size: 30px;
}
.welcome .row {
    display: table-cell;
    white-space: nowrap;
    vertical-align: middle;
}
.welcome .row p {
    display: table-cell;
    padding: 10px;
    background: black;
    color: white;
    opacity: 0.7;
}​

注意100%html, body 行上的使用,它允许您在.welcome 元素上使用margin: autoheight: 100%。我修复了白色间隙(由于 display: table-cell 块中解释的“空白”,即那些 p 标记位于不同的行,因此存在间隙。

http://jsfiddle.net/userdude/CuUgV/3/

【讨论】:

    【解决方案2】:

    我用一些 jQuery 做这个,希望这是你想要的:FIDDLE

    HTML:

    <div class="welcome">
        <div class="container">
            <p>Testing Stuff</p> <br /> <br />  
            <p>Testing Stuff</p> <br /> <br />
            <p>Testing Stuff</p> <br /> <br />
            <p>Testing Stuff</p>
        </div>
    </div>​
    

    CSS:

    .welcome {
        position: absolute;
        padding-left: 50px;
        font-family: 'Helvetica-Light';
        font-size: 40px;
        height: 100%;
    }
    
    .welcome p{
        display: inline;
        vertical-align: middle;
        padding: 10px;
        background: black;
        color: white;
        opacity: 0.7;
        white-space: nowrap;
    }
    
    .container {
        position: absolute;
        top: 50%;
    }​
    

    JS:

    $(function() {
        var containerHeight = $('.container').height();
        $('.container').css({'margin-top': -containerHeight/2});
    });​
    

    UPDATE 不带 jQuery:FIDDLE

    【讨论】:

    • jQuery?你不需要 jQuery。
    • 只是设置边距顶部,但这只是一种解决方案;)
    • 你能告诉我在哪里粘贴那个 JS 代码 sn-p 吗?我对所有这些网络东西都很陌生,所以我什至不知道该放在哪里。
    • &lt;head&gt; 中包含这个:&lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;,然后在&lt;div class="welcome"&gt;&lt;/div&gt; 之后,或在文件末尾:&lt;script&gt;[CODE INSIDE]&lt;/script&gt;
    • 哦,来吧。 @ZacharyHunt 不需要添加整个库 和单独的代码来执行此操作。太矫枉过正了,伙计。但是,是的,这是一种方法。 :s
    猜你喜欢
    • 2014-09-18
    • 2013-07-06
    • 2013-10-15
    • 2012-09-22
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多