Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between inline styles vs classes?

In my head, I've always known to use classes over inline styles for any project. But are there any effective differences between the two?

like image 817
Corey Hart Avatar asked Jun 29 '10 16:06

Corey Hart


People also ask

What is the difference between CSS and inline style?

The main difference between inline CSS and external CSS is that inline CSS is processed faster as it only requires the browser to download 1 file while using external CSS will require downloading HTML and CSS files separately.

What is the difference between class and style in CSS?

CSS class is a selector to assign class name either one or group of element and apply specific styles. Using class selector you can easily modify element styles. CSS class selector define using class attribute with value (user define class name).

What is the difference between style and class in HTML?

So the difference between both is you can re-use classes whereas you can't re-use inline styles. Show activity on this post. Inline Styles are definitely the way to go. Just look at http://www.csszengarden.com/ - that would never have been possible with classes and external style sheets...

What is the main difference between inline styles and the style tag?

Inline elements only affect their respective element. An important difference between the style tag and the inline attribute is specificity. Specificity determines when one style overrides another. Generally, inline styles have a higher specificity.


1 Answers

First of all:

  • If the HTML is built or generated independent of the overall site design (e.g. shared template code), then add reasonably-named classes and IDs, linked exclusively to external stylesheet(s). Use sufficient elements to allow for arbitrary CSS manipulation. For example, see the CSS Zen Garden. This applies to ALL CMSes, programs, scripts, and other dynamically-generated site content. The HTML output must contain absolutely no styling or layout of any sort at all. No exceptions.

Assuming you're dealing with static content, then:

  • If there's any way you can reuse the style, make it a class and link to a stylesheet.

  • If there's no way would ever reuse the style (it's a one-off thing that doesn't make sense anywhere else) then use a <style> block that references the element's #id.

  • If the CSS attribute only makes sense in the context of the surrounding HTML (e.g. some usages of clear:) then I inline the style into the element.

A lot of people call this heresy, just like a lot of people denounce any use of goto in modern programming languages.

However, rather than subscribing to stylistic dogma, my view is you should chose the method based on your circumstances that decreases your overall workload the most. Stylesheets add a level of indirection that makes site-level changes easy and helps build consistency. But if you have several dozen classes on each page that are only used in one place, then you're actually increasing your workload, not decreasing it.

In other words, don't do something dumb and confusing just because people tell you it's the right way to do it.

like image 118
tylerl Avatar answered Oct 17 '22 14:10

tylerl