dongwang6837 2013-12-03 19:46
浏览 29
已采纳

使用PHP进行CSS排序包括

I am staring to incorporate PHP in order to streamline my site.

First of all I am using php include to call a header which is used accross all pages.

the header links to a specific stylesheet stylesheet_header, and then the rest of the page uses another stylesheet, stylesheet_content.

The page has this basic layout:

wrap div
php include header
content div
content div
footer div

problem: once the header is called, it's stylesheet (stylesheet_header) is applied to the rest of the page.

I thought the solution would be to call the content_stylesheet after the header, this partly works but alters some position formatting in both the header and the content.

Is there an an obvious solution?

  • 写回答

1条回答 默认 最新

  • duanjiwei1283 2013-12-03 20:05
    关注

    Stylesheets are not scoped based on where they are included in the page, only based on the rules within them. So the following stylesheet rule always applies to all p elements on the page, wherever the rule itself appears:

    p { font-size: 50%; }
    

    The obvious solution to this is to give each container within your page an ID or class which can be used to scope the rules more tightly. Remember that an ID must always be unique across the rendered page, so any block which might be included more than once on the page should be identified by a class instead (this includes things like the content of a search form, even if your current design has it in a unique placement).

    So you might have separate rules like so:

    #header p { font-size: 50%; }
    #main_content p { font-size: 100%; }
    .sidebar_block p { font-size: 90%; }
    

    In general, you'll want to group your style rules by which blocks they apply to, so you will find yourself with lots of rules next to each other with the same prefix. You can save on the redundancy of this by using a server-side CSS pre-processor such as LESS or SCSS (SASS) which supports nested rules.

    LESS was originally developed in JavaScript, and SASS/SCSS in Ruby, but implementations of both are available in PHP, e.g. these from leafo.net. Both would allow you to write this, which would be rendered as the same CSS as the above example:

    #header {
        p { font-size: 50%; }
        // other rules within #header
    }
    #main_content {
        p { font-size: 100%; }
        // other rules within #main_content
    }
    .sidebar_block {
        p { font-size: 90%; }
        // other rules within .sidebar_block
    }
    

    Finally, if you are using an HTML5 doctype (and have taken appropriate precautions for older browsers) you can use "semantic" containers instead of div elements with IDs or classes, so that (along with a CSS pre-processor) you could end up with this (where <header>, <main>, and <aside> are all new elements in HTML5):

    header {
        p { font-size: 50%; }
        // other rules within #header
    }
    main {
        p { font-size: 100%; }
        // other rules within #main_content
    }
    aside {
        p { font-size: 90%; }
        // other rules within .sidebar_block
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)