skip to Main Content

I have encountered a simple question but can’t find the answer. The question is that, when I want to use inline style, for example width and height for an element in HTML, is it better to do it by width attribute or by inline style width?

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

VS.

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

what about one of them being deprecated or better for SEO solutions?

3

Answers


  1. The size is about styling so you should use CSS for it, however you need to add px to define the unit of the height and width.

    The MDN says regarding the height attribute:

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

    Login or Signup to reply.
  2. Based on this MDN link for HTML Attributes, you should use style as attributes for <div> (or elements that are not <canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video>) are considered legacy.

    From the linked MDN article:

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

    Login or Signup to reply.
  3. Along with what others have hinted at. Using style is more appropriate, as the width attribute only exists on specific elements.

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search