Style Attribute and Installation ID and External Css

I understand the concept of saving all presentation elements outside the markup and putting them in an external .css file.

I want to understand what types of situations you would justify using the style attribute and installation identifiers and external Css.

At this point, I often used a style attribute, I usually use it to specify presentation elements specific to that element, and all that is for all elements that I draw into an external css file, but I would like to re-evaluate my position and make a better one future choice.

+6
css markup
source share
3 answers

Use external CSS for static definitions. Use element id lookups and style attributes for things that change at runtime or where you need Javascript to customize because CSS is not capable of what you want.

A good example of the latter was the alternation of zebras in jQuery to the widespread support for CSS 3 selectors :

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

Today you can do it in static CSS, but once upon a time, in Javascript there was a better option.

+2
source share

I use external style sheets, and the reasons are below:

  • Maintaining health is much easier when all my presentation materials are in one file.
  • Saving DRY code - yes, this is again. 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 this class if something needs to be hidden and deleted, if something needs to be shown. These days of full-blown Ajax applications, I save my code without repeating such things, and it is much cleaner.
  • It helps when you work in a large project - at my last workplace we had a set of applications that looked the same. Putting all this in an external stylesheet, including styles that will be called after a certain event, this helped the team apply a consistent user interface design to applications.

I tried to come up with reasons for using style attributes, but to be honest, I can only say that I use it when I'm lazy to open the stylesheet to change something quickly (I'm not too proud of this part, so I try to reduce it to minimum she)

+2
source share

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

For this reason, it’s worth it alone to assign an identifier to a particular element and define its styles in the stylesheet.

Secondly, if you find yourself writing a lot of inline styles, you can probably factor more of these styles using CSS inheritance properties or a few well-equipped classes in addition to the ID.

Performance, identifier selection is as fast as CSS, so using a large number of classes is actually slower than drilling with identifiers, even if only in microseconds.

The only real time that I think it is advisable to use inline styles is very fast properties, such as animation using javascript or hiding and displaying an element (although this can be done with classes).

0
source share

All Articles