Jump to content

[SOLVED] Code to include css in a .php file


sanderphp

Recommended Posts

I've done a few php scripts and db's but I have never incorporatated css.  I'm trying to set some backgrounds and some font colors and wanted to make sure I am doing this correctly.

 

Do I just put my style sheet reference within the php tags like I have listed below?

 

<?php

<link rel="stylesheet" href="/styles.css" type="text/css">

require( "config.php" );

$Quote = new CQuote; 

if( $Quote->Get( $strQuote ) == 0 )
echo $strQuote;

?>

Link to comment
Share on other sites

Below is my styles.css file.  I'm not seeing any errors, but my php file is not using it.

 

body {
background-color: #330000;
}

body, td, th {
color: #FFCC99;
}

h1 {
color: #FF6666;
}

h2 {
color: #FF99CC;
}

h3, h4 {
color: #CC9999;
}

h5, h6 {
color: #FFCCCC;
}

a {
color: #FF0033;
}

form {
background-color: #990000;
}

Link to comment
Share on other sites

have you checked the paths for the css file ? i often had problems with my path to css file from localhost to the webserver.

 

 

<head>
     <link rel="stylesheet" href="/styles.css" type="text/css">
</head>

 

/styles.css

? can't understand this part why do you have a "/" in front of styles.css

Link to comment
Share on other sites

an alternative is always just doing this, especially seeing how small your CSS is:


<?php

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">';

echo 'body {
background-color: #330000;
}

body, td, th {
color: #FFCC99;
}

h1 {
color: #FF6666;
}

h2 {
color: #FF99CC;
}

h3, h4 {
color: #CC9999;
}

h5, h6 {
color: #FFCCCC;
}

a {
color: #FF0033;
}

form {
background-color: #990000;
}';
   
echo '</style></head><body>';



require( "config.php" );

$Quote = new CQuote; 

if( $Quote->Get( $strQuote ) == 0 )
echo $strQuote;

echo '</body></html>';

?>

 

there's no reason why that code above won't work. however, that code doesn't have any of what's defined in your CSS; unless you're providing it in your config.php (which doesn't make sense anyway).

 

what does your config.php consist of?

Link to comment
Share on other sites

That code above will work, but I recommend using this one:

<?php

echo '<head>';
    echo ' <link rel="stylesheet" href="/styles.css" type="text/css">';
echo '</head>';
?>

 

Why? Not only is that code harder to read, meaning that its easier to make errors on, it causes that code to have to be passed through the php parser for absolutely no reason at all.

Link to comment
Share on other sites

Do you guys recommend putting the alignment in the styles.css or just in the php.  All I'm doing is reading a random quote from a file and then displaying in on a page.  I want the quote to be in the center vertically and horizontally.

 

<TABLE HEIGTH=75% WIDTH=75% BORDER="0"  VALIGN="Middle" ALIGN="center">

<?php

echo '<head>';
    echo ' <link rel="stylesheet" href="styles.css" type="text/css">';
echo '</head>';





require( "config.php" );

$Quote = new CQuote; 

if( $Quote->Get( $strQuote ) == 0 )
echo $strQuote;

?>
</table>

Link to comment
Share on other sites

Do you guys recommend putting the alignment in the styles.css or just in the php.  All I'm doing is reading a random quote from a file and then displaying in on a page.  I want the quote to be in the center vertically and horizontally.

 

<TABLE HEIGTH=75% WIDTH=75% BORDER="0"  VALIGN="Middle" ALIGN="center">

<?php

echo '<head>';
    echo ' <link rel="stylesheet" href="styles.css" type="text/css">';
echo '</head>';





require( "config.php" );

$Quote = new CQuote; 

if( $Quote->Get( $strQuote ) == 0 )
echo $strQuote;

?>
</table>

 

I would do it with CSS, but that's just me. I use CSS for probably 98% of my design (whether it be adjusting a small font, or making a floating DIV).

because it looks cooler.

 

Not at all.

 

this is fun. :D lol

 

I always put my HTML in php, unless I'm writing a big paragraph or something. It's good practice with reading it for me. I also put little notes all through it; it makes me feel cool. lol IE: // Initiate body... // Show header...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.