Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

controlling css through php

Tags:

css

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 would also be pretty easy.

like image 581
Ahmad Avatar asked Nov 27 '25 00:11

Ahmad


1 Answers

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.

like image 117
Jan Hančič Avatar answered Nov 29 '25 14:11

Jan Hančič



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!