Change background color of a page using php

前端 未结 3 1718
闹比i
闹比i 2020-12-06 23:28

I have a standard html page so:


..
.
..
and so on

and i have a php script linked to that html page with:



        
相关标签:
3条回答
  • 2020-12-06 23:53

    put your <body> tag inside the if else

    if blablabla {
    
     echo '<body style="background-color:white">';
    }
    else {
     echo '<body style="background-color:orange">';
    }
    
    0 讨论(0)
  • 2020-12-06 23:53

    here is code that i whant to change background of body

    <body bgcolor="<?php echo $name2; ?>">

    0 讨论(0)
  • 2020-12-06 23:55

    It's not necessarily great practice, but it should get the job done:

    if ( your_conditional ) {
      $styleBlock = sprintf('
        <style type="text/css">
           body {
             background-color:%s
           }
        </style>
      ', $yourColor);
      echo $styleBlock;
    }
    

    One good (?) thing about this is that with this technique, you can put your if statement anywhere - it'll act on the body tag regardless of where you put it.

    One caution: if you have additional style rules for the body tag's background color farther down your page, those will take precedence over the one from your if statement.

    0 讨论(0)
提交回复
热议问题