PHP, CSS dynamic nr of stylesheets?

  • Christian Chat is a moderated online Christian community allowing Christians around the world to fellowship with each other in real time chat via webcam, voice, and text, with the Christian Chat app. You can also start or participate in a Bible-based discussion here in the Christian Chat Forums, where members can also share with each other their own videos, pictures, or favorite Christian music.

    If you are a Christian and need encouragement and fellowship, we're here for you! If you are not a Christian but interested in knowing more about Jesus our Lord, you're also welcome! Want to know what the Bible says, and how you can apply it to your life? Join us!

    To make new Christian friends now around the world, click here to join Christian Chat.
W

wwjd_kilden

Guest
#1
I am playing around with PHP, HTML and CSS...

I keep my header in a separate file and include it.
For now I have two stylesheets (just to make the basics reusable, and to see how it worked out).
One can be dynamically changed (by me naming it in each file)

So my header contains this:

<link rel="stylesheet", type="text/css", href="stylesheets/basicStylesheet" />
<link rel="stylesheet", type="text/css", href="stylesheets/<?php echo $css ?>" />

and each of my files start off with this:

<?php
$css='Stylesheet.css';
require_once("Header.php");
?>


Would it be possible (and would it make sense ) to make it possible to choose to have just one stylehseet or to add one, two, three etc at wish?

I.e. if I have one page that is distinctly different, can I somehow get the header code to work in such a way that I can add a third stylesheet? Or would that just be a waste of time / resources?

Am just trying to figure out if it best to have one large stylesheet with everything, or several small ones that one can add at choice.
 

JasonNosneh

Senior Member
Aug 2, 2015
110
4
18
#2
Typically it is best to keep CSS in a single file because multiple requests are much slower for the user and search engines dock SEO ranking on that stuff too. If you want to write CSS targeting a specific page then just handle writing your selectors correctly. For example, you could have a container div in each page with an id specific to that page. Then start all your selectors referencing that id for that page.
#about-page .some-class {
.....
}

Much more advanced, but if you are wanting to neatly organize your CSS then you could look into something like LESS or SASS compilers which would let you create lots of .less/.sass files and all be compiled into 1 .css file (if you setup it up correctly).
 
W

wwjd_kilden

Guest
#3
Okai, thanks :)

I think I'll keep it simple for now :p but I'll make a note of the less / sass concept in case I get use for it later.
 

breno785au

Senior Member
Jul 23, 2013
6,002
764
113
39
Australia
#4
It's debatable as to whether you should put you're stylesheet links in the footer. The positive is it will no long be render blocking, the negatives you get FLOUT (Flash Of Unstyled Text)

If your server is configured for HTTP/2 I think you are better off breaking up stylesheets, concatenation is obsolete in that respect.

That being said I haven't done any web development for a couple of months now (taking a break) so my info may be abit rusty lol
 
Last edited: