【问题标题】:How to position text block over a image?如何在图像上放置文本块?
【发布时间】:2015-01-05 16:44:23
【问题描述】:

我试图在我的HTML 网站中将文本块放在图像上。上网google了一阵子,幸运的找到了以下4种方法:

<!--method 1 -->
<div style="position: relative; background: url(hknight.jpg); width: 738px; height: 284px;">
	<div style="position: absolute; bottom: 0; left: 0.5em; width: 400px; font-weight: bold; color: #fff;">
		<p>(text to appear at the bottom left of the image)</p>
	</div>

	<p style="position: absolute; top: 1em; right: 2em; width: 120px; padding: 4px; background-color: #fff; font-weight: bold; font-size: 11px;"> (text to appear at the top right of the image) </p>
</div>

<!--method 2 -->
<div style="background:url({{site.baseurl}}assets/hknight.jpg) no-repeat;width:738px;height:284px;text-align:center">
	<span style="color:#fcc">some text...</span>
</div>

<!--method 3 -->
<div style="position:relative; width: 738px; height: 284px">
	<span style="position:absolute; left:50; bottom:50; color:#fff; font-weight:bold">some text</span>
	<img src="hknight.jpg">
</div>

<!--method 4 -->
<TABLE BORDER="0" cellpadding="5" CELLSPACING="0">
  <TR>
    <TD WIDTH="738" HEIGHT="284" BACKGROUND="hknight.jpg" VALIGN="bottom">
      <FONT SIZE="+1" COLOR="yellow">some text ...</FONT>
    </TD>
  </TR>
</TABLE>

但是这四种方法都没有产生我想要的效果。你可以参考下图。 第一种方法可以将文本块准确地定位在图像上。但是,图像无法自动适应屏幕。例如,如果我从智能手机访问网页,则仅显示图像的左侧部分。顺便说一句,你可以看到边框就像一个尖角的正方形。 第二种方法与第一种方法类似,只是文本块不能准确放置。 第三种方法使图像自动适应屏幕。边框显示曲线,看起来非常好。但我无法准确定位文本。如您所见,在我的代码中,我使用了left:50; bottom:50;,它仅在左上角出现文本框。 第四种方法是table 的特殊方法。问题类似于第一种方法,因为它的边界很清晰,不能适应屏幕。更糟糕的是,虽然我指定了真正的widthheight,但图像比原始尺寸小。

您能想出一个更好的解决方案来满足我的要求吗?


编辑 1
对于方法 3,如果我选择 em 作为位置参数,它现在可以工作了!不知道为什么pixel在指定位置不显示文字。

编辑 2
我的要求:

  1. 图像边框线应为曲线。
  2. 文本块应按我的意愿准确定位。
  3. 图像可以自动适应屏幕。

【问题讨论】:

  • @RokoC.Buljan 这不是问题,而是我更喜欢的效果。
  • "However, the image cannot adapt to the screen automatically" 如果您设置了width: 738px; height: 284px;,您希望元素如何响应?
  • also (demo 1) background: hknight.jpg 将不起作用,因为 bg img 需要 url()
  • 您可能希望将max-width: 100% 添加到您的代码中以使其响应。
  • PS:这将是明智的,而不是通过 你实际上不想要什么的四个例子来解释你想要什么......解释简短的你真正想要什么

标签: html css image text


【解决方案1】:

让图像在overflow:hidden 定位的父对象中垂直流动。
随意放置absolute 文本。

.box{
  position: relative;
  overflow: hidden;
  color: #fff;
  border-radius: 10px;
}
.box img{
  width: 100%;
}

.bottom,
.right{
  margin:1em;
  position:absolute;
}
.bottom{
  bottom: 0px;
}
.right{
  right: 0px;
  top: 0px;
}
<div class="box">
  
  <img src="http://images2.layoutsparks.com/1/119132/city-lights-building-night.jpg">
  
  <p class="bottom">(text to appear at the bottom left of the image)</p>
  <p class="right"> (text to appear at the top right of the image)</p>
</div>

【讨论】:

  • 这里为什么使用.bottom{bottom:0;}.right{right:0;}
  • @Zachary 的唯一目的是向您展示您可以将该值更改为您需要的任何值,例如 bottom: 20px;left:50px; 应该不难!此外,如果您完全删除 bottom:0;,您几乎可以回答自己的问题。
  • 如果我需要在我的网页中插入两个不同大小的图像。然后.imgBg 不起作用,因为height 问题。
  • 我接受你的回答。这很棒。每次我需要在我的网页中插入一个图形。我只需要修改img 标签。 Tow cmets: 1 background url 不符合我的要求。 2 为了将图像放置在图像上的任何位置,我认为应该稍微改变一下答案。例如,定义一个.posotion {top:0px, bottom:0px, left:0px, right:0px)
  • @Zachary 我们不是来修复像素的,而是给你一个想法让你继续你的任务!我的回答希望涵盖您的所有 3 个要求。谢谢,编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多