How to apply CSS to the internal elements of an ASP.NET CheckBoxList control

smartcaveman picture smartcaveman · Sep 1, 2011 · Viewed 10.1k times · Source

I need to manipulate the attributes of a label element inside an ASP.NET CheckBoxList control. Each checkbox label needs to have a different class. The best alternative I see is to apply the classes with jQuery after the fact. Does anyone know a better method?

I found this post marginally helpful, however, adding an attribute to the list item only wraps the input element and the label element in a span tag with the denoted attributes.

Answer

Barrie A Sargent picture Barrie A Sargent · Oct 25, 2013

Variation of Billy's second answer, but without server side code:

Assign the class to checkboxlist itself

<asp:CheckBoxList runat="server" id="MyCBL" CssClass="MyClass"></asp:CheckBoxList>

Any CSS you apply to the parent element (in this case the checkboxlist, which renders as a table) can be passed to its child elements:

<style>
    .MyClass label {color: Blue}
    .MyClass input[type='checkbox'] {background-color: Red}
</style>