Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I'm using a technology like Grails. It's really easy to wind up with inline styling.

Are there any downsides to auto-generating code with inline styling? Is this considered bad practice? Why?

share|improve this question

migrated from Mar 6 '12 at 17:03

This question came from our site for pro webmasters.

There are two main reasons why this auto-generated inline styling is bad:

  1. Inline CSS styling overwrites by default the rules defined in a stylesheet. This means that you'll have a hard time to style the content according to your requirements through the stylesheet itself, and it would require to constantly switch from stylesheet to the tool which let you customize the auto-generated code.

  2. Auto-generated code is [mostly] always stupid. And by stupid, I mean... totally, horribly stupid. If you have three buttons on a web page and you change the text color of those buttons, instead of writing one line of code, the auto-gen tool will write three lines. More buttons → more lines → more nightmare and performance issues.

Moreover, auto-generated code is mostly limited to what it is capable of doing. In CSS, I know how to create a shadow or a gradient which will work in Chrome, Firefox and other normal browsers. I also know it will not work in Internet Explhorror. If you use auto-generated code, in most cases (today there are a few exceptions), either it will not even let you create a shadow or a gradient, or it will do it, but will not tell you that it works only in some browsers¹.

Finally, consider a following scenario. You've created a website which uses auto-generated code to change the color of the buttons to gray. There are around 30 pages with an average of 5 buttons per page. Two days before the release, you're asked to switch the color from gray to teal:

  • If you've used a stylesheet from the beginning, you would spend ten seconds finding the line which specifies the color, and changing it.

  • If you use auto-gen code, you have to go and change all those colors for all 150 buttons, by hand. Or you can try to develop a reliable solution which will walk through all your source files and change the color. Can you do it in ten seconds?

¹ Of course, this is not an issue if you're knowledgeable about CSS and you know exactly what you're doing.

share|improve this answer
+1 for "Internet Explhorror" – Thomas Eding Oct 10 '12 at 20:26
What about cases where styling is semantically significant, e.g. labels are individually assigned to colors along a continuum which represent the states of some parameters, such that there are hundreds or thousands of different colors that might be used? If CSS allowed a class to say that a color should be computed from a function in a certain way, and then the HTML for individual labels could specify parameter values, that would be nicer, but I don't think that's possible. – supercat Aug 26 '14 at 21:36
@supercat: If you're creating thousands of blocks with different colors by hand, you're doing it wrong anyway. But yes, if a style is proper to a single element on the whole site, it makes sense to use inline styling. – MainMa Aug 26 '14 at 22:09
@MainMa: What would be "doing it right"? – supercat Aug 26 '14 at 22:28
@supercat: "doing it right" would be determining the logic behind the colors and use either server-side programming language or JavaScript to generate the colors automatically. – MainMa Aug 27 '14 at 7:30

Downsides, to my eye:

  • You may impair the integrity of visual design. If you reuse styles from a carefully crafted CSS, your design has better chances to look consistent.

  • Changing the visual design becomes harder because styling information is scattered in code.

  • Your page gets bigger. This may play a role if you e.g. style each item in a long list.

There a are times when you can't escape inlining style info: it's sending HTML-formatted emails.

share|improve this answer

Yes, inline styling is considered very poor practice. You have no centralized location for your styles, which is why (ideally) you want to link to an external style sheet. With an external style sheet, you make a few simple edits and your changes are reflected site-wide. With inline styles, you have to go hunting for those styles and make your edits.

share|improve this answer
Right - if you're dealing with static code there's no argument that inline styles are poor practice - but I'm referring to a situation in which code is being automatically generated. The overhead of changes is handled by the generator, not you. – Joshc1107 Mar 6 '12 at 16:42
@Joshc: even then it makes it harder. If you have a dedicated designer, they will want to work with css, not digging through generated files to tweak things. Additionally, depending on the way the generator spits it out, it may result in a larger download and longer page load time for the browser. – Daenyth Mar 6 '12 at 17:35
I don't think OrganizedFellow and Daenyth are following what you mean by 'generated code'. It can be contained in 1 place (in your code library), and the designers would be touching that single spot, not the multiple places where its output in the HTML. And its highly unlikely that inline styles would ever be large enough to cause a visible performance impact on anything other than mobile sites. – Graham Oct 10 '12 at 17:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.