style width vs width attribute in HTML

前端 未结 3 886
渐次进展
渐次进展 2021-01-12 16:16

I saw last post similar to my question HTML5 canvas style height vs attribute height
But in that post, there was no clear information regarding which one will work and

3条回答
  •  梦毁少年i
    2021-01-12 16:57

    For most html elements, width attribute has nothing to do with the element's width. What defines an element's style(certainly contain width) is the element's style attribute.

    In other words, the style.width(style="width: 200px;") attribute determines the element's width.

    But some elements like canvas, svg, the width attribute will determines the element's width, if you don't set style.width attribute. In this case, width="200px" is the same as width="200" because most browsers use the px as default unit.

    PS:

    • The width is invalid to set the select's width.
    • But the width attribute is valid. You can access it and change it with freedom. You can use it to do other things.

提交回复
热议问题