css - 您能否清除 CSS 宽度属性以便它使用 "width"HTML 属性?

标签 css

在我正在处理的 WordPress 网站上,父主题添加了 width: autoheight: auto作为所有图像的默认样式。但是 WordPress 使用 widthheight用于设置通过所见即所得(即 <img src="/blah.png" width="320" height="160"> )添加的图像大小的 HTML 属性,这些样式会覆盖它。在不触及父主题的情况下,最好是在没有 JavaScript 的情况下,有没有办法保留使用通过所见即所得设置的大小的行为?我试过了 width: inherit , width: initial , 和 width: unset ,一切都无济于事。

最佳答案

Without touching the parent theme, and ideally without JavaScript, is there a way to preserve that behavior of using the size set through the WYSIWYG?

不是我能想到的。但是这里有一个使用 JavaScript 的解决方案,它将 widthheight CSS 内联设置为等同于 WP 集的属性。

var imgs = document.getElementsByTagName('img');

Object.values(imgs).forEach((el) => {
  elWidth = el.getAttribute('width');
  elHeight = el.getAttribute('height');
  el.style.width = `${elWidth}px`;
  el.style.height = `${elHeight}px`;
});
img {
  width: auto;
  height: auto;
}
<img src="https://via.placeholder.com/800x800" width="200" height="200" />
<img src="https://via.placeholder.com/400x400" width="200" height="200" />

关于css - 您能否清除 CSS 宽度属性以便它使用 "width"HTML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52298674/

相关文章:

javascript - 计算响应父 div 内定义数量的方形 div 的最大尺寸

JQueryMobile 包装数据角色 ="content"div

html - html 5 中子菜单的正确位置

javascript - 使用 CSS 或 Javascript 复制/剪切时从文本中删除样式

css - 使用带有自定义 CSS 的表单生成器并需要帮助更改按钮对齐方式

html - 如何将内部 DIV 对齐到外部 DIV 的中心

html - HTML 中的半透明 <Select> 元素

html - 右对齐文本居中的第二行

javascript - 如何测试 flexbox div 是否设置为 `display:block` ?

php - 请帮助我创建一个提交表单,该表单可以提交并保留在同一页面上。