0

I have a DIV that I would like to modify the CSS for programmatically in VB.Net/C#.

I know that, for example, I could add style attributes simply by

divMyDiv.Style.add("color","#ff0000")

but I want to add a new CSS class, together with its attributes to the DIV. So in an ideal world I would like to write something like

divMyDiv.Style.add(".smallRedText", "{font-size:10px; color:#ff0000}")

Is this even possible ? Am I missing the bigger picture ?

All help gratefully received :-)

3
  • Is there a reason you don't predefine the classes and just change the class programmaticly? Commented Jan 16, 2014 at 11:17
  • That would work - but how do I change the class programmatically ? Commented Jan 16, 2014 at 11:36
  • This seems to already be answered in the answers below. Commented Jan 16, 2014 at 11:39

2 Answers 2

1

What about:

divMyDiv.Attributes.Add("class", "new-class")
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest that you define the variations in css ahead of time that you know you'll need. Then, you can manipulate the class attribute of whatever control you need to change on the fly.

For example, you could define:

.smallRedText { font-size: 10px; color: #ff0000 }
.smallBlueText { font-size: 10px; color: #0000ff }

Then when comes time to select dynamically:

divMyDiv.Attributes.Add("class", whateverClassWeNeed);

2 Comments

Thanks - but what if I want to, for example, change the definition of smallRedText so that the font size was 11px ?
I am fairly certain that will require JavaScript. I know you can access stylesheets in the DOM through document.styleSheets, but I don't have experience using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.