简体   繁体   English

如何在要设置大小的图像上叠加文本?

[英]How do I overlay text on an image who's size is to be set?

I am trying to make a bar chart using tables, which I have almost accomplished to my liking. 我正在尝试使用表格制作条形图,而我几乎已经按照自己的喜好完成了。 The last step I want is text over my image which represents the bar. 我想要的最后一步是在图像上的文字表示条形图。 Here is the code I have thus far for building my little bar charts: 到目前为止,这是构建小条形图的代码:

$height = 50;

//build length
$width = 450;
$multi = $brewAvg / 5;
$width = $width * $multi;
print " <tr > <td > $count.  <a href=\"$breweryURL\"> $brewR</a>  </td>   <td > <img src=\"blueBar.png\"  width=\"$width\" height=\"$height\">  </td>  </tr> ";

And this produces something like this: 这将产生如下内容:

在此处输入图片说明

You can see in the code how I simply calculate the length of the bar based on a breweries rating. 您可以在代码中看到如何简单地根据啤酒厂的等级计算酒吧的长度。 What I want to do next is have the rating number show on top of each breweries on the left hand side. 接下来,我要在左侧的每个啤酒厂的顶部显示等级编号。 How would I go about accomplishing this? 我将如何实现这一目标?

Update: 更新:

I tried a tutorial I read here: 我尝试了在这里阅读的教程:

http://www.kavoir.com/2009/02/css-text-over-image.html http://www.kavoir.com/2009/02/css-text-over-image.html

and I changed my code to this: 我将代码更改为此:

print "<div class=\"overlay\"> ";
            print " <tr valign=\"middle\" > <td > $count.  <a href=\"$breweryURL\"> $brewR</a>  </td>   <td > <img src=\"blueBar.png\"  width=\"$width\" height=\"$height\">  </td>  </tr> ";
            print" 
                <div class=\"text\">
                <p> $brewAvg  </p>
                  </div>
                </div>
            ";

And my css I added was this: 我添加的CSS是这样的:

<style>
.overlay {
    position:relative;
    float:left; /* optional */
}
.overlay .text {
    position:absolute;
    top:10px; /* in conjunction with left property, decides the text position */
    left:10px;
    width:300px; /* optional, though better have one */
}


</style>

And it did put any of the value son top of my images. 它的确把任何有价值的儿子放在了我的形象之上。 All the text is in a list above all the bars like this: 所有文本都在所有小节上方的列表中,如下所示:

在此处输入图片说明

After using the help in the response I marked the answer, here is the result: 在响应中使用帮助后,我标记了答案,结果如下:

在此处输入图片说明

Now I just need to figure out how to vertically center everything so the grey background looks nicer. 现在,我只需要弄清楚如何使所有内容垂直居中,以使灰色背景看起来更好。 Thanks for the help! 谢谢您的帮助!

You can replace your print code with this one : 您可以用以下代码替换您的打印代码:

print " <tr > <td > $count.  <a href=\"$breweryURL\"> $brewR</a>  </td>   <td style=\"position: relative;\"> <img style=\"z-index: 8;\" src=\"blueBar.png\"  width=\"$width\" height=\"$height\"> <span style=\"position: absolute; top: 10px; left: 10px; color:#fff; z-index: 9; font-size: 14px;\">$brewAvg</span> </td>  </tr> ";

Of course I have hard-coded you can move appropriate style to your CSS file, and with changing top, left you can re-position the span for rating number. 当然,我已经硬编码你可以移动相应的样式到你的CSS文件,并与不断变化的上,左,你可以重新位置分级数跨度

You don't have to do any sort of black magic to achieve this .. just put the brewery name and rating in the same td , perhaps separated by a div. 您无需采取任何黑魔法来实现这一目标..只需将啤酒厂名称和评级放在同一td ,也许用div分隔即可。 The number can also be in a td so it can be vertically aligned. 该数字也可以是td因此可以垂直对齐。 Something like: 就像是:

<td>1.</td>
<td>Rating<br><a>Company</a></td>
<td><img></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM