如何使用 CSS 定位图像上的文本

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

enter image description here

当我使用背景图片时,我不会从 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');
?>
654066 次浏览

为什么不设置 sample.png作为背景图像的 texth2 css 类?这将产生效果,因为你已经写在一个图像。

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

如果有多行,则需要将显示设置为表格单元格,并将垂直对齐设置为中间。

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

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

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 中使用的类,请参考 -网格: 均匀排列的柱子。希望能帮到你

这是处理响应大小的另一种方法。它将保持您的文本居中,并保持其在其父级中的位置。如果您不希望它居中,那么更容易,只需使用 absolute参数。请记住,主容器使用的是 display: inline-block。还有许多其他方法可以做到这一点,这取决于您正在从事的工作。

基于 以未知为中心

这里有一个工作代码示例

超文本标示语言

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

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

做同样事情的一种小而简单的方法:

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

快速解决方案: 在容器元素上设置 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%);