如何将文本居中在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:absolute和z-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)
Gho*_*cho 28
这是使用自适应大小的另一种方法.它将使文本居中并保持其在父级中的位置.如果您不希望它居中,那么它就更容易了,只需使用absolute参数即可.请记住主容器正在使用display: inline-block.还有很多方法可以做到这一点,具体取决于你正在做什么.
基于中心未知
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)
为什么不设置sample.png为背景图片text或h2CSS类?这将在您在图像上书写时生效.
对于响应式设计,最好使用具有相对布局和内容(放置在容器中)具有固定布局的容器.
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中使用的类,请参阅 - 网格:均匀间隔列.希望它可以帮助你... :)