【问题标题】:How to position text over an image with CSS如何使用 CSS 将文本放置在图像上
【发布时间】:2021-09-03 02:03:15
【问题描述】:

如何在 css 中将文本置于图像的中心?

<div class="image">
    <img src="sample.png"/>
    <div class="text">
       <h2>Some text</h2>
    </div>
</div>

我想做类似下面的事情,但我遇到了困难,这是我当前的 css

<style>
.image {
   position: relative;
}

h2 {
   position: absolute;
   top: 200px;
   left: 0;
   width: 100%;
   margin: 0 auto;
   width: 300px;
   height: 50px;
}
</style>

当我使用背景图像时,我没有从 html2pdf 获得任何输出:

<style>
#image_container{
    width: 1000px;
    height: 700px;
    background-image:url('switch.png');
}
</style>
<a href="prints.php">Print</a>
<?php ob_start(); ?>
<div id="image_container"></div>
<?php 
$_SESSION['sess'] = ob_get_contents(); 
ob_flush();
?>

这里是prints.php:

<?php require_once('html2pdf/html2pdf.class.php'); ?>
<?php
$html2pdf = new HTML2PDF('L', 'A4', 'en');
$html2pdf->writeHTML($_SESSION['sess']);
$html2pdf->Output('random.pdf');
?>

【问题讨论】:

  • 可以用背景图制作吗?
  • 这会在图像中产生叠加效果

标签: html css


【解决方案1】:

截至 2017 年,这对我来说反应更快并且更有效。 这是为了将文本放在里面 vs 上面,就像一个徽章。 我有一个变量来从数据库中提取数据,而不是数字 8。

这段代码以上面Kailas的回答开头

https://jsfiddle.net/jim54729/memmu2wb/3/

.containerBox {
  position: relative;
  display: inline-block;
}

.text-box {
  position: absolute;
  height: 30%;
  text-align: center;
  width: 100%;
  margin: auto;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  font-size: 30px;
}

.img-responsive {
  display: block;
  max-width: 100%;
  height: 120px;
  margin: auto;
  padding: auto;
}

.dataNumber {
  margin-top: auto;
}
<div class="containerBox">
  <img class="img-responsive" src="https://s20.postimg.org/huun8e6fh/Gold_Ring.png">
  <div class='text-box'>
    <p class='dataNumber'> 8 </p>
  </div>
</div>

