views:

492

answers:

12

As someone who is beginning to make a transition from table based design to full CSS I'm wondering if using the style attribute to make adjustments to elements is considered "cheating" and if absolutely ALL presentation should be strictly in the style sheet?

See also:

A question of style - approaches to styling and stylesheets

+14  A: 

There are cases where you know for sure that all you want to do is tweak the style of this one specific element, and nothing else.

In those cases you can happily use an inline style attribute. But then, at some point in the future, you'll realise that in fact you need to apply the same style to something else, and you'll realise you were wrong.

Been there, done that. 8-)

RichieHindle
+1 ... Been there, done that *and* got the t-shirt.
Adrien
+1  A: 

It's a very "personal" question, to me the word "ALL" is a very strong word. You should do your best to have most of the styling in your css. but you can use style occetionally if it makes your life easier.

Nir Levy
+2  A: 

It's not maintainable. All of us have done it. What you're best to do is put every adjustment into a style. Let me teach you something most developers do not know about CSS ... you can use N styles at a time.

For example, imagine you have a great style for colorized divs called someDIVStyle:

.someDIVStlye
{
  background-color: yellow;
  ...
}

You want to use it, but just want to adjust the background-color to blue. Many people would copy/paste it and then make a new style with the change. However, simple create a style like this:

.blueBackground
{
  background-color: blue;
}

Apply it as such:

<div class="someDIVStyle blueBackground">...

The style furthest to the right always overrides the properties of the styles preceding it. You can use a number of styles at once to meet your needs.

Nissan Fan
Pedantry: you probably want to avoid class names that just describe the styles applied by rules written against that class; instead, describe the specific purpose of the intended rules (example: `highlight`). That way, you can come back later and change `background-color: blue` to `background-color: red` without causing all the markup using it to become misleading...
Shog9
while I agree that CSS class naming shouldn't talk about position on a page or visual appearance as a rule, having these specialized adjustments (blueBackground) named for what they are does increase the readibility quite a bit when you're looking through n number of classes if all they are is accent classes.
Nissan Fan
Actually, the last declared style overrides the previous declared style, so in this example you would need to ensure that .blueBackground comes before .someDIVStyle in your stylesheet. This actually bit me once, and it took me forever to figure out.
Stuart Branham
@shog9 - that ain't pedantry, this reveals a serious flaw in the design philosophy
annakata
I'd frown on this practice _more_ than an inline style because this obscures the makes it much more difficult to figure out what exactly is going on.
dburke
I think this is a trivial design decision. I've been using styles since the days of Quark, PageMaker and SGML-based Compugraphic type codes driving phototype (which you could argue are the basis for HTML quite frankly). I would say having seen this stuff and used it for over 30 years I know what is easiest to manage over time. Why not just use GUIDs or MD5 hashes for your style names since you have no intention of letting them represent what they are?
Nissan Fan
The point is for styles to represent meaning - i.e. <span class="search-result"> and <span class="warning"> rather than <span class="blueBackground"> and <span class="yellowBackground"> - if you later need to add a third style that only makes sense in blue or yellow, you don't need to change the HTML for it to work sensibly.
Peter Boughton
A: 

Generally it is best to have styles on the style sheet especially if it will be used multiple times, but using the style attribute is definitely not "cheating". A quick look through the stackoverflow source shows many examples of this.

Craig
Just because StackOverflow does something doesn't make it right!
Peter Boughton
+4  A: 

You can use the style attribute, but the point of using CSS is that you make a change in a single file, and it affects the entire site. Try to avoid it as much as possible (old habits die hard)

Virat Kadaru
+2  A: 

I agree with some other posters that it is best to keep the style information in the stylesheet. CSS tends to get complicated quickly, and it is nice to have that information in one place (rather than having to jump back and forth from HTML to stylesheet to see what styles are being used).

A little off-topic tip: Pressing F12 in IE8 brings up a great tool that lets you inspect the styles of elements in web pages you're browsing. In Firefox, FireBug does the same thing. Those kinds of tools are lifesavers if you want to know how a style change will affect an element.

Jon
++for mentioning Firebug (and IE8's clone): these tools really change the game when it comes to maintaining styled markup, allowing you to write complex sets of rules without needing to keep track of all the interactions in your head.
Shog9
Yep, they show inherited style information too (all the way up to the built-in styles from the browser, at least FireBug does). You can get the IE Web Developer tools for other versions of IE, all the way back to IE6.
Jon
A: 

Yes, it's kind of cheating, but it's up to you if you want to cheat a little. :)

