如何在 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');
?>
原文由 Wern Ancheta 发布,翻译遵循 CC BY-SA 4.0 许可协议
这样的事情怎么样:http: //jsfiddle.net/EgLKV/3/
它通过使用
position:absolute
和z-index
将文本放置在图像上来完成。