Linking to an external css file

試著忘記壹切 提交于 2020-01-14 02:42:29

问题


i have been trying to link a css file i created in my local computer to my html code but it doesn't seem to work. Where should we keep the css file we want to link in our html code or how should we link to that file? As an example i am posting this html code:

<!DOCTYPE html>
<html lang="en-GB">
<head>
  <meta charset="utf-8">
  <title>Breeding Dogs—Tips about Alsatians</title>
  <meta name="description" content="How to breed Alsatians, tips on proper breeding and information about common issues with this breed.">
  <meta name="keywords" content="Dogs,Alsatian,Breeding,Dog,Tips,Free,Pet">
  <link rel="stylesheet" type="text/css" media="screen" href="styles.css">
  <link rel="stylesheet" type="text/css" media="print" href="printstyles.css">
  <script src="leaving.js"></script>
</head>
<body>
Test!
<a href="http://dailypuppy.com" onclick="return leave()">The Daily Puppy</a>
</body>
</html>

from this example, if i wanna re-create this where should i be keeping my styles.css file and etc. I am a little confused and could realy use a bit help , thank you.


回答1:


It's dependent on your file structure. Conventionally, if you keep .html files in the root directory, you'd keep style files (.css) in a directory labeled css so your structure would like

/-
 - /img
 - /css
 | --- style.css
 | --- printstyle.css
 - index.html

which means your tag line would read

<link rel="stylesheet" type="text/css" media="screen" href="./css/styles.css">
<link rel="stylesheet" type="text/css" media="screen" href="./css/printstyles.css">

Please note the "./" preceding the directory name. This means it's a relative location and is needed if you're reading your .html files locally (using file:/// instead of serving them via a local server as I imagine you are).




回答2:


Given the example you've posted there, the HTML file and style sheets should reside in the same directory, e.g. Somefolder/index.html and Somefolder/styles.css



来源:https://stackoverflow.com/questions/20982936/linking-to-an-external-css-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!