简体   繁体   中英

Cannot link to CSS in HTML

I cannot link to CSS in HTML but the paths are right. I've tried everything, even magic isn't helping.

  • HTML path is romanchenko_test_task/templates/hello.html
  • CSS path is romanchenko_test_task/static/first-style.css

hello.html:

<!DOCTYPE HTML>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="/static/first-style.css">
    </head>
    <body>
        <p> Hello world </p>
    </body>
</html>

first-style.css:

body {
     background: blue;
     color: white;
 }

Your relative links are incorrect.

Your two folder paths are:

romanchenko_test_task/templates/hello.html
romanchenko_test_task/static/first-style.css

So to get out of your templates folder, you need to go up 1 level by using ../

Try:

<link rel="stylesheet" type="text/css" href="../static/first-style.css">

Thanks for everyone.

<link rel="stylesheet" type="text/css" href="../static/first-style.css"> 

is linking CSS to HTML right. My problem was that I've forgot to write a handler for static files (*.css), so tornado (python framework) couldn't identify them.

Hendlers should look like:

handlers=[(r'/', MainHandler)],
    template_path=os.path.join(os.path.dirname(__file__), "templates"),
    static_path=os.path.join(os.path.dirname(__file__), "static"),
    debug=True)

The directory you are calling the CSS from is incorrect. You should use C:/romanchenko_test_task/static/first-style.css (the entire directory) to call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM