简体   繁体   English

单独文件中的CSS不适用于index.html

[英]Css in a separate file doesn't apply to index.html

I am new to html and I tried to create my webpage with the html as follows 我是html的新手,我尝试使用html创建我的网页,如下所示

<!DOCTYPE html>
<html>
  <head>
    <title>My webpage</title>
  </head>
  <body>
    <h1> My web page </h1>
  </body>
</html>

And the following css 和以下css

h1{
  color: red;
}

I tried adding a class to the h1 and styling it that way but in vain. 我试图将一个类添加到h1并以这种方式样式化,但是徒劳无功。 If i do the css in style tags inside my html file it works but i want to have it in a main.css file 如果我在html文件中的style标签中执行css工作,但我希望将其保存在main.css文件中

I want the h1 to have the color red, also I would want the css in a separate file, no the same one as my html so that it will be clean 我希望h1的颜色为红色,也希望将css放在一个单独的文件中,与我的html不在同一个文件中,这样它才是干净的

If I am not mistaken you did not link your css file to the index.html, try something like this 如果我没记错的话,您没有将CSS文件链接到index.html,请尝试这样的操作

<!DOCTYPE html>
<html>
  <head>
    <title>My webpage</title>
    <link rel="stylesheet" type="text/css" href="./main.css">.
  </head>
  <body>
   <h1> My web page </h1>
  </body>
</html>

But try to replace ./main.css with the actual path of your main.css reffer to this for more info. 但是,试图取代./main.css与你的main.css reffer到的实际路径获取更多信息。

In the head section you have to link the html file to your css file. head您必须将html文件链接到css文件。

Place the following code within <head> tags: 将以下代码放在<head>标记中:

   <link rel="stylesheet" href="[the path to your CSS file]">

It is usually this tag 通常是这个标签

<link rel="stylesheet" href="yourfilename.css">

But I think the problem you might be facing is the path of your css. 但是我认为您可能面临的问题是CSS的路径。 Where is your css file located if it is inside a folder like a style folder then you would do something like this. 如果css文件位于样式文件夹之类的文件夹中,则该文件位于何处,那么您将执行以下操作。

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

您需要在HTML文件中导入CSS

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

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