开发者

Css ordering, and external style sheet combined with php

开发者 https://www.devze.com 2023-03-30 18:34 出处:网络
Okay, so my first question was is it important to order your css tags, which i discovered via this site that the answer is yes.

Okay, so my first question was is it important to order your css tags, which i discovered via this site that the answer is yes.

What I failed to discover in these response was HOW to order them. For my my case is a little different then where this was vaguely answered elsewhere (from my research anyways).

I am using php include_once tags to insert my header, footer, and body. The purpose of this is to condense the file sizes (optimization) and to organize and separate the code so a noob such as myself doesn't get confused; also it allows me to use theses aspects throughout the site on different pages and just have to change that one file to change things site wide (for example, if i were to change the logo, it would change site wide). So essential my index looks like this so far:

div
php include of header
/div
div
php include of body
/div
div
php include of footer
/div

now I know this is going to need to be fine tuned so the header, body and footer dont cluster up, but I (think that I) have it all covered.

What my issue is is that I've styled the divs in this page (at least the body div) and the divs in the body include file, which is essentially a two column div page with divs inside the two containers for the content.

How do I begin to know how to style this. Also when ever I put the css into a style sheet, half the styles are there, the other half 开发者_C百科aren't, am I supposed to put a link to the style sheet in every included file? And if so how do I do this while still being able to validate, for example I can't put a head tag in the includes, because then there would be four head tags in the index :/ I know this is more than one question but it all ties together into one mess of a problem, so if you could clarify that would be awesome. Thanks!


Generally, what I like is to do things like so:

<?php require_once(parts/head.php); ?>

Page contents in HTML

<?php require_once(parts/sidebar.php); //If present ?>

<?php require_once(parts/footer.php); ?>

The head.php file, includes EVERYTHING:

  • doctype
  • <html> and <head> tags.
  • stylesheets and scripts
  • beginning of <body> and if needed <div id=main-container>.

And it works fine for me. There is no need for you to include multiple instances of style sheets everywhere, only once in the head tag. As for the half style sheets, clear your cache and try again, make sure the file is really whole and not half saved by mistake.


Why don't you just put the head tag in the header include and link to the stylesheet there? Also if you put all your css in one stylesheet and link to it on every page, you've got your styles covered. Just make your files like this: header.php:

<!DOCTYPE /><!-- don't forget this one -->
<html>
<head><!-- your head stuff here --></head>
<body><!-- open the body in this file -->
<div>Your logo goes here</div>

index.php (or any other content file):

<div>Your content goes here</div>

You might also want to put the opening div tag of the content files in header.php and the closing div tag in footer.php

footer.php:

<div>Your footer stuff goes here</div>
</body>
</html>


Keep a separate include for the head. You absolutely should not define styles for the elements contained in other include in their respective files. The styles go in one file which contains the header period. Although it might seem a little inconvenient to specify the styles of elements in different files it is the right way.

This is a over simplified application page:

require_once('header.php'); //Contains the CSS and <head>
require_once('body.php');
require_once('footer.php);
echo header();
$myContent = 'foo'; //Do all processing here
echo body($myContent);
echo footer();

You might also want to look into templating systems


The way you're laying out your file is fine, includes are frequently used to keep code maintainable – this doesn't make you a noob, this makes you organized! In terms of styling, you only need one <head/> element and one reference to the stylesheet.

If you need clarification on how your file should be structured, you want something like this:

<!DOCTYPE html>
<html lang="en">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Page Title</title>

    <link rel="stylesheet" type="text/css" href="path/to/your/stylesheet.css"/>

</head>
<body>

    <?php include 'path/to/your/header/file.php'; ?>

    <?php include 'path/to/your/body/file.php'; ?>

    <?php include 'path/to/your/footer/file.php'; ?>

</body>
</html>


  1. The order of webpage-parts hasn't anything to do with CSS. Instead the order of your contents should be logical, usable and stay the same on every sub-page. You have to decide first, which order of content you may present and style the markup afterwards.
  2. On how to use PHP-includes for combining site-parts see Rikudo Sennin's answer
  3. BTW: "to condense the file sizes (optimization)" PHP-files won't process faster, when minified, splitted or otherwise 'speed optimized'.
0

精彩评论

暂无评论...
验证码 换一张
取 消