在php中动态更改css?

时间:2013-10-06 14:05:34

标签: php css

我有一个小问题。我可以用这种方式动态改变(使用php)css样式的内容吗?

<?php
               header("Content-type: text/css; charset: UTF-8");

               $color = "red;";

               ?>

               header      {

                           color:<?php print $color; ?>

                           }    

?>

3 个答案:

答案 0 :(得分:3)

当然有可能,为什么不试一试?

以HTML格式链接php css:

<link rel="stylesheet" type="text/css" href="css/name-of-file.css.php">

并在您的css.php文件中放置代码,但没有

<style type="text/css">

所以看起来应该是这样的

 <?php
           header("Content-type: text/css; charset: UTF-8");
           $color = "red;";
           ?>

           header      {

                       color:<?php print $color; ?>

                       }    

答案 1 :(得分:1)

最好使用你的javascript来改变php标签之间的风格。

例如:

<div class="form-group">
     <asp:Label ID="TelFieldLabel" class="col-md-3 control-label" runat="server" Text="Contact No." AssociatedControlID="TelField"></asp:Label>
     <div class="col-md-3">
          <asp:TextBox ID="TelField" runat="server" class="form-control" type="Number"></asp:TextBox>
     </div>
</div>
<div class="form-group">
     <asp:Label ID="EmailFieldLabel" class="col-md-3 control-label" runat="server" Text="Email address" AssociatedControlID="EmailField"></asp:Label>
     <div class="col-md-3">
          <asp:TextBox ID="EmailField" runat="server" class="form-control" type="Text"></asp:TextBox>
     </div>
     <div class="col-md-offset-3 col-md-9">         
          <asp:RegularExpressionValidator Display="Dynamic" runat="server" ID="RegularExpressionValidator1" SetFocusOnError="True" ForeColor="Red" ControlToValidate="EmailField" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Email address is not a valid format.</asp:RegularExpressionValidator>
          <asp:CustomValidator id="CustomValidator2" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please enter either a phone number of an email address." ClientValidationFunction="ContactFields_ClientValidate" OnServerValidate="PrefContact_ServerValidate" />
     </div>
</div>
div class="form-group">
     <div class="col-xs-12">
          <div class="pull-right">
               <asp:LinkButton ID="SubmitButton" runat="server" OnClick="SubmitButton_Click" CssClass="btn btn-primary" OnClientclick="ShowProcessingPopup();"><span class="glyphicon glyphicon-ok"></span> Submit</asp:LinkButton>
          </div>
     </div>
</div>

答案 2 :(得分:0)

你应该试试这个

<?php
echo<<_CSS
<style>
body{
color:red;
}
</style>
_CSS; 
?>