【问题标题】:CSS to always center text in a pageCSS始终使页面中的文本居中
【发布时间】:2013-07-25 22:32:24
【问题描述】:

我正在尝试将文本添加到页面中心。即使用户尝试调整页面大小,页面中的文本也应该居中对齐。我接近解决方案,但在代码中缺少一些位置。这是我的code。任何帮助将不胜感激。

html代码

<div class="errorMessageContainer">

    <div class="errorMessageIcon">
        :(
    </div>
    <div class="errorMessageHeaderContainer">
        <div class="errorMessageHeader">
            I am trying to center me in a page.
        </div>
        <div class="errorMessageText">
            <span class="errorMessageSubHeader">I want to float.</span>Even if page is restored or forced minimized. <span class="errorContact"><a href="#">Please </a></span> help me on this.
        </div>
    </div>
</div>

css代码

  body {
        background-color: #666666;
    }

    .errorMessageContainer {
        width: 620px;
        margin: 200px 0 0 300px;

    }

    .errorMessageIcon {
        font-family: Segoe UI Semibold;
        font-size: 72px;
        color: #29abe0;
        display: inline-block;
        vertical-align: top;
    }

    .errorMessageHeaderContainer {
        display: inline-block;
        width: 500px;
        padding: 20px 0 0 30px;
    }

    .errorMessageHeader {
        font-family: Segoe UI Light;
        font-size: 28px;
        color: #ffffff;
    }

    .errorMessageSubHeader {
        font-weight: bold;
    }

    .errorMessageText {
        font-family: Segoe UI;
        font-size: 13px;
        color: #ffffff;
        width: 450px;
    }

    .errorContact a {
        color: #ffffff;
        text-decoration: underline;
    }

【问题讨论】:

    标签: css text centering


    【解决方案1】:

    最好的方法是正确设置您的displaymargins。不要为此设置值,否则如果 div 的大小发生变化,它将失败。这是我使用div 包装器和值display:table-cell 编写的示例

    演示http://jsfiddle.net/kevinPHPkevin/DSATE/1/

    【讨论】:

      【解决方案2】:
      .errorMessageContainer{
          position: absolute;
          height: 200px;
          width: 400px;
          margin: -100px 0 0 -200px;
          top: 50%;
          left: 50%;
      }
      

      【讨论】:

        【解决方案3】:

        这会将您的消息容器放在页面中心:

        .errorMessageContainer {
            width: 620px;
            height: 100px;
            position: fixed;
            top: 50%;
            left: 50%;
            margin-left: -310px;       
            margin-top: -50px;       
        }
        

        fiddle

        【讨论】:

          【解决方案4】:

          您可以将margin: 200px auto 0 auto; 用于.errorMessageContainer,请参见此处的小提琴:http://jsfiddle.net/DSATE/

          【讨论】:

            【解决方案5】:

            试试这个:

            .errorMessageContainer {
                width: 620px;
                margin: 200px auto 0;
            
            }
            

            【讨论】:

              猜你喜欢
              • 2016-10-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-06-12
              • 2015-02-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多