VS2010 and CSS: what is the best strategy for individually positioning controls

Ok, I have a ton of controls on my page that I need to place separately. I need to set margin here, padding there, etc. None of these specific styles that I want to apply will apply to more than control. What is the practice of betting to determine which level a style is placed at, etc.

Ok my options

1) External CSS file

1A) Using ClientIdMode = Auto (default)

I can assign a unique CssClass value to an ASP.NET control and in an external CSS file create a class selector that will only apply to that control.

1B) Client Client ID = Predicatable

In an external CSS file, I could define what the ID would be for the controls of interest and create an ID selector (#ControlID {Style}). However, I am wary of maintenance issues due to inclusion / removal of parent containers, which could lead to a change in ID.

1C) Client Client ID = Static.

I could choose static ids for the controls to minimize the chances of colliding with auto-generated ids (perhaps by prefixing ID with StaticID_) and use an external stylesheet with ID selectors.

2) I can place the style right on the control. The only downside here, as I see it, is that style information is flushed every time, not cached, which is what I get from using external CSS. Unless the style is reused, I personally don't see much benefit to putting it in an external file, although please explain why if you disagree. Is there a moiré reason why "Is it good to have all the CSS in one place?"

+2


a source to share


1 answer


Definitely use an external CSS file.

The 1 AC options are really personal preference. I would go with ClientIDMode="Static"

as it gives you maximum control over ids and it will make it easier to access the elements with Javascript (if needed). I've always hated the ugly generated IDs in earlier versions of ASP.NET. Using a unique CSS class for each sorta element defeats the purpose of a class that is intended to be used on multiple elements.



To confirm your thoughts on option 2, this is not the best approach. Putting your styles in an external CSS file will cause the file to be loaded once and stored in the cache, instead of having inline styles bloating your HTML that is sent to the client every time.

+2


a source







All Articles