1

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

4
  • 3
    Keep CSS in .css, HTML in .html, C# in .cs Commented Jun 28, 2012 at 11:33
  • Hope this interests you codeproject.com/Articles/14033/… Commented Jun 28, 2012 at 11:35
  • the first question must be if the css must really be created dynamically. Means are the rules changing or every time the same, or do the ids and classnames of the object change? If not I see no reason why not to use a static external css file. Commented Jun 28, 2012 at 11:35
  • yeah sorry Sven ... probably didn't make it that clear. The CSS must be dynamically generated from the object properties. Commented Jun 28, 2012 at 13:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

cheers ... looks interesting so will look into this approach.
0

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.

1 Comment

We're creating our own controls which do very specific things. I've now seen a method of programatically adding in a reference to the css file link so this can be generated and linked during run time.
0

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.

2 Comments

with css string i mean the metatag <link rel="stylesheet" type="text/css" .... to include a css file in a page
Thanks ... found a page which explains how to link a css file at run time which is great, but I'm going to need a simple way to generate the css from the object properties.

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.