Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

Adding <!DOCTYPE html> to my code prevents CSS external rules from applying

I have a webpage that I am trying to validate with w3. It says I need to add <!DOCTYPE html> to the page above the <html> tag so I did. As a result, my CSS messed up.

Using the "web developer" in the latest Firefox, I saw that the CSS file is loading over the network just fine, but under the style editor, it says for that CSS file 0 rules even though there should be 25 rules. When I edit the CSS file in the style editor (even simply hitting the enter button on a new line) it applies the CSS file and everything works just fine.

I also tried taking the CSS code and putting it directly into the html file and it works then to.

So the problem is when I load the CSS externally. I have done lots of research and the most common suggestions say:

  1. Adding the switches modes to quirks mode (or visa versa) and to include html,body{height:100%;} in your CSS. (Check and already done, no differences)
  2. Make sure to use the type="text/css" (Check and already done, no differences)
  3. End the link tag with a /> (Check and already done, no differences)

Does anyone have any idea why the web browser is not applying my css code?

Here are some code samples:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>StudiOm Yoga, Jackson MS</title>
    <link rel="stylesheet" href="libraries/jquery-ui-1.12.1.custom/jquery-ui.css">

    <script src="libraries/jquery-3.2.1.min.js"></script>
    <script src="libraries/jquery-ui-1.12.1.custom/jquery-ui.js"></script>

    <script>
        <?php
            if($_SESSION['generalsettings']['showwelcomemessage'] == "checked")
            {
        ?>
        $( function() {
        $( "#dialog" ).dialog();
        } );
        <?php
            }
            else
            {
        ?>
        $( function() {
        $( "#dialog" ).hide();
        } );
        <?php
            }
        ?>
    </script>



    <link rel="stylesheet" type="text/css" href="style/default.php"/>


    </head>

My head section including the beginning html tag and doctype html tag.

body, html {
            height: 100%;
            margin: 0;
            font: 400 15px/1.8 "Lato", sans-serif;
            color: #ffffff;
            }


            a:-webkit-any-link {
                color: inherit;
                text-decoration: none;
                cursor: grabbing;
            }
            a:link {
            color:inherit;
            }

            .bgimg-1, .bgimg-2, .bgimg-3, .bgimg-4, .bgimg-5 {
            position: relative;
            opacity: 0.65;
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;

            }
            .bgimg-1 {
            background-image: url("../images/website/allsmaller2.jpeg");
            min-height: 100%;


            background-repeat: repeat;
            background-position: left;
            background-size: 100% auto;

            }


            .bgimg-2 {
            background-image: url("../images/website/ddog.jpg");
            min-height: 60%;
            }

            .bgimg-3 {
            background-image: url("../images/website/leg.jpg");
            min-height: 60%;
            }

            .bgimg-4 {
            background-image: url("../images/website/hangingwide.jpeg");
            min-height: 60%;
            }

            .bgimg-5 {
            background-image: url("../images/website/yoga5.jpg");
            min-height: 100%;
            }

            .caption {
            position: absolute;
            left: 0;
            top: 50%;
            width: 100%;
            text-align: center;
            color: #000;
            }

            .title
            {
                display:inline;
                background-color: #111;
                color: #fff;
                border-radius: 6px;
                padding: 15px;
                margin: 0% 0%;
                font-size: 25px;
                letter-spacing: 10px;
            }

            .caption span.border {
            background-color: #111;
            color: #fff;
            padding: 18px;
            font-size: 25px;
            letter-spacing: 10px;
            }

            h3 {
                letter-spacing: 5px;
                text-transform: uppercase;
                font: 20px "Lato", sans-serif;
                color: #777;
            }




            .larger
            {
                font-size: 50;
            }
            a:visited
            {
                color:#ffffff;
            }

            div.table {font-size:20px;display: table;border:1px solid white; margin-left: auto; margin-right: auto; width:85%;}
            div.tablerow {display:table-row; border:1px solid white; text-align:center;}
            div.tablecol {display:table-cell; border:1px solid white;}
            div.schedule {padding: 10px;}

            div.table2 {font-size:20px;display: table; margin-left: auto; margin-right: auto; width:85%;}
            div.tablerow2 {display:table-row;  text-align:center;}
            div.tablecol2 {display:table-cell; vertical-align:middle;}


            div.tile 
            {
                display: inline-block;
                max-height: 300px;
                max-width: 200px;

                box-shadow: 5px 5px grey;
                border: 2px solid black;
                margin: 20px 20px;
            }




            #sn
            {
                filter:invert(100%);
            }

            /* Turn off parallax scrolling for tablets and phones */
            @media only screen and (max-device-width: 1024px) {
                .bgimg-1, .bgimg-2, .bgimg-3 {
                    background-attachment: scroll;
                }
            }

            @media only screen and (max-width: 500px) 
            {               
                div.table {display:block; border:1px solid white;margin-left:auto; margin-right:auto; width:75%;}
                div.tablerow {display:block;border:1px solid white;}
                div.tablecol {display:block;border:1px solid white;padding:10px;}

                div.table2 {display:block; border:1px solid white;margin-left:auto; margin-right:auto; width:75%;}
                div.tablerow2 {display:block;border:1px solid white;}
                div.tablecol2 {display:block;border:1px solid white;padding:10px;}
            }

My CSS file that is not being applied.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That JS you have in your body is whacked, the syntax is all wrong. Also that PHP won't process if it's in a straight html file as it will not run through the php parser. I'd reckon that's your issue right there. PHP is a preprocessor that generates html, you cant just put it in an html file and have it run. Since it's in a script tag and in the head, it won't output in the body, but if you use inspector in whatever browser you're using, you'll see it in the script tag in the markup.

The reason the PHP is parsing without the doctype declaration, is I am assuming because whatever server you're running this code on is assuming it's a php file instead of html. If you want to deliver dynamic css via a php file, you could call the php file externally like you would a css file eg: mysite.com/style.php.

Don't forget that if you're delivering dynamic css to users, to practice proper sanitization prior, eg using CSS tidy or some other method of validation and escaping. Ideally if you can do so, do the escaping/sanitizing on save, rather than on delivery, to lower overhead on your server for every call as well as to speed up the delivery to the end user. However, that's somewhat irrelevant if you're caching the css anyways.

Style.php would look something like this.

echo header("Content-type: text/css");
header("Pragma: nocache");
header("Cache-Control: no-cache, must-revalidate");
header("Last-Modified: " . date("D, d M Y H:i:s") . " GMT");
echo $css_stuffz_goes_here;
exit // or die

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...