user11016383
user11016383

Reputation:

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

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

And the following css

h1{
  color: red;
}

I tried adding a class to the h1 and styling it that way but in vain. 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

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

Upvotes: 1

Views: 1158

Answers (4)

fahad.kazi
fahad.kazi

Reputation: 386

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. Where is your css file located if it is inside a folder like a style folder then you would do something like this.

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

Upvotes: 2

Radu
Radu

Reputation: 540

If I am not mistaken you did not link your css file to the index.html, try something like this

<!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.

Upvotes: 5

cdrrr
cdrrr

Reputation: 1112

In the head section you have to link the html file to your css file.

Place the following code within <head> tags:

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

Upvotes: 4

Dhiraj Talele
Dhiraj Talele

Reputation: 11

You need to import the css in the HTML file

Upvotes: 0

Related Questions