向PHP添加CSS [英] Adding CSS to PHP

查看:126
本文介绍了向PHP添加CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我在html中创建了一个普通的页面,并链接了一个CSS页面。使用

 < link href =css / index.css =stylesheettype =text / css/ > 

我想知道如果我想对某些PHP使用相同的CSS方法这一页。有没有办法做到这一点,或者我必须使用style =在标签内吗?



这是我试过...



  #test {
width:75px;
background-color:#FFF;
display:inline-block;
}

<?php
echo'< a href =test.phpid =test>测试< / a>'
?>


解决方案

根据我的理解, CSS样式。



为此,最好直接在标签中使用 style = p>

 < a style =width:<?php echo $ myValue?>> Link< / a> 

如果你想在CSS文件中使用php,你可以创建一个PHP文件,而不是你的CSS档案:



style.css.php



 <?php 

header(Content-type:text / css);

?>

.style {
width:<?php echo $ myvalue; ?
}

然后您可以从您的html:



page.php

 < link href =css / style.css.phprel =stylesheettype =text / css/> 

PHP文件的MIME类型将是 text / css 扩展名为.php



如果您希望扩展名为.css,则必须将.css文件添加到要由服务器解释的文件列表中。如果您使用Apache,您可以启用 mod_headers

  a2enmod headers 
service apache2 restart

并创建一个 .htaccess文件以下内容:

 < FilesMatch\.css $> 
SetHandler应用程序/ x-httpd-php
标题集内容类型text / css
< / FilesMatch>

如果这样做,您不需要在css文件中手动设置头。 / p>

Alright so I was creating a normal page in html and have linked a CSS page to it. Using

<link href="css/index.css" rel="stylesheet" type="text/css" />

I was wondering if I wanted to use the same method of CSS on some PHP that is echoed out onto the page. Is there a way to do this or do I have to do it using style="" within the tag?

This is what I've tried...

#test{
     width:75px;
     background-color:#FFF;
     display:inline-block;  
}

<?php
     echo '<a href="test.php" id="test">Test</a>'
?>

解决方案

From what I understand, you want to apply dynamically generated CSS styles.

For this, it is better to use style="" directly in the tag:

<a style="width: <?php echo $myValue ?>">Link</a>

If you want to use php within a CSS file, you can create a PHP file instead of your CSS file:

style.css.php

<?php 

header("Content-type: text/css");

?>

.style {
    width: <?php echo $myvalue; ?>;
}

Then you can link it from your html:

page.php

<link href="css/style.css.php" rel="stylesheet" type="text/css" />

The MIME type of that PHP file will be text/css although the extension is .php

If you want the extension to be .css, you must .css files to the list of files to be interpreted by your server. If you are using Apache, you can enable mod_headers:

a2enmod headers
service apache2 restart

And create a .htaccess file with the following content:

<FilesMatch "\.css$">
  SetHandler application/x-httpd-php
  Header set Content-type "text/css"
</FilesMatch>

If you do this, you don't need to set the header manually in the css file.

这篇关于向PHP添加CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