【问题标题】:Add a margin for all images without link inside a post content为帖子内容中没有链接的所有图像添加边距
【发布时间】:2019-03-02 18:09:45
【问题描述】:

我想为帖子内容中无链接的所有图片添加边距。

.post-content img (here I need to target only the images whith no link) {
margin: 1em;
}

所有带有链接的图片都必须保留自己的边距。

请问您有 CSS 甚至 javascript 的解决方案吗?

干杯

【问题讨论】:

  • 您的图片如何有链接?我假设它们只是包裹在<a> 标签中?在这种情况下,.post-content img 将只针对没有链接的图像。如果有 JavaScript,你将无能为力。如果您可以更新您的问题以在minimal, complete, and verifiable example 中列出所有 相关代码,这将有所帮助。如果您的 HTML 是在服务器端生成的,能否请您发布 输出。如需更多信息,请参阅有关how to ask good questions 的帮助文章:)
  • 添加您的HTML 代码。
  • 如果你说的是srcattribute,那么它是必需的(HTML5规范),所以我根本不明白这个问题
  • 你说得对,我问的问题不对。我需要为 中包含的所有图像添加边距

标签: css image margin


【解决方案1】:

如果<img><div class="post-content"> 的孩子,您可以使用此代码。

.post-content > img {
  margin: 1em;
}

因为img是第一级的children。

但是如果你有其他包装器,你可以使用这个代码给没有链接的图像添加边距,然后重置有链接的图像的边距。

.post-content img {
  margin: 1em;
}

.post-content a > img {
  margin: 0; /* Or the original margin */
}

【讨论】:

  • 你绝对明白了!谢谢琼
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2015-04-14
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多