【问题标题】:AMP image going beyond DIV limitsAMP 图像超出 DIV 限制
【发布时间】:2018-12-29 23:07:33
【问题描述】:

我正在尝试在我的博客上使用 AMP,并将图像转换为 AMP 图像会导致图像超出 DIV 并破坏整个显示。

AMP 未激活时,图像如下所示:

<img class="alignleft size-full wp-image-3654" src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/10/honor-8x-test-avis-review-photo-taille-main.jpg" alt="honor 8x test avis review photo taille main" width="2500" height="1875" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main.jpg 2500w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-300x225.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-768x576.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-1024x768.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-1400x1050.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-900x675.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-700x525.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-500x375.jpg 500w" sizes="(max-width: 2500px) 100vw, 2500px" />

激活 AMP 后,它们看起来像这样

<amp-img  class="alignleft size-full wp-image-3654" src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/10/honor-8x-test-avis-review-photo-taille-main.jpg" alt="honor 8x test avis review photo taille main" width="2500" height="1875" srcset="https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main.jpg 2500w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-300x225.jpg 300w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-768x576.jpg 768w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-1024x768.jpg 1024w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-1400x1050.jpg 1400w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-900x675.jpg 900w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-700x525.jpg 700w, https://test.laurentwillen.be/wp-content/uploads/sites/25/2018/10/honor-8x-test-avis-review-photo-taille-main-500x375.jpg 500w" sizes="(max-width: 2500px) 100vw, 2500px" / layout="responsive" /></amp-img>

我只是执行了一个 preg_replace 来用 amp-img 替换 img 标签。

如果没有 AMP,图像会保留在父 DIV 中,但激活 AMP 后,它们会完全脱离 DIV。如果我尝试将父 DIV 设置为宽度的 10% 以查看会发生什么情况,我会看到父 DIV 占用了 10% 的宽度,但图像保持其大小,就好像它使用 CSS 浮动属性一样。

你可以在这里看到它的样子: https://test.laurentwillen.be/gadgets/honor-8x-test-avis-et-prix/ 我可以强制父DIV变小,但不会影响图片。

你有什么想法吗?

谢谢

劳伦特

【问题讨论】:

  • 在您的网页中发现了很多错误(您可能需要检查您的 AMP 验证器)。我注意到在sizes 之后和layout 之前有一个额外的“/”。尝试将其删除并修复验证器中的错误,以帮助您的 AMP 页面按预期工作。

标签: amp-html responsive-images


【解决方案1】:

您似乎已将“大小”属性设置为“100vw”,浏览器宽度最多为 4000 像素

sizes="(max-width: 4000px) 100vw, 4000px"

更改父 div 的大小不会做任何事情,因为内部确实是绝对定位的,因此不受父 div 宽度的影响。

我发现这篇文章很有帮助,也许它对你也有用:
https://alistapart.com/article/using-responsive-images-now

【讨论】:

  • 嗨,罗西娜,是的,这正是重点。我尝试了不同的值,但问题是在 1000px 以上,父 div 占据了 75% 的宽度,所以我自然会使用 75vw 但它与可用空间不完全匹配,略有不同。我基本上想将图像拉伸到可用宽度的 100%。如果我使用浏览器控制台删除尺寸,它可以工作,但如果我从代码中删除它,AMP 根本不会显示图像。
猜你喜欢
  • 1970-01-01
  • 2011-05-18
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多