html - 如何完善定位代码?

标签 html css css-position smartphone

以下是我在图像上定位文本的代码。要求是:

  1. 图像应自动适应屏幕。即使在智能手机上,图像也应该完整显示。不允许仅显示部分图像。
  2. 文本应准确定位在我希望的任何位置。

.txtimg{
  position: relative;
  overflow: auto;
  color: #fff;
  border-radius: 5px;
}

.txtimg img{
  max-width: 100%;
  height: auto;
}

.bl, .tl, .br,
.tr{
  margin: 0.5em;
  position: absolute;
}

.bl{
  bottom: 0px;
  left: 0px;
}
.tl{
  top: 0px;
  left: 0px;
}
.br{
  bottom: 0px;
  right: 0px;
}
.tr{
  top: 0px;
  right: 0px;
}
<div class="txtimg">
  
  <img src="http://vpnhotlist.com/wp-content/uploads/2014/03/image.jpg">
  
  <p class="bl">(text to appear at the bottom left of the image)</p>
  <p class="tr"> (text to appear at the top right of the image)</p>

</div>

但是,在我的 firefox 浏览器上,左下角的文本无法完全显示。 enter image description here

通过单击下面的运行代码片段,代码片段在 stackoverflow 中运行良好。

我不知道为什么。我在任何地方找到了解决方案:将 overflow:auto 更改为 overflow:visible。问题将消失。

有什么建议吗?

最佳答案

我无法重现此特定代码的问题,但我知道问题所在。只需在图像上添加垂直对齐。

.txtimg img {
max-width: 100%;
height: auto;
vertical-align: bottom;
}

这也是这样工作的:

.txtimg img {
max-width: 100%;
height: auto;
display: inline-block;
}

关于html - 如何完善定位代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830227/

相关文章:

javascript - 在 Django 中设置当前导航菜单项的样式

javascript - "z-index"类似跨浏览器窗口/ View 的方法

javascript - 仅使用 JavaScript 获取另一个网站元素的内容

javascript - less 1.4 string.trim() ie8 崩溃

internet-explorer-8 - IE8 最小宽度和绝对定位问题

html - 如何阻止 max-width 被定位项忽略?

html - 滚动在 chrome 中无法悬停

ios - 在纯 css 视差中平滑滚动

html - 是否可以让 child 漂浮在垂直滚动条上?

css - 如何将我的导航栏固定到页面顶部