I was wondering if there was a way using php or javascript to see if my webpage will be longer than one page when printed. I have specific elements that need to be at the bottom of the page regardless of how long the content in the middle of the page is, (max one page including footer).
Tried various css elements such as bottom.
Have you tried making a separate 'print.css' file that only alters the way a page looks when it is going to be printed? <link rel="stylesheet" type="text/css" href="print.css" media="print">. This can also be done with an #media print { } tag in your regular css file.
Related
I'm trying to set up a development server used by designers to test CSS before a deployment. The caveat is I want to be able to load HTML / PHP into an iframe, but have a variable passed to it that will allow me to swap the link to CSS from within the iframe. This will emulate the control I have over the production ad feeds that come via iframes.
I attempted a function that returned HTML with a variable css link, but it will not come back as an iframe, of course. The iframe will ensure the included css is the only css that will be used, and that the main page css will not affect the content of the iframe.
How can I load an iframe with a variable CSS link?
The solution is to use an iframe with a query string parameter on the source.
<iframe height="100%" width="100%" frameborder="0" src="/inc/mockads.php?css=<?=($settings['stylesheet']);?>">
which will pass the pre-defined stylesheet to the markup to be iframed. Then, in the mockads.php file, I add the logic to do:
<?php
if ( isset($_GET['css']) && strlen($_GET['css']) )
{
$stylesheet = $_GET['css'];
}
?>
Then the rest of the markup, with the link set like
<link rel="stylesheet" href="<?=$stylesheet;?>" type="text/css" media="all">
I have a stylesheet link that looks like below:
<link rel="stylesheet" href="/example/get_page.php?location=bla.css" id="main_ss" />
get_page.php just gets a URL using file_get_contents():
if (isset($_GET['location'])) {
echo file_get_contents('/example/styles/' . $_GET['location']);
}
I can see that the stylesheet file is being fetched properly (for example the text of the file is showing in firebug when I expend the link tag) but for some reason it is ignored by the browser. If I just fetch the CSS file directly of course everything works.
The code can be seen here: www.specman-verification.com/example/bla.html
Any leads? I'm at loss here.
Add the Content-type header like this (do this before you output anything):
header("Content-type: text/css");
Your code is just trying to load the script get_page.php. To load the CSS file you need:
<link rel="stylesheet" type="text/css" href="/example/bla.css" />
(or similar depending on the actual path to your CSS file). In other words the href attribute needs to specify the path to your spreadsheet file, not the HTML page file.
You need to do it the right way. I understand what you're doing here. You need a good mechanism to dynamically load external CSS and have the result display normal html in the browser output.
Follow the instructions on this url: http://www.warpconduit.net/2009/05/12/dynamically-load-css-and-js-files-using-php/
This will at least get you to have a mechanism to load external css file with php dynamically. You're definitely missing steps in your code.
I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern
I have an PHP/HTML main page, in which I include different other PHP files (I include them within the body of the main page).
What if I want to include a CSS stylesheet into only ONE of the included file?
What if that included file is a PHP-only file? (that is, it doesn't have those <head> tags, where the stylesheet reference is supposed to be)
What happens if I put "head" tags into a PHP file that is included in the body of the main page? I am wondering if putting head tags in the middle of the HTML body tags could cause problems at all, or at least with certain browsers.
Think of it this way, after all of you includes you still get one html file at the end of it. If you are including files in the body of your document, it should only contain tags that fit within the body, not html, head, title, body, etc. Your css file should be loaded in the of the main doc. Something like this
<html>
<head>
<title>Title</title>
<!--
css and js here
-->
</head>
<body>
<!-- php include here ---->
<div>content</div>
</body>
</html>
So after your includes the html should still be valid. you can check that here http://validator.w3.org/
EDIT:
You can included as many css files as you want anywhere in the document but the overall structure should still be as above. But try to consolidate your css files as much as possible. Every external file is one more request the browser has to make, and can slow the load time if you have a lot.
You can add style tags in the middle of your html, it's bad behaviour, but you can and it'll work. So, you can only import css you need depending on what you include to your main php. But, you don't have to put head tags for that.
It is ok if you will put you link your css file in any of your files, also if your file is php, you can put your css before or after the php tag. But for me its better to include your css file on your main page as long as you have a unique design or style for a unique element or id on the file that you want to design.
<link href="style.css" type="text/css" rel="stylesheet">
<?php
Content of your php file here
?>
I have a multiple pages website where I wan't to change some css stuffs.
So my index.php?p=page points to various pages but on every page I also want to adjust some css like the color of the currently active menu item(li) etc.
What is the best way to achieve this? Should i just make a php var on each page?
One way to handle this is to put a class on the BODY tag for each page, then make different subclasses for the stuff that changes. This way you don't need to feed in any variables from PHP. It's all done via CSS.
<body class="pageOne">
CSS:
.pageOne h1 {
color:#ff0000
}
.pageTwo h1 {
color:#000000
}
You should have the CSS on an external file, and link it using a <link> tag, like so:
<link rel="stylesheet" type="text/css" href="path_to_stylesheet.css">