0

I am looking to include or gain access of a css file using another php. I know it's possible but I don't exactly know how to do it, the following is my css file:

 <style type="text/css">
        .titleStyle
        {
            color:black;
            text-align: center;
            font-family: Stencil Std; 
        }
</style>

I am planning to use this file and the style within it in another php or html script. I heard the word include or require might be needed, could someone please help me with this situation? thanks!

pnuts
  • 58,317
  • 11
  • 87
  • 139
paopao33
  • 107
  • 4
  • 13

3 Answers3

1

It's simple:

<?php
  include "css/style.css"; // include the 'style.css' file in a directory
?>

More detail: read-on

Using include:

<?php
  include "css/style.css"; // include the 'style.css' file in a directory named 'css'
  include "style.css"; // include the 'style.css' file in current directory
  include "../style.css"; // include the 'style.css' file in parent directory
?>

Using require:

<?php
  require "css/style.css"; // include and require the 'style.css' file in a directory named 'css'
  require "style.css"; // include and require the 'style.css' file in current directory
  require "../style.css"; // include and require the 'style.css' file in parent directory
?>

Note 1: What's the difference between include and require ?

include includes the file, and it throws an error when the file is not found.
require includes and require the file, and it exit (stop reading) the file when the file is not found.

Note 2: How to use this script in HTML?

It would not be easy to use this script in an .html file. But this does not means it's impossible. The simplest way to do this is to use .htaccess file. You can create a file named .htaccess and put the following content in there:

AddType application/x-httpd-php .html .htm

Note that this might not always work when your/your-hosting-company's computer or server does not support htaccess. And in my opinion, this is not a smart thing to do.

More Info:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
Using .htaccess to make all .html pages to run as .php files?

Community
  • 1
  • 1
Cai Haoyang
  • 192
  • 1
  • 3
  • 16
1

Consider having a separate header file like a Header.php.

HEADER.PHP

<html>

<head>
    <title>TITLE</title>
    <link rel="stylesheet" type="text/css" href="layout.css"/>
</head>

and then include it into the file you wish to

<?php include 'header.php'; ?>
Daksh B
  • 269
  • 1
  • 8
  • 45
0

use

include ('css/style.css');
Kirs Sudh
  • 373
  • 2
  • 15