简体   繁体   中英

Combine my css files into one main css file with php

I was wondering if someone could help me out. I have many different css files so I have a nice overview for myself. I was wondering if I could automatically combine all these css files into one big main-css.css file with php. As a temporary solution I have used <?php foreach (glob("css/*.php") as $css) {echo "<link type='text/css' rel='stylesheet' href='$css'>\n";}?> in my index.php. This will take all my css files and echo them in the header of my index.php file. I have read that it is better to have one main css file so the website will run faster, is this true? If so, what is a nice way to automate the merging of all my css files? So that I only have to use <link type='text/css' rel='stylesheet' href='main-css.php'> .

Current output on the index.php file:

<link type='text/css' rel='stylesheet' href='css/reset/reset.css'>

<link type='text/css' rel='stylesheet' href='css/banner-style.php'>
<link type='text/css' rel='stylesheet' href='css/button-style.php'>
<link type='text/css' rel='stylesheet' href='css/cookie-style.php'>
<link type='text/css' rel='stylesheet' href='css/credits-style.php'>
<link type='text/css' rel='stylesheet' href='css/font-style.php'>
<link type='text/css' rel='stylesheet' href='css/form-style.php'>
<link type='text/css' rel='stylesheet' href='css/header-style.php'>
<link type='text/css' rel='stylesheet' href='css/html&body-style.php'>
<link type='text/css' rel='stylesheet' href='css/preloader-style.php'>
<link type='text/css' rel='stylesheet' href='css/price-tag-style.php'>
<link type='text/css' rel='stylesheet' href='css/scroll-top-style.php'>
<link type='text/css' rel='stylesheet' href='css/scrollbar-style.php'>
<link type='text/css' rel='stylesheet' href='css/section-style.php'>
<link type='text/css' rel='stylesheet' href='css/selection-style.php'>
<link type='text/css' rel='stylesheet' href='css/tooltip-style.php'>
<link type='text/css' rel='stylesheet' href='css/wrapper-style.php'> etc...

Expected output on the index.php file:

<link type='text/css' rel='stylesheet' href='css/reset/reset.css'>
<link type='text/css' rel='stylesheet' href='css/main-css.php'> <!--All css files combined-->

Example of a current css file:

<?php  
    header('Content-type: text/css; charset: UTF-8');
    include 'variables/variables.php'; 
?>

/* ======================================================
    ► HEADER
   ====================================================== */

#header{
    height: <?= $headerHeight; ?>;
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 99;
    background-color: <?= $headerBackgroundColor; ?>;
    border-bottom: <?= $headerBorder; ?>;
    box-shadow: <?= $headerShadow; ?>;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    transition: all 0.2s;
    box-sizing: inherit;
}

#header-wrapper{
    width: 100%;
    height: 100%;
    max-width: <?= $headerWrapperWidth; ?>;
    margin: 0 auto;
}

#header-container{
    display: inline-flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: center;
    align-content: center;
    height: 100%;
    width: 100%;
    box-sizing: inherit;
    padding: <?= $headerPadding; ?>; 
    transition: all 0.2s;
}

@media only screen and (max-width: 960px){

    #header-container{
        padding: <?= $headerPaddingMob; ?>;
    }

    #header{
        height: <?= $headerHeightMob; ?>;
    }
}

Put this as content of your new script

<?php 
$realList=glob("css/*.php"); // or whatever it really is as for server real path will be needed
foreach ($realList as $css) {
  readfile($css); //in case if you want to write that as text
  // OR
  require_once($css); //in case if you want to load those as scripts
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM