简体   繁体   English

我无法将CSS链接到html

[英]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. 当我尝试在django项目中使用此代码时,django无法找到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. 检查您的CSS文件在哪里。 Check the name of the file. 检查文件名。 Is the CSS file in the CSS folder? CSS文件在CSS文件夹中吗? Is the HTML located at the same place as the CSS folder? HTML是否与CSS文件夹位于同一位置?

Remember, 记得,

  • Is the CSS in the same directory as the file referencing it? CSS与引用它的文件在同一目录中吗?
  • Is the CSS in a directory below ? CSS在下面的目录吗?
  • Is the CSS in a directory above ? CSS在上面的目录中吗?

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. 或将RequestContext添加到响应中应将STATIC_URL变量加载到模板中。

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. 请尝试以下HTML代码。 Refresh : press ctrl+f5 刷新:按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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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