【问题标题】:putting absolute position div overtop last image将绝对位置 div 放在最后一张图片之上
【发布时间】:2016-03-13 18:41:42
【问题描述】:

我有以下代码,它以我的 Wordpress 帖子中的最后一张图片为目标,并将#first-img 放在上面,它只留下其他图片。

  <div id="s-img" class="row">      
    <?php
        preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
        for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
        if ($i == end(array_keys($images[1]))) {
        echo sprintf('<div id="last-img">%s</div>', $images[1][$i]);
        continue;
        }
            echo $images[1][$i];
        }
    ?> 
</div>

css

#last-image { 
   position: absolute
   height: 50px; width: 50px;
   background-color: red;
}

问题是我希望它把#first-img 放在图像的顶部,而不是图像本身。现在它替换了图像。

渲染的 html - 在图像和最后一张图像上

<div id="s-img" class="row">        
<img class="alignnone size-large wp-image-396" src="http://localhost/wordpress/wp-content/uploads/2015/12/img3-1008x720.jpg" alt="img3" height="720" width="1008"> </img>
</div>

<div id="last-img">
<img class="alignnone size-large wp-image-397" src="http://localhost/wordpress/wp-content/uploads/2015/12/img4-1080x720.jpg" alt="img4" height="720" width="1080"></img>
</div>

【问题讨论】:

  • 请从浏览器发布呈现的 HTML,因为 PHP 代码将没有用处
  • 请复制并粘贴屏幕截图无助于调试问题,谢谢
  • 添加到.row div - position:relative

标签: php jquery html css wordpress


【解决方案1】:

问题是我希望它把 #first-img 放在图像的顶部,而不是图像本身

您究竟想在这里实现什么目标?我没有看到您输出任何额外的文本内容(例如图像描述),所以我假设您只想在图像上覆盖一个空元素,以应用某种 CSS 效果(背景颜色)?

如果是这样,您应该能够为此使用伪元素 - 不是在图像本身上(因为具有空内容模型的元素不能拥有这些),而是在您放置在图像周围的 div 容器上:

#last-image { 
   position: relative;
   /* display:line-block; */ /* you might need to add that, */
                      /* if the image is not covering full width */
}
#last-image:after {
   content: "";
   position: absolute;
   top: 0; left:0; right:0; bottom:0; /* automatically stretch */
                                   /* to full parent width/height */
   background-color: red;
   z-index: 1; /* might not be necessary, depends on rest of CSS */
}

【讨论】:

  • #last-image 的写法是连接到图像。我在帖子中有不止一张图片。我不想更改图像的 css,我只想在最后一个上放一个 div。应用于#last-img 的所有css 仅应用于最后一张图像顶部的div。不是图像本身。这是它现在遇到的问题
  • 你试过我的建议了吗?如果是,它没有满足您的哪一部分要求?
  • 我有并且没有任何变化,图像仍然存在,但顶部没有任何显示(我尝试过有评论部分和没有评论部分)
  • 那么请给我们看一个工作示例,我们可以看看实际情况。
  • 不幸的是,它都是通过 wordpress、javascript 等引入的。我不知道如何重新创建它
【解决方案2】:

z-index 会将图像放置在所有其他元素的“上方”,以及所有其他具有较低 z-index 的页面元素的“上方”。

#first-img { 
z-index: 999;
}

【讨论】:

  • 问题在于,由于它们已连接,这也将最后一张图片置于其他所有内容之上
  • 我是不是搞错了什么?我知道#first-img 应该放置,绝对定位,高于其他。还是您希望它按顺序放置在其他图像之前,就像appendChild() 一样。看来我的误解使我蒙受了损失。希望人们能尽快提供建设性的东西,而不是仅仅吐出毒液。
  • 我也不知道为什么,我的问题是 #first-img 也链接到图像,所以如果我更改 z 索引如果影响图像和其他任何东西。我需要将 div 分开。定位最后一张图片,但不要更改它。我希望 css 应用到它上面的 div
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多