The fundamental idea of having the styles in a style sheet is to separate the content from the layout. If you use the style attribute you are still mixing layout within the content.

However It's not that terrible, as you can quite easily move the style into a class. It's quite handy during development to be able to set a style on a specific element so easily without having to make up a class name and worry how the style will cascade.

I sometimes let the style attribute go through in the production code, if it's something that is specific for just one page, and if it's doubtful that it will be there for long. Occationally just because I am pressed for time, and it can be cleaned up later on...

So, even if you use a style attribute sometimes, you should still have the ambition that all the styles should be in a style sheet. In the long run it makes the code easier to maintain.

Guffa
A: 

As others have said, in general, no.

However, there are cases where it makes perfect sense. For example, today I had to load an random background image into a div, from a directory with an unknown # of files. Basically, the client can drop files into that folder and they'll show up in the random background image rotation.

To me, this was a clear reason to dynamically build up the style tag on the div.

In addition, if you're using, for example, the .net framework with webforms and built-in controls then you'll see inline styles used anyway!

ScottE
A: 

There can be very good reasons to put style information in a specific page.

For example, if you want to have a different header background on every page (travel agencies...), it is far easier to put that style information in that specific element (better, in the head of the document...) than to give that element a different class on every page and define all those classes in an external style-sheet.

jeroen
Why not just use a separate, page-specific stylesheet for each page?
Shog9
Depends on the amount of styling. If it is just one background image, it seems a bit of a waste to slow down the loading of your page by adding another external style sheet.
jeroen
Doesn't have to be external, if that's a concern.
Shog9
That´s what I meant with __better, in the head of the document__. I just think that in some cases it is convenient to just overwrite one property in the head of the document in a style declaration instead of declaring classes for every single page for only one property.
jeroen
Ah, gotcha. I wasn't really thinking of in-page stylesheets and external stylesheets when i read your answer the first time through.
Shog9
A: 

The style attribute does have one important use: setting style programmatically. While the DOM includes methods to manipulate style sheets, support for them is still spotty and they're a bit heavyweight for many tasks, such as hiding and showing elements.

outis
+4  A: 

I feel there's an aspect that has not been touched upon here: the distinction between hand-edited HTML snippets and generated HTML snippets.

For human editing, it's probably better and easier to maintain to have the styles in a file.

However

As soon as you start generating HTML elements, with server-side scripts or with some kind of JavaScript, be sure to make all styles required for basic functionality inline!

For example, you wrote some kind of JavaScript library that generates tooltips. Now, you will inject DIVs into your page, that will need some styles. For example, position: absolute and, initially, display:none. You may be tempted to give these elements the class .popup and require that this class has the correct definitions in some CSS file. After all, styles should be specified in the CSS file, right?

You will make your JavaScript library very annoying to reuse, because you can no longer simply copy and invoke one .js file and be done with it. Instead, you will have to copy the .js file, but also have to make sure that all styles required by the script are defined in your CSS file, and you have to go hunting for those, and make sure their names don't conflict with classes you already have.

For maximum ease of use, just go ahead and set the required styles directly on the element as you create it. For styles that are purely for aesthetical purposes, such as background-color, font-size and such, you can still attach a class, to give the consumer of your script an easy way to change the appearance of your script elements, but don't require it!

rix0rrr
And that's why you make your .js attach a `<style>` tag to your `<head>` containing rules with low enough specificity so that the developer using your script may easily override them in an external style sheet. But you still not use the `style=` attribute as it is almost impossible to override.
Andrew Moore
The style attribute is perfectly well overridable using the !important keyword.
rix0rrr
With `style=` the CSS editor needs to know all the inner workings of the generated code because they can't see all the possible styles in a style-sheet. If your generated elements are meant to be customized through CSS then the CSS should be external.
Mr. Shiny and New
A: 

Yes, the style attribute is frowned upon in general. Since you're moving to the CSS method from table-based, I'd strongly recommend that you avoid inline styles. As a previous poster pointed out: bad habits are hard to break and getting into the habit of using inline styles for their temporary convenience is a mistake. You might as well go back to using the font tag. There's really no difference.

Having said that, there are occasions where it makes sense to use a simple inline style, but first develop the habit of using stylesheets. Only when you're comfortable with putting everything in a stylesheet should you start looking at shortcuts.

I think that's the general consensus of everyone who posted an answer

Launte