How to use css style in php
They are used for displaying table, tableheader, tabledate. Im new to php css, so im just wondering how to use the above css style in php displaying codes:
echo "
ID | hashtag |
---|---|
$row[0] | $row[1] |
I don’t fully understand your question. You don’t use them differently in PHP than in normal HTML: either include an external stylesheet or embed the stylesheet in your
element11 Answers 11
I guess you have your css code in a database & you want to render a php file as a CSS. If that is the case.
In your html page:
Then, within style.php file:
table < margin: 8px; >th < font-family: ; font-size: ; background: #666; color: #FFF; padding: 2px 6px; border-collapse: separate; border: #000; > td < font-family: ; font-size: ; border: #DDD; >
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. more info : http://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS is not a programming language, and does not have the tools that come with a server side language like PHP. However, we can use Server-side languages to generate style sheets.
table < margin: 8px; >th < font-family: Arial, Helvetica, sans-serif; font-size: .7em; background: #666; color: #FFF; padding: 2px 6px; border-collapse: separate; border: 1px solid #000; >td echo ""; echo "ID hashtag "; while($row = mysql_fetch_row($result)) < echo "$row[0] $row[1] \n"; > echo "
"; ?>
Just put the CSS outside the PHP Tag. Here:
table ID hashtag $row[0] $row[1] \n"; > ?>
Take note that the PHP tags are .
Try putting your php into an html document:
Note: your file is not saved as index.html but it is saved as index.php or your php wont work!
I didn’t understood this Im new to php css but as you’ve defined your CSS at element level, already your styles are applied to your PHP code
Your PHP code is to be used with HTML like this
Also remember, you did not need to echo HTML using php, simply separate them out like this
Absolute easiest way (with your current code) is to add a require_once(«path/to/file») statement to your php code.
Also, as an aside: the opening on the end, and the closing ?> php tag does not start with < . Weird, but true.
You can also embed it in the php. E.g.
hope this help for anyone in the future.
This doesn’t solve the problem, since it applies the style to only one element in particular. OP noted that they have a move complex css
I don’t know this is correct format or not. but it can solved my problem with removing type=»text/css» when insert css code in html/tpl file with php.
Please, don’t post your code/error messages as images. We are not able to copy/paste from images and secondly search engines are unable to index that information. So please make sure that any textual information is actually provided in text form.
css :hover kinda is like js onmouseover
row1 < // your css >row1:hover < color: red; >row1:hover #a, .b, .c:nth-child[3]
not too sure how it works but css applies styles to echo’ed ids
This does not answer the question. The question is about how to incorporate css into a php file, not on the use of :hover . If you have remarks for OP, leave a comment
I had this problem just now and I tried the require_once trick, but it would just echo the CSS above all my php code without actually applying the styles.
What I did to fix it, though, was wrap all my php in their own plain HTML templates. Just type out html in the first line of the document and pick the suggestion html:5 to get the HTML boilerplate, like you would when you’re just starting a plain HTML doc. Then cut the closing body and html tags and paste them all the way down at the bottom, below the closing php tag to wrap your php code without actually changing anything. Finally, you can just put your plain old link to your stylesheet into the head of your HTML. Works just fine.
This is the easiest: require_once(«../myCSSfile.css»); as elbrant mentioned.
I had to add at beginning of myCSSfile.css and at the end (normally not needed in a CSS file).
This question is in a collective: a subcommunity defined by tags with relevant content and experts.
Linking CSS in PHP
my CSS file «styles.css» isn’t loading. I’m on a mac and my PHP server is set to be loading from my «PHP» folder on my desktop. Here is the file path to the css folder: /Users/evanbutler/Desktop/PHP/css Code from the PHP doc:
2 Answers 2
Proper way is copy style.css from your desktop to project folder/css. then include css by href=»css/styles.css»
They are four method to link css file into html:
This is a red heading with a black background
You should use a relative path to your css file from your root folder of your working project. Are you using an environment like XAMPP to test your local project on?
If so, your css relative url would be a href=»css/styles.css»
You’ll need some way to use your computer as a server in order to view anything you’re doing with PHP. That means you’ll have to install Apache and Mysql at least (or some other database engine). An easy way to do this is to download a packaged deal, which includes Apache, PHP, and Mysql. You can get these for Windows (check out WAMP or XAMPP), Mac (MAMP), or Linux (LAMP). It makes things a lot easier.
Started using MAMP and figured everything out, just had to change the Apache Port to 8000 and linked the bootstrap file from the document root instead of a website. Thanks for your help!!
You’re welcome. It’s pretty standard to use a development environment to test out your project along the way before you deploy it live. You should turn on error reporting as well so you can debug the stuff as you go.
Include: CSS with .php file extension?
I’d like to wrap a CSS file in PHP. So I write the header for the CSS file and give it a .php file extension, thus. css.php. Will this work if the page is already being used as an include? Or will this new header clash with the frame the page is being included into?
Might I ask what you want to use this css.php file to serve? Some sort of styles that are database generated or something?
5 Answers 5
and then in top of your .css.php file:
Make sure that header is printed at very top of the file. If you have (like I did) an empty line at top it may not work somewhere!
If you have a file called css.php just make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into a bootstrap.php if you haven’t already. A quick example of loading some style info from a databse:
From your other php file, just output the tag to include the css.php, you do not want to use the php include() function for this task!
Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn’t do much good. You could force that to update by adding a get parameter to the href of the link like so:
Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don’t change very often, the first example should work for you quite well.
How to add css files using PHP inside the tag?
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
I’m not expert on php, but I believe the solution would involve some sort of protocol between your php files, so that your index.php would collect on one hand the needed css files, and on the other hand the html which goes into your body tag, from other php files like home.php in this case.
3 Answers 3
You could do it using ob_start() and ob_end_flush() functions e.g.
>', $csspage, $buffer)); > ob_start("loadCSS"); ?> > is just a placeholder that will be replaced with the new value of $csspage defined later in the code, otherwise it will replaced with its initial value (default.css) --> >" />
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page. (add this to )
You can put a variable at the top of an individual script if you need a specific stylesheet:
CSS file references can be placed in the body of your code, if needed.
I'll be blue as soon as linktothecss.css finishes loading!
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven’t been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
css-requirements/home.php:
including css in php from header
I have a header file that is linked to a css file. When I run the header file it displays exactly as I want. I therefore know that the css and header file are working. When I include the header in my index.php the css is ignored and the header displays incorrectly. In the header file I import the css as:
Any ideas what I need to do so that the css is also included so that the page will display correctly? My header.php is inside a header folder, which also includes the css folder.
It sounds like you need to include header/css/style.css since that is the relative location from the index.php page.
As @kainaw points out, when you include, your path changes to header/css/style.css . You probably want to either change the path to the full, absolute url, or else change it to be based on the relative root: /css/style.css
5 Answers 5
I would define a constant BASE_URL and use that in your template header as such:
|--- css | |--- main.css | |--- templates | |--- footer.php | |--- header.php | |--- index.php
css/main.css" type="text/css" rel="stylesheet">
I had the same problem and i do not know what the language makers did. If you want to use a header with css on an index.php or another file you will have to make sure that the header and that file are in the same directory and NOT the header in a subdirectory. With this you can at least work until you find a better answer
include('header.php'); //this works even with the css in it include('header/header.php'); // this does not work when you have a css file in it
Without seeing your project tree, etc it’s difficult to pinpoint exactly the issue. From your question though I gather the header.php file is loading fine, but the CSS file you’re trying to load isn’t.
As above, when the header.php file is loaded, the browser is looking for the style.css file in the header/css/ folder, which probably doesn’t exist.
You’re then within the header folder of your site. Yet I’m guessing the css folder is located at one level above? In which case, I’d bet money on you needing to go back a level as follows within your header.php file:
If the css folder is located one level up further, you could just go up another level:
Failing that, you could revert back to the root level: