Is it always bad idea to use inline css for used-once property?
I ha开发者_高级运维ve a table, with 10 columns. I want to control the width of each column. Each column is unique, right now I create an external CSS style for each column:
div#my-page table#members th.name-col
{ width: 40px; }
I know there is a best practice to avoid inline style. I do approve using external CSS for anything look'n'feel related: fonts, colors, images. But is it really better to use external CSS in this case?
It does not incur extra maintenance cost.
It is easier to produce.Cons I can think of:
If you have separate designers and development team - using inline styles will force designers to modify content-file (aspx in my case). It might use more bandwidth.Anything else I've missed?
IMPORTANT: I am asking about only one specific case where style used will ever apply to exactly one element, and is not part of the global-theme, such as width of one particular column.
There are lots of reasons why style=""
is bad. I consider it a bug anywhere I encounter it in my codebase. Here are some reasons:
style=
has higher priority than other CSS selectors making it hard to effect changes in the stylesheet; the stylesheet has to work harder to override these.style=
rules are hard to find in the code when you need to make global changes.- You download this CSS code with every page view.
- If you have multiple stylesheets for the same HTML codebase your style= rules are not part of the stylesheet and thus are unchangeable.
However, if you are generating content and it's difficult to describe this content in a static CSS file, you might also need to generate the CSS to match that content. It is often easier to simply generate style=
rules despite the drawbacks. If, however, your generated content can be easily described in a CSS file, because the generated structure doesn't often change, or because you can easily generate HTML AND a CSS file at the same time, then it's probably better to not use style=
.
Some suggestions for alternatives to style=
that may be appropriate:
- Instead of inline styles, use class names. Works well if you have lots of columns in your table that have the same properties, and which you expect to always have the same properties. Then your stylesheet can remain fixed while your HTML is fluid, since the CSS only references classes. This is probably the approach I'd use.
- Use JQuery or some other javascript library to style your objects programmatically after the page loads.
Easier to produce is not really a valid argument - people like splitting huge chunks of code into smaller chunks - same with code and markup.
However: + No extra HTTP connection (unless you are already downloading a .css file anyway) - Every time the page gets sent, this CSS is sent with => more bandwith - Designers need to modify CSS in Application Code (bad practice)
Usually, you shouldn't - unless it's a really well thought through performance tweak as google does.
精彩评论