简体   繁体   中英

Best Practices: CSS or Themes in ASP.NET?

When should I use ASP.NET Themes, and when should I use CSS? What are the advantages or disadvantages of using one over the other?

I would recommend using CSS over Themes. The reason for this is in CSS you can modify your styles to they work with all browsers. You can do the same thing with themes but Microsoft's designer is notorious for fixing the things that you fixed to make them work on all browsers so its counter productive. Stick to CSS you will spend less time mucking about.

Better together !

But Themes are not a replacement of CSS, or they're not built for the equvalent purpose to the CSS. It's purpose is to define different themes on your application and to change them with a single line. Themes can include CSS files, image files and skins.

With skins, you can define styles for asp.net controls, so it includes complex and complete solution. For example you can define a gridview and define its style and attributes. You can define it application-wide.

So you I think they are better together, but not equivalent to compare.

如果您正在考虑聘请外部设计机构或设计师,那么使用CSS会更好,因为CSS对他们来说是众所周知的 - 因为主题更多是以开发人员/ VS为中心的。

You should combine them. Use your css files in the theme folder for your normal styling of all the html elements in your website (include all the generated elements).

In the skin file of a control, you can set the default css class. Other properties like the layout and default behavior of the elements (sample: calender control) are editable here too.

Skin files are good for all layout specific configuration you can't easily do with css, but with the .net properties of the controls.

Themes come in really handy if you are using Membership ,profiles and personalization. Other than that yes the Visual Studio Designer is notorious. You should use CSS extensively if you have browser compatibility in mind.

As mentioned, they are not mutually exclusive. I've had the occasion to have multiple themes that in themselves contain their own set of CSS/Media/Skin files that are appropriate based on site configuration.

Well i would recommend using both together, i use the theme to set the css classes on controls and then style them in the css files. Example:

Skin:

<asp:CompareValidator runat="server" Text="*" CssClass="Validator" Display="Dynamic" />
<asp:RangeValidator runat="server" Text="*" CssClass="Validator" Display="Dynamic" />
<asp:CustomValidator runat="server" Text="*" CssClass="Validator" Display="Dynamic" />
<asp:RegularExpressionValidator runat="server" Text="*" CssClass="Validator" Display="Dynamic" />
<asp:RequiredFieldValidator runat="server" Text="*" CssClass="Validator" Display="Dynamic" />
<asp:ValidationSummary runat="server" CssClass="ValidationSummary" />

Css:

.Validator
{
    color: Red;   
}

.ValidationSummary
{
    font-size: 0.8em;
}

.ValidationSummary > ul
{
    list-style-type: disc;
    padding: 0 0 0 15px; 
    margin: 0;
}

.ValidationSummary > ul > li
{
    padding: 0;
    margin: 0;
    color: Red;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM