sanderphp Posted February 15, 2009 Share Posted February 15, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/ Share on other sites More sharing options...
peranha Posted February 15, 2009 Share Posted February 15, 2009 The css link goes in the head of the document. <head> <link rel="stylesheet" href="/styles.css" type="text/css"> </head> Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762916 Share on other sites More sharing options...
sanderphp Posted February 15, 2009 Author Share Posted February 15, 2009 <head> <link rel="stylesheet" href="/styles.css" type="text/css"> </head> <?php require( "config.php" ); $Quote = new CQuote; if( $Quote->Get( $strQuote ) == 0 ) echo $strQuote; ?> So if this doesn't work then the problem is in my styles.css right? Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762921 Share on other sites More sharing options...
alphanumetrix Posted February 15, 2009 Share Posted February 15, 2009 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>'; require( "config.php" ); $Quote = new CQuote; if( $Quote->Get( $strQuote ) == 0 ) echo $strQuote; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762933 Share on other sites More sharing options...
sanderphp Posted February 15, 2009 Author Share Posted February 15, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762938 Share on other sites More sharing options...
fantomel Posted February 15, 2009 Share Posted February 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762978 Share on other sites More sharing options...
alphanumetrix Posted February 15, 2009 Share Posted February 15, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-762983 Share on other sites More sharing options...
sanderphp Posted February 15, 2009 Author Share Posted February 15, 2009 It was the slash in front of /styles.css like fantomel suggested. Thanks for everyones help. This is great stuff. Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763004 Share on other sites More sharing options...
haku Posted February 16, 2009 Share Posted February 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763098 Share on other sites More sharing options...
alphanumetrix Posted February 16, 2009 Share Posted February 16, 2009 because it looks cooler. Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763104 Share on other sites More sharing options...
sanderphp Posted February 16, 2009 Author Share Posted February 16, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763105 Share on other sites More sharing options...
haku Posted February 16, 2009 Share Posted February 16, 2009 because it looks cooler. Not at all. Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763110 Share on other sites More sharing options...
alphanumetrix Posted February 16, 2009 Share Posted February 16, 2009 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. 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... Quote Link to comment https://forums.phpfreaks.com/topic/145325-solved-code-to-include-css-in-a-php-file/#findComment-763568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.