jquery - 用响应覆盖图像上的文本

标签 jquery html css cordova

我花时间努力在具有移动响应的图像上放置文本。实际上,我正在构建一个 HTML5 jQuery Mobile cordova 移动应用程序。

这是较小设备中的示例屏幕。

enter image description here

这是大设备中的示例屏幕。

enter image description here 我想将文本 Some Text 放置在图像的底部,与响应中心对齐。

这是在 jQuery Mobile 上生成图像和文本的代码。

<div class="ui-block-a">
  <div class="ui-bar" style="padding:5px">
    <img style="height: 100%;width:100%" src='img/book150.png'/>
     <p id="text">Some Text</p>
  </div>
</div>

尝试使用以下 CSS 样式,但这仅在我将图像大小固定为一种尺寸时才有效。

#text {
  z-index: 100;
  position: absolute;
  left: 150px;
  top: 350px;
}

最佳答案

你需要给 parent 一个position:relative才能让它工作。

要水平居中放置绝对的 block 元素,您需要应用此:

position:absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;

来自 MDN

Elements that are positioned relatively are still considered to be in the normal flow of elements in the document. In contrast, an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements. The absolutely positioned element is positioned relative to nearest positioned ancestor(non static). If a positioned ancestor doesn't exist, the initial container is used.

片段

.ui-block-a {
  position: relative;
  max-width:150px; /*you need to set a max-width/width */
}
.ui-bar {
  padding: 5px
}
#text {
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
   /*demo*/
  bottom: 5px;
  background: red;
  max-width:100px;
  text-align:center

}
img {
  height: auto;
  max-width: 100%
}
<div class="ui-block-a">
  <div class="ui-bar">
    <img src='//lorempixel.com/150/150' />
    <p id="text">Some Text</p>
  </div>
</div>

关于jquery - 用响应覆盖图像上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382184/

相关文章:

javascript - 使用 YouTube API 时 undefined variable ?

html - 关于 svg 的流模型的疑问

javascript - d3.js 问题。追加 div

css - 在宽度固定的 div 内缩放

jquery - 需要一个砖石插件来复制这个网格或只是 CSS?

jquery - 根据该 anchor jquery中的特定文本向 anchor 添加一个类

jQuery - 在 Div2 单击时显示 Div1,然后在单击文档时隐藏 Div1

jquery - 了解 jQuery Deferred.pipe()

javascript - 使用 HTML5 文件 API 检查文件是否已更改

javascript - jQuery .hover 效果 'color' 无法正常工作