This documentation is archived and is not being maintained.

WebControl.CssClass Property

Gets or sets the Cascading Style Sheet (CSS) class rendered by the Web server control on the client.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Public Overridable Property CssClass As String
Dim instance As WebControl 
Dim value As String 

value = instance.CssClass

instance.CssClass = value
<asp:WebControl CssClass="String" />

Property Value

Type: System.String
The CSS class rendered by the Web server control on the client. The default is Empty.

Use the CssClass property to specify the CSS class to render on the client for the Web Server control. This property will render on browsers for all controls. It will always be rendered as the class attribute, regardless of the browser.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

For example, suppose you have the following Web server control declaration:

 <asp:TextBox id="TextBox1" ForeColor="Red" CssClass="class1" />

The following HTML is rendered on the client for the previous Web server control declaration:

 <input type=text class="class1" style="ForeColor:red">

If you use cascading style sheets (CSS) to customize the appearance of a control, use either inline styles or a separate CSS file, but not both. Using both inline styles and a separate CSS file could cause unexpected results.


On browsers that do not support CSS, setting the CssClass property will have no effect.

How to: Create External Cascading Style Sheets (Visual Studio)Building ASP .NET Web Applications in Visual Studio

The following example illustrates how to use the CssClass property to change the style of a HyperLink control.


The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  

<script runat="server">
    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If HyperLink1.CssClass = "CssStyle1" Then
            HyperLink1.CssClass = "CssStyle2" 
            HyperLink1.CssClass = "CssStyle1" 
        End If 
    End Sub

<html xmlns="" >
<head id="Head2" runat="server">
    <title>CssClass Property Example</title>
    <style type="text/css">
           font: 10pt Verdana; 
           color: Green;

           font: 15pt Times; 
           color: Blue;
    <form id="form1" runat="server">
        <h3>CssClass Property of a Web Control</h3>
        <asp:HyperLink id="HyperLink1" 
            Text="Click here to go to the Microsoft site" 
            Target="_new" runat="server" />
        <p><asp:Button id="Button1" 
            Text="Click to change the CSS style of the link"
            OnClick="Button1_Click" runat="server" />


          font: 12pt verdana; 

          font: 15pt times; 


  <script language="C#" runat="server">

      void Button1_Click(Object sender, EventArgs e) {



  <h3><font face="Verdana">CssClass Property of a Web Control</font></h3>

<form runat="server">

  <asp:HyperLink id="HyperLink1" NavigateUrl="" 
    CssClass="spanstyle" Text="Click here to go to the Microsoft site" 
    Target="_new" runat="server"/>


  <asp:Button id="Button1" Text="Click to change the Css style of the above link"
    OnClick="Button1_Click" runat="server"/>



Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0