controlling css through php
im about to start work on a new LAMP project.
in general, all the css classes that i make, specially for layout are like this:
.pageBoundaries{
width:999px;
margin:auto;
}
.onethird{
width:333px;
float:left;
display:block;
}
.twothird{
width:666px;
float:left;
display:block;
}
.half{
width:499px;
float:left;
display:block;
}
now , i was thinking that i make a php file to control such things as width (may be color, borders,etc.). just like:
$pageWidth=999;
.pageBoundaries{
width:<?php $pageWidth ?>px;
margin:auto;
}
.onethird{
width:<?php $pageWidth/3 ?>px;
float:left;
display:block;
}
.twothird{
width:<?php ($pageWidth*2)/3 ?>px;
float:left;
display:block;
}
.half{
width:<?php $pageWidth/2 ?>px;
float:left;
display:block;
}
and then i can set headers from this php file so that browser interprets it as a css file. any ideas about this scheme? its pros and cons?
by doing this, i think making multi-colored themes wou开发者_StackOverflow中文版ld also be pretty easy.
Seems OK, just make sure to send all the proper HTTP headers for caching, so that browsers won't download your CSS files on every request.
Also take a look at SASS and LESS. They are two different technologies that solve the same problem (so decide for one of them). What they do is, give you more tools for writing CSS. Stuff like variables, nesting (so you don't have to repeat your selectors all over the place), extending, mixins, functions, etc. Look at the home page of SASS, it gives you a good overview of what it does.
This approach is usually* fine.
Just don't forget to send the right caching headers as well so the resource isn't requested every time.
If you have CSS settings that may change on every page, consider separating those and putting them into the actual HTML document, so the whole style sheet doesn't have to be loaded every time.
* On very high-traffic sites the fact that the style sheet gets parsed by the PHP interpreter on every request is an issue, because it takes up RAM. In that case, you're better off having a PHP script generate static files.
Thoughts
It is an interesting idea, but probably not the best approach. Dynamic CSS through PHP does have some cool advantages, but I wouldn't use it for page width.
Specific Con's:
• Loads slower. You can set cache headers so it only loads the CSS once, but still not the best way. You would have to constantly empty/invalidate the cache, rename the file or add a parameter.
• Also, PHP only would set the page width values on the page load. What happens if the page resizes? (Hence why people usually use javascript instead of PHP for a purpose like you've described)
Good uses of dynamic CSS through PHP:
• If you want to set specific CSS attributes for certain users or based on certain variables set in PHP. (ex. color preferences stored for a user in a database)
When not to use CSS through PHP:
• When you need the CSS to change after the page has loaded. (ex. resize/move an element when the browser window is resized -- not using percentages)
Alternative
A javascript-based approach might be better. A framework like jQuery has some good tools for manipulating CSS as well as accessing the width of elements such as the document or window.
Other CSS Tools:
SASS/SCSS and LESS are also useful. They provide tools for setting variables, etc with CSS.
You must embed your css and php code on same file. One more thing, remember, php is rendered first, as its server scripting, following by client script like html, js or css
The idea is fine,but i think you can do the same thing in a cleaner way using LESS
Pros: You can have your own dynamic theme with this approach
Cons: You have to stop client browser from caching to get the dynamic content, but if you do so, CSS file will be downloaded upon each request, it will take more time to load each page for the client
精彩评论