importing CSS into a HTML does not work

2020-05-10 19:11发布

问题:

I am trying to @import CSS file into an HTML but it does not work. I did try linking path, It is not working either.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Lesson</title>
<meta charset="utf-8">
<style rel="stylesheet">
@import url("U5.css");
</style>
</head>
<body>

<script src="assets/script/U5.js" ></script>

</body>

But this format seems working as "http:....." ;

  • U5-LättJS
  • href="http://127.0.0.1:54149/assets/pages/U5-JS.html"
    

    Can I convert path to http so it can work? Thank you.

    click for validated code picture

    回答1:

    If your CSS file is in the same directory use this:

    <link rel="stylesheet" type="text/css" href="./U5.css">
    

    If this HTML file is at the root use:

    <link rel="stylesheet" type="text/css" href="/assets/pages/U5.css">
    

    Use https://www.w3schools.com/Tags/tag_link.asp for more information on linking to external style sheets



    回答2:

    Docs:

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style

    https://developer.mozilla.org/en-US/docs/Web/CSS/@import

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Lesson</title>
    <meta charset="utf-8">
    <style>
       @import url("./FILE.css");
    </style>
    </head>
    <body>
    
    <script src="assets/script/U5.js" ></script>
    
    </body>
    </html>
    


    回答3:

    YESSSSSS!! I finally found how to do that. i just copied HTML path and wrote it as style path. Example for HTML;

    href="http://127.0.0.1:54149/assets/pages/U5-JS.html"
    

    so this is the style tag after change;

    href=''http://127.0.0.1:54149/assets/style/U5.css'' 
    

    So it perfectly works. I still do not know what was wrong. Thanks again all efford guys, i appreciated.