Need Dynamic Styles

I need to create a box that contains different content (e.g. Stats, Facts, Quotes) which is database-driven, so the content will change.

The styles need to also change dynamically.

For instance, in all cases let’s say I have a rounded box that is 50 x 50 pixels, however,…

For for fact #1, I want a white background and blue font.

For quote #2, I want a tan background and dark-brown border and font.

For stats #3, I want a black background and white font.

Is there some way I can set up the styles so I can change them dynamically?

I was thinking of somehow storing a pre-defined style in the database along with the content, and then my PHP could grab that and turn it into the appropriate HTML/CSS.

Would this make sense…

<div id='dynamic-box' class='box-background' class='box-font' class='box-type'>Content Here</div>

No.

This would though.

<div id='dynamic-box' class='box-background box-font box-type'>Content Here</div>
2 Likes

Okay, thanks.

On a related note, I am starting to see that it will be trickier to come up with a set of styles for marking up the facts/stats/quotes.

Would it be bad if I hard-coded inline styles in with the text in my database?

Yes :slight_smile: .

So what is the alternative?

Move the styles to a class.

@RyanReese,

I am trying to create an info box that has rotating content. The problem is the content is different size, so I think I need to hard-code the styles tot he specific text.

For example…

Info #1:

35
Number of pizzas avg American eats each year. 

Info #2:

10,399
Avg number of text messages a teen sends each year.

Info #3:

7
Continents on Earth.

Each of those would be in a fixed 50 x 50 div box. Depending on the number of digits in the number on the first line, I would need a different style. Likewise, if the text is short like in the last example, I could use larger font.

Since the PHP function I am writing returns a random fact/stat, I’m not sure the most efficient way to do the CSS.

Follow me?

'because I thought it may be useful:

Demo: Need dynamic Styles

Edit:
I am confused about the box being 50 x 50 pixels.

Thanks @John_Betong. Still kicking ideas around…

1 Like

Is there a limit to how many classes I can combine together?

Could I have up to 5 or 6?

I don’t know if there is an effective limit, but take a look at a WordPress site’s mark-up.
So many classes it can make your head spin.

Anyway, I think it’s safe to say you can use as many as you will reasonably need.

For what I am working on now - and really a lot of my websites - it seems like if I created styles for different things like Size, Color, etc then I could more easily combine styles to get what I want without a whole bunch of “one-off” styles which is what I have now…

Agree or disagree?

Nope.

Sounds like your heading to making your own styling framework at this rate. In that case, I’d rather see you use a pre-made one.

Please tell me more…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.