【问题标题】:How to have the caption move with the image?如何让标题随图像移动?
【发布时间】:2015-07-30 23:20:44
【问题描述】:

当页面被调整大小时,我怎样才能让我的标题与我的图像一起移动,比如当用户使页面更长/更宽时?我怎样才能让标题与图像一起移动而不是留在原地?

如果您认为这不是一个合适的问题或表述不正确,请不要删除我的问题。

CSS:

#caption {
    position: absolute;
    padding: 10px;
    box-sizing: border-box;
    color: white;
    font-size: 14pt;
    font-family: sans-serif;
    background-color: #008800;
    opacity: 0.5;
    text-align: left;
    bottom:0px;
}

HTML:

<div style="text-align: Center;" id="photoSection">
    <img  id='photo' src='memchu.jpg' alt='photo' />
    <div id="caption">
        Stanford Memorial Church - the church used to have an 80' bell tower, which fell in the 1906 earthquake.
    </div>
    <div id="forwardOverlay"></div>
    <div id="backwardOverlay"></div>
</div>

JS:

"use strict";

var photoArray = [
    {filename: "memchu.jpg",
     caption: "Stanford Memorial Church - the church used to have an 80 bell tower, which fell in the 1906 earthquake."},
    {filename: "quad.jpg",
     caption: "The Stanford Quad"},
    {filename: "hoop.jpg",
     caption: "The <i>Red Hoop Fountain</i> with Green Library in the background."},
    {filename: "memorial-court.jpg",
     caption: "Memorial Court (in the front of the Quad) with Rodin's <i>Burghers of Calais</i> statues."},
    {filename: "gates.jpg",
     caption: "The Gates Building - home of Stanford Computer Science."},
    {filename: "stone-river.jpg",
     caption: "The Stone River statue near the Cantor Arts Center (Stanford's own art museum)."},
];

我试过了:

position: absolute;

但是当页面重新调整大小时,这似乎并没有让我的标题与图像保持一致。

【问题讨论】:

  • 你能提供一个 JSFiddle/JSBin 吗?
  • 试试position: relative;Fiddle
  • @sjm 是的,我可以,但如果我这样做,图像会可见吗?
  • @ZakariaAcharki 我试过了,但我需要标题在底部图像的顶部,但它移动到底部,所以我需要标题与图像保持一致,但也要在顶部它。
  • 响应式标题与图像之前已被多次询问,尝试将图像包装在与标题相同的
    中,然后进行这些编辑-stackoverflow.com/questions/17687819/…

标签: javascript arrays


【解决方案1】:

我认为这是你的追求,关键是 display:table on parent

#photoSection{display:table;margin:0 auto;position:relative;}
#photoSection img{max-width:100%;}
#caption {
 position:absolute;
 padding: 10px;
 color: white;
 font-size: 14pt;
 font-family: sans-serif;
 background-color: #008800;
 opacity: 0.5;
 text-align: left;
 bottom:0;
}

https://jsfiddle.net/sjmcpherso/dz0dyu6v/

【讨论】:

  • 是的,谢谢,但问题是 display:table;做吗?
  • 据我了解,它会将父级限制为子块元素的宽度, display:inline 会做同样的事情,但不允许 margin-left/margin-right:auto
猜你喜欢
  • 2020-03-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多