HTML 内联样式 vs 样式属性(哪个更好)

HTML inline style vs style attributes (which is better)

我遇到了一个简单的问题,但找不到答案。问题是,当我想使用内联样式时,例如 HTML 中元素的宽度和高度,是通过 width 属性还是通过内联样式 width 更好?

<div style="width:${widthx}; height:${heightx};"></div>

VS.

<div width="${widthx}" height="${heightx}"></div>

对于 SEO 解决方案,其中之一被弃用或更好怎么办?

大小与样式有关,因此您应该使用 CSS,但是您需要添加 px 来定义高度和宽度的单位。

MDN关于高度属性说:

Note: In some instances, such as <div>, this is a legacy attribute, in which case the CSS height property should be used instead.

基于此 MDN link for HTML Attributes,您应该使用 style 作为 <div>(或不是 <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video> 的元素)的属性被视为遗产。

来自 linked MDN 文章:

Note: For all other instances, such as <div>, this is a legacy attribute, in which case the CSS width property should be used instead.

以及其他人暗示的内容。使用 style 更合适,因为 width 属性只存在于特定元素上。

https://www.w3schools.com/tags/att_width.asp