Anthonius
Anthonius

Reputation: 340

Inline css for several elements

I'm trying to use inline css because I have some parameters I need to pass which I can't do it in css files. Everything is fine if I set the style for one element, for example :

<div class="example" style="background-color:#**<?=$model->color?>**">

But because there are lots of elements need that color parameter, can I put all of them in one style? If in css, I do it like this :

.example h1, h2, li, p a {color: red};

I'm trying this in inline css but it doesn't work :

<div class="example" style="h1, h2, li, p a color:#**<?=$model->theme_color;?>**">

Does anyone know how to do this? And may I do it inline?

Upvotes: 4

Views: 225

Answers (4)

Aziz
Aziz

Reputation: 7783

Why not apply classes instead of internal/inline CSS? Something like:

HTML:

<div class="color-<?=$model->color?>">

CSS:

.color-red {
  color: red;
}

Upvotes: 4

Pedram
Pedram

Reputation: 16575

Try this way:

<?php
 echo "
   <style>
     .example h1, h2, li, p a {
       color: #".$model->theme_color."
     }
   </style>
";
?>

Upvotes: 2

dorado
dorado

Reputation: 1525

This should do it

<?php
echo "<style>
    .example h1, h2, li, p a {
        color: $model->theme_color
    }
    </style>";
?>

Another way:

<style>
    .example h1, h2, li, p a {
        color: <?php echo $model->theme_color; ?>;
    }
</style>

Upvotes: 4

potatopeelings
potatopeelings

Reputation: 41065

What you probably need is internal CSS (instead of inline CSS) - that's where you include your CSS within style tags. So something like

<!-- your external CSS files -->
<style>
   h1, h2, li, p a { color:#**<?=$model->theme_color;?>** }
</style>
<body>
   <!-- your HTML -->
</body>

Upvotes: 4

Related Questions