【讨论】:

    【解决方案2】:

    做同样事情的小而短的方法:

    .image {
      position: relative;
      margin-bottom: 20px;
      width: 100%;
      height: 300px;
      color: white;
      background: url('https://via.placeholder.com/600') no-repeat;
      background-size: 250px 250px;
    }
    <div class="image">
      <p>
        <h3>Heading 3</h3>
        <h5>Heading 5</h5>
      </p>
    </div>

    【讨论】:

    • 标题在段落内没有业务。这是无效的 HTML。
    【解决方案3】:

    这是使用响应式尺寸的另一种方法。它将使您的文本居中并保持其在其父级中的位置。如果您不希望它居中,那就更容易了,只需使用 absolute 参数即可。请记住,主容器使用的是display: inline-block。还有很多其他方法可以做到这一点,具体取决于您的工作。

    基于Centering the Unknown

    Working codepen example here

    HTML

    <div class="containerBox">
        <div class="text-box">
            <h4>Your Text is responsive and centered</h4>
        </div>
        <img class="img-responsive" src="http://placehold.it/900x100"/>
    </div>
    

    CSS

    .containerBox {
        position: relative;
        display: inline-block;
    }
    .text-box {
        position: absolute;    
        height: 100%;
        text-align: center;    
        width: 100%;
    }
    .text-box:before {
       content: '';
       display: inline-block;
       height: 100%;
       vertical-align: middle;
    }
    h4 {
       display: inline-block;
       font-size: 20px; /*or whatever you want*/
       color: #FFF;   
    }
    img {
      display: block;
      max-width: 100%;
      height: auto;
    }
    

    【讨论】:

    • 此解决方案非常适合您无法指定设置高度或宽度的时候。
    【解决方案4】:

    对于响应式设计,最好使用具有相对布局的容器和具有固定布局的内容(放置在容器中)。

    CSS 样式:

    /*Centering element in a base container*/
    
    .contianer-relative{ 
      position: relative;
     }
    
    .content-center-text-absolute{ 
      position: absolute; 
      text-align: center;
      width: 100%;
      height: 0%;
      margin: auto;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      z-index: 51;
    }
    

    HTML 代码:

    <!-- Have used ionic classes -->
    <div class="row">
        <div class="col remove-padding contianer-relative"><!-- container with position relative -->
            <div class="item item-image clear-border" ><a href="#"><img ng-src="img/engg-manl.png" alt="ENGINEERING MANUAL" title="ENGINEERING MANUAL" ></a></div> <!-- Image intended to work as a background -->
            <h4 class="content-center-text-absolute white-text"><strong>ENGINEERING <br> MANUALS</strong></h4><!-- content div with position fixed -->
        </div>
        <div class="col remove-padding contianer-relative"><!-- container with position relative -->
            <div class="item item-image clear-border"><a href="#"><img ng-src="img/contract-directory.png" alt="CONTRACTOR DIRECTORY" title="CONTRACTOR DIRECTORY"></a></div><!-- Image intended to work as a background -->
            <h4 class="content-center-text-absolute white-text"><strong>CONTRACTOR <br> DIRECTORY</strong></h4><!-- content div with position fixed -->
        </div>       
    </div>
    

    对于 IONIC Grid 布局、均匀间隔的网格元素和上面 HTML 中使用的类,请参考 - Grid: Evenly Spaced Columns。希望它可以帮助你... :)

    【讨论】:

      【解决方案5】:

      这样的事情怎么样:http://jsfiddle.net/EgLKV/3/

      通过使用position:absolutez-index 将文本放置在图像上来完成。

      #container {
        height: 400px;
        width: 400px;
        position: relative;
      }
      #image {
        position: absolute;
        left: 0;
        top: 0;
      }
      #text {
        z-index: 100;
        position: absolute;
        color: white;
        font-size: 24px;
        font-weight: bold;
        left: 150px;
        top: 350px;
      }
      <div id="container">
        <img id="image" src="http://www.noao.edu/image_gallery/images/d4/androa.jpg" />
        <p id="text">
          Hello World!
        </p>
      </div>

      【讨论】:

      • 你可以避免z-index
      • @danielad:在您编辑答案之前它工作正常。我已回滚您的编辑。
      • 哈哈--谢谢我,我只是提醒你解决问题--昨天它不工作 - vene the hello world text
      • 你怎么能得到图片中间的文字?左 200,下 200?
      • 遗憾的是,如果您希望您的网站在修复容器大小时遵循现代的响应式做法,这最终会成为一个非常糟糕的选择(答案是从 2012 年开始的,因此可以理解它已经过时了)。可以在这里找到更好的解决方案:stackoverflow.com/questions/43333495/text-over-image-responsive
      【解决方案6】:

      正如 Harry Joy 指出的那样,将图像设置为 div 的背景,然后,如果您只有一行文本,您可以将文本的 line-height 设置为与 div 高度相同,这将放置您的div 中心的文本。

      如果您有不止一行,您需要将显示设置为表格单元格和垂直对齐到中间。

      【讨论】:

      • 我正在使用 html2pdf 从中创建一个 pdf。如果我使用背景图像,我看不到任何图像。请查看我的编辑。
      • 对不起,我不知道 html2pdf 是什么。
      【解决方案7】:

      为什么不将sample.png 设置为texth2 css 类的背景图片?这将在您在图像上书写时生效。

      【讨论】:

      • 如果图像需要成为文档的语义部分怎么办?
      • @animuson:我不认为这里是这种情况。 :\
      • 我正在使用 html2pdf 从中生成 pdf,如果我使用 background-image 我什么也得不到。
      • @KyokaSuigetsu :那么您应该更改问题标题以包含您正在使用 html2pdf。
      猜你喜欢
      • 2021-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多