如何使用CSS将文本定位于图片上方

148

如何在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>

在此输入图片描述

使用background-image时,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');
?>

可以使用背景图片来实现吗? - Webwoman
这会在图像中制作叠加效果。 - Rayees AC
8个回答

197

这样的东西怎么样: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>


18
你可以避免使用 z-index。 - FooBar
9
在你修改答案之前,它一直运作良好。我已经将其恢复到以前的版本。 - Ayush
哈哈,感谢我提醒你修复问题,昨天它没有工作 - 甚至连“Hello World”文本也没有。 - Nickromancer
你怎么能够获取图片中间的文本?左边200,底部200? - Mason H. Hatfield
@MasonH.Hatfield 是的,只需玩弄左侧和底部值以移动文本即可。 - Ayush
7
如果您想让您的网站遵循现代、响应式实践,那么这将成为一个非常糟糕的选择,因为您正在固定容器的大小(答案是2012年的,因此可以理解为已过时)。在这里可以找到更好的解决方案:https://dev59.com/RlcQ5IYBdhLWcg3wFf8-。 - Sk446

33

这是另一种处理响应式尺寸的方法。它将保持您的文本居中,并在其父级内保持其位置。如果您不想让它居中,那么就更容易了,只需使用 absolute 参数即可。请记住,主容器正在使用 display: inline-block。根据您正在处理的内容,还有许多其他方法可以实现此目的。

基于 Centering the Unknown

这里有一个工作的 CodePen 示例

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;
}

6
对于那些无法指定固定高度或宽度的情况,这种解决方案非常棒。 - Yazmin

8
为什么不将sample.png设为texth2 CSS类的背景图像?这会产生你在图像上书写的效果。

2
如果图像需要成为文档的语义部分,该怎么办? - animuson
@animuson:我不认为这在这里是正确的。:\ - Harry Joy
我正在使用html2pdf生成PDF文件,如果我使用background-image,则无法得到任何内容。 - Wern Ancheta
8
@KyokaSuigetsu:那么你应该修改问题标题,让它包含你正在使用 html2pdf。 - Harry Joy

8

对于响应式设计,建议使用相对布局的容器,而将内容(放置在容器内)采用固定布局。

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网格布局、均匀分布的网格元素以及上面HTML中使用的类,请参考-Grid: Evenly Spaced Columns。希望对您有所帮助... :)


4

正如Harry Joy所指出的,将图像设置为div的背景,如果您只有一行文本,则可以将文本的行高设置为与div高度相同,这将使您的文本位于div的中心。

如果您有多行文本,则需要将显示设置为table-cell并将垂直对齐设置为middle。


我正在使用html2pdf从中创建pdf。如果我使用background-image,我看不到任何图像。请查看我的编辑。 - Wern Ancheta
很抱歉,我不知道html2pdf是什么。 - Yevgeny Simkin

3
截至2017年,这个解决方案更加响应,并且对我来说很有效。它是用于将文本放置在内部而不是覆盖的,就像徽章一样。与数字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>


0
一个更简短的方法:

.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>


2
标题没有放在段落里。这不是有效的HTML。 - isherwood

0
快速解决方案:在容器元素上设置position: relative;,并在该容器元素中的子元素上设置position: absolute;,并使用必要的topleftbottomright调整参数。

.top-left {
    position: absolute;
    top: 2px;
    left: 2px;
}

.bottom-right {
    position: absolute;
    bottom: 2px;
    right: 2px;
}

.container {
    position: relative;
    text-align: center;
    color: white;
    float:left;color: white;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
    <div class="container" style="">
    
        <img src="https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2@2x.png" width="100">
        <div class="top-left">Wikipedia</div>
        <div class="bottom-right">Everyone's Encyclopedia</div>
    
    </div>

使用以下CSS将其直接居中...

  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接