2

I am working on an application where the user will be allowed to change the font color of header titles.

The header titles use a value from cssClass (the cssClass uses font-size, font-color, font-weight & font-family).

Is there a way that I can allow a custom font-color in the css file itself?

One option would be to eliminate font-color from the cssClass, and use Font-Color in the .aspx page.

2
  • How do you intend on having the user pick the color? Did you want a postback? Did you want to use JavaScript to achive this? Commented Aug 18, 2011 at 13:43
  • javascript would be better... Commented Aug 18, 2011 at 13:45

5 Answers 5

1

There isn't any problem in having color inside your CSS file and also inline on any HTML element, since inline styles will override the color defined on the file.

Imagine your CSS have this:

a#lnkTest { color: #ff0000; }

But you do it on your aspx.vb:

lnkTest.ForeColor = Color.Blue;

Your generated HTML will be:

 <a id="lnkTest" style="color: #0000ff">Bla bla bla</a>

So, the overrided color works without problems. Also, if you want to do it with JavaScript, it works in the same way:

document.getElementById("lnkTest").style.color = "#0000ff";
Sign up to request clarification or add additional context in comments.

Comments

0

Use a label control between the <h1> tags and use System.Drawing.Color to apply the color.

Comments

0

You could use a dynamically created css file, i.e. an aspx page that only output css.

<%@ Page Language="C#" %>

h1 {
font-size: 15px;
color: <%= myUsersColor () %>;
}

2 Comments

How would I call the "h1" styling to my label?
Well your question told about "heading titles", so I assumed h1. You can put wathever selector you want. It was just an exemple of generated css file.
0

Use javascript to dynamic change the style of the element.

Comments

0

If you are letting user's pick just their own font color, it's probably easiest to set the Font-Color attribute or set an inline style attribute on the client side. If you did it on the client side, the interface would be snappier (not requiring a postback for a color change), but you would need to save the user's choice somewhere and set it again on postback.

Here's an example of setting the header colors on the client-side:

With jQuery

function setHeaderColor(color) {
    // The color parameters should be of the form '#000000'
    $('h1, h2, h3').css({ color: color });
}

Without jQuery

function setHeaderColor(color) {
    var hdrs = [].concat(document.getElementsByTagName('h1'))
                 .concat(document.getElementsByTagName('h2'))
                 .concat(document.getElementsByTagName('h3')),
        ii, jj;

    for (ii = 0; ii < hdrs.length; ii += 1) {
        for (jj = 0; jj < hdrs[ii].length; jj += 1) {
            hdrs[ii].item(jj).style.color = color;
        }
    }
}

1 Comment

in your sample, 'h1' is the ID value of the control, correct?

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.