首页 > 解决方案 > 为帖子内容中没有链接的所有图像添加边距

问题描述

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

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

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

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

干杯

标签: cssimagemargin

解决方案


如果<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 */
}

推荐阅读