2

I want to change the content of style sheet dynamically like:

 .menu
{
 color: #333333;
 Font-size : 12px;
}

I want to change the color and font size dynamically. How to replace value of color (like #333333 to #ffffff) and font size (12px to 14px) dynamically.

I am finding the way to use variables in stylesheet, and assigning it to attributes that can make my work easy.

Waiting for reply with example.

2
  • Can you Provide your HTML? Commented Jul 24, 2013 at 6:58
  • Write your CSS template, write a ASHX handler, and serve the CSS on demand with your database values in place. Commented Jul 24, 2013 at 9:45

3 Answers 3

1

If you use asp.net, use inline html. Read here

<div style="font-size: <% Response.Write(i)%>">
        Hello World<br />
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

I am not pretty much sure that you can use variables in stylesheets, what I think a way around for it is to generate the style sheet at run time and assign it to the page.

In you asp.net page you can have like:

<head>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <asp:literal id="literalCss" runat="server"></asp:literal>
</head>

and than in your code behind you can use:

private void Page_Load(object sender, System.EventArgs e)
{
// create css file here for the specific user and save it in a folder,
// give a meaning full name to file, if user specific you can append user id in name like
string fileName = "cssFile_" + userid + ".css";
    literalCss.Text = @"<link href='/style/" + fileName + "' rel='stylesheet' type='text/css' />";
}

I hope it can help you out.

1 Comment

You shall make a check as well that if a file is already created of this user than don't create it again.
0

You may use JavaSctipt with JQuery:

$("#myButton").Click(function()
{
    $(".menu").css({"color":"#ffffff","font-size":"14px"});
});

Comments

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.