简体   繁体   中英

I cant link my css to html

I tried many things by looking at this site, but I could not find one thing. When i try to use this code in my django project, django cant found css. I am new in this and I am very trying to do this right. I apologize for my bad English. Please help me.

 body{ background-image:url(honey.jpg); } ul{ margin:0px; padding:0px; } ul li a{ text-decoration:none; color:white; display: block; } ul li{ float:left; width: 150px; height: 40px; background-color: black; font-size:15px; line-height:40px; text-align: center; opacity: .7; border:1px solid #285189; } ul li a:hover{ background-color: orange; } ul li ul li{ display: none; } ul li:hover ul li{ display: block; } 
 <!DOCTYPE html> <html lang="en"> <html> <head> <title></title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div> <ul> <li><a href="#">Home</a></li> <li><a href="#">Pcelinje drustvo</a> <ul> </ul> </li> <li><a href="#">Tipovi kosnica</a><ul> <li><a href="#">Db</a></li> <ul> <li><a href="#">Lr</a></li> </ul> </ul> </li> <li><a href="#">Tehnike pcelarenja</a></li> </ul> </div> </body> </html> 

您必须在html标记上方包含{% load staticfiles %} ,然后将<link rel="stylesheet" href="css/style.css">替换为<link rel="stylesheet" href="{% static 'css/style.css' %}">

See This,

<link rel="stylesheet" href="css/style.css">

Check where your CSS file is. Check the name of the file. Is the CSS file in the CSS folder? Is the HTML located at the same place as the CSS folder?

Remember,

  • Is the CSS in the same directory as the file referencing it?
  • Is the CSS in a directory below ?
  • Is the CSS in a directory above ?

Relative Paths

Here is all you need to know about relative file paths:

  • Starting with "/" returns to the root directory and starts there
  • Starting with "../" moves one directory backwards and starts there
  • Starting with "../../" moves two directories backwards and starts there (and so on...)
  • To move forward, just start with the first subdirectory and keep moving forward

That being said,

Modify link element,

<link rel="stylesheet" href="{% static "style.css" %}" type="text/css" media="screen" />

Or Adding RequestContext to the response should load the STATIC_URL variable into the template.

Try changing:

from django.shortcuts import render_to_response

def index(request):
    return render_to_response('index.html')

to:

from django.shortcuts import render_to_response
from django.template.context import RequestContext

def index(request):
    return render_to_response("index.html", context_instance=RequestContext(request))

在与HTML文件相同的文件夹中,必须是文件夹名称:css,在此文件夹中放置您的.css文件。

您必须将style.css包含在静态文件夹内,在settings.py文件中添加STATIC_ROOTSTATIC_PATH ,并且应该遵循@subish的建议。

please try below HTML code. Refresh : press ctrl+f5

<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <div>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Pcelinje drustvo</a></li>
                <li><a href="#">Tipovi kosnica</a>
                    <ul>
                        <li><a href="#">Db</a></li>
                        <li><a href="#">Lr</a></li>
                    </ul>
                </li>
                <li><a href="#">Tehnike pcelarenja</a></li>
            </ul>
        </div>
    </body>
</html>

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