Style attribute vs setting ID and external Css

与世无争的帅哥 提交于 2021-01-27 05:18:03

问题


I understand the concept of keeping all presentational elements outside of the markup and putting them into an external .css file.

I want to get a feel for what types of situations you'd justify use of the style attribute vs setting IDs and external Css.

To this point, I've used the style attribute a lot, I usually use it to specify presentation items specific to that element and anything that is for all elements I pull into an external css file but I'd like to reevaluate my position and make the best choice going forward.


回答1:


Use external CSS for static definitions. Use element ID lookups and the style attribute for things that change at run-time or where you need Javascript to set things up because CSS isn't capable of what you want.

A good example of the latter was zebra-striping in jQuery prior to widespread support for CSS 3 selectors:

$(document).ready = function() {
    $("table tr:nth-child(even)").addClass("striped");
});

Today, you can do that in the static CSS, but once upon a time, doing in in Javascript was the best option.




回答2:


I use external stylesheets and the reasons are below:

  1. Maintainability - it's much easier when all my presentation stuff are in one files.
  2. Keeping code DRY - yep, this one again. Before, I used to even use the style attribute to set the display to "block" or "none" interchangeably. Now, I just use a class called "hide" and use that class if something needs to be hidden and remove it if something needs to be shown. In these days of full blown Ajax applications, I keep my code free from repeating such things and it's much more clean.
  3. Helps when you work in a large project setting - in my last workplace, we had a suite of applications that shared the same look and feel. By putting it all in an external stylesheet, including styles that will be invoked after a certain event has occurred, it helped the team to apply consistent UI design to the apps.

I tried to think of reasons of using style attributes, but honestly, I can say I only use it when I am lazy to open up the stylesheet to change something quickly(not too proud of this part, so I try to minimize it)




回答3:


One of the big advantages to moving all of your styles into a stylesheet is maintainability. Finding inline styles can be a huge pain for other people trying to maintain your code.

For that reason alone it's worth it to assign the specific element an ID and define its styles in the stylesheet.

Second, if you find yourself writing a lot of inline styles, you could probably be factoring out more of those styles using CSS's inheritance properties or a few well-factored classes in addition to the ID.

Performance-wise, ID selection is as fast as CSS gets, so using lots of classes is actually slower than drilling down with IDs, even if only by microseconds.

The only real time I find it appropriate to use inline styles is for very transient properties like animation using javascript or hiding and showing an element (though that can be done with classes as well).



来源:https://stackoverflow.com/questions/1403349/style-attribute-vs-setting-id-and-external-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!