Is it a bad practice to use custom HTML attributes and style them with CSS?

后端 未结 4 1544
故里飘歌
故里飘歌 2021-01-27 15:24

Is there any problem creating a CSS class like this:

[test] { font: 13px; }

and use it in an HTML attribute as this:

&
4条回答
  •  醉酒成梦
    2021-01-27 16:06

    While there is no problem in applying styles this way, and sure it does work in the browsers, you have to understand that this is not a standard way of applying styles.

    Since you have also asked from a 'practice' perspective, then, yes, this surely is not the right practice. The idea is: HTML is used to define the elements to be shown within the browser window, CSS is used to apply any styling that needs to be applied on these elements and JavaScript is used to perform any 'action' on it. So, from a practice perspective, this surely is bad practice!

    On another note, why the reluctance to create a class and apply it on the div? After all, this class can be reused as and when required. If you need it only once, then why not create an id selector?

    HTML:

    The right way of applying styles

    CSS:

    .rightApproach { color:Red; }

    See this fiddle where you can see your approach as well as the correct way of applying styles, be it class selector or id selector.

    http://jsfiddle.net/JkRPt/

提交回复
热议问题