Tim
Tim

Reputation: 318

Programmatically writing CSS in .NET

We're displaying objects within a web-page which have basic height and width properties. We can generate the HTML objects utilising the HtmlTextWriter object and plan to apply the Height and Width properties by referencing the object's ID in embedded css class at the top of the class.

#bob
{
   height: 20
   width: 20
}

...

<div id="bob"></div>

1) Firstly, is this a good approach or should we be considering another approach like inline css ?

2) There doesn't appear to be anything written in the surrort of rendering css within the .net framework. Does anyone know of anything which might be of assistance in rendering css notation with the .net framework. Is there an equivalent to HtmlTextWriter ?

3) Do you know of any tools which can be of assistance in generating css from code ?

Kind Regards

Upvotes: 1

Views: 2203

Answers (3)

JohnnBlade
JohnnBlade

Reputation: 4327

Try almost not to use inline css and you can use a literal that has the css string in it, and let the htmlTextWriter write it.

Upvotes: 0

Ashwin
Ashwin

Reputation: 12411

Not sure if you can do it in .net. But I am doing it in jquery as below. I hope this is someway useful.

$.rule("#bob {height:"+ht+"}").appendTo('style');

When this is generated then you can check in your dom that some css has been added to style element.

It is better than inline css. It works for the webpage which is displayed on the browser and not physically written to the file.

More info - here

It works in IE8, FF and Chrome when I checked.

Upvotes: 1

user1921
user1921

Reputation:

Why are you writing out HTML, does one of the existing controls not provide what you need?

In general, CSS should be in a .css file.

It's not really possible to answer if this is a good approach since you haven't explained the intent of the code.

Upvotes: 0

Related Questions