如何在css中的图像上定位文本

Wer*_*eta 122 html css

如何将文本居中在css中的图像上?

<div class="image">
    <img src="sample.png"/>
    <div class="text">
       <h2>Some text</h2>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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

<style>
.image {
   position: relative;
}

h2 {
   position: absolute;
   top: 200px;
   left: 0;
   width: 100%;
   margin: 0 auto;
   width: 300px;
   height: 50px;
}
</style>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当我使用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();
?>
Run Code Online (Sandbox Code Playgroud)

这是prints.php:

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

xbo*_*nez 171

这样的事情怎么样: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;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container">
  <img id="image" src="http://www.noao.edu/image_gallery/images/d4/androa.jpg" />
  <p id="text">
    Hello World!
  </p>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 你可以避免z-index (12认同)
  • @danielad:在您编辑答案之前,它工作正常.我已经回滚了你的编辑. (8认同)
  • 遗憾的是,如果您希望您的网站在修复容器的大小时遵循现代的响应式实践,那么这最终将成为一个非常糟糕的选择(答案来自2012年,因此可以理解为过时).可以在这里找到更好的解决方案:/sf/ask/3033344681/ (3认同)

Gho*_*cho 28

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

基于中心未知

在这里工作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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案适用于无法指定设置高度或宽度的情况. (5认同)

Har*_*Joy 7

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

  • @KyokaSuigetsu:那么您应该更改问题标题以包含您正在使用的html2pdf. (8认同)
  • 如果图像需要是文档的语义部分怎么办? (2认同)

Kai*_*las 6

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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