【问题标题】:How to center a text which is placed on an image如何使放置在图像上的文本居中
【发布时间】:2016-04-16 04:21:32
【问题描述】:

我有以下代码: HTML:

<div class="container-fluid">
            <div class="row css-logo-image">
                <a href="#">
                <div class="meteor_head">
                    <h1 class="meteor_heading">Meteor</h1>
                    <h3>Some text!</h3>
                 </div>
                  <img src="" class="landing-image imageMeteor" />
                </a>
             </div>
             <div class="row css-logo-image">
                 <a href="#">
                  <img src="" class="landing-image" />
                 </a>
             </div>
    </div>

CSS:

.meteor_head{
  position: absolute;
  z-index: 1;
  margin-left: 40vw;
}

.imageMeteor{
  position: relative;
  z-index: 0;
}
.meteor_heading{
    color: white;
    font-size: 100px !important;
    font-family: 'Exo 2', sans-serif;
}

.mainarea{
    padding-top: 0 !important;
}

如您所见,我正在尝试使用 z-index 在图像上显示文本。它有效,但唯一的问题是我不知道如何响应地居中文本。我尝试了很多东西,包括:

margin-left:40vw;

它只是拒绝工作。

【问题讨论】:

    标签: html css twitter-bootstrap meteor


    【解决方案1】:
    .meteor_head {
        position: absolute;
        z-index: 1;
        width: 100%;
    }
    .imageMeteor {
        position: relative;
        z-index: 0;
    }
    .meteor_heading {
        color: white;
        font-size: 100px !important;
        font-family: 'Exo 2', sans-serif;
        text-align: center;
    }
    h3 {
        text-align: center;
    }
    .mainarea {
        padding-top: 0 !important;
    }
    

    【讨论】:

      【解决方案2】:

      如果你知道文本的高度/宽度,下面的技巧可能会起作用:

      .meteor_head{
        position: absolute;
        z-index: 1;
        width: 200px;
        height: 100px;
        top:50%;
        left:50;
        margin-top: -50px;
        margin-left: -100px;
      }
      

      基本上,您可以使用 top/left 50% 将绝对元素居中,然后通过减少元素的高度和宽度的一半来修复它...

      【讨论】:

        【解决方案3】:

        试试这个

        * {
          box-sizing: border-box;
          font-family: sans-serif;
        }
        
        .container {
           position: relative;
           overflow: hidden;
        }
        
        img {
           width: 100%;
           opacity: .8;
        } 
        
        .centered-text {
           border-radius: 6px;
           padding: 0 6px;
           color: white;
           text-align: center;
           background-color: rgba(0, 0, 0, .4);
           position: absolute;
           top: 50%;
           left: 50%;
           -webkit-transform: translate(-50%, -50%);
           -moz-transform: translate(-50%, -50%);
           -o-transform: translate(-50%, -50%);
           -ms-transform: translate(-50%, -50%);
           transform: translate(-50%, -50%);
        }
        <div class="container">
          <img src="https://parrot-tutorial.com/images/nature_autumn.jpg">
          <div class="centered-text">
            <h4>Centered</h4>
            <p>Lorem ipsum dolor sit amet.</p>
          </div>
        </div>

        【讨论】:

        • 还请对您的答案进行一些解释,仅发布代码的答案可能不会被视为正确答案。
        猜你喜欢
        • 2015-09-16
        • 2017-07-04
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 2019-08-12
        相关资源
        最近更新 更多