絶対パス・相対パス

ファイル構成(サンプル)

index.html
├ css/style.css
└ image/logo.png

htmlファイル

絶対パス

<link rel="stylesheet" type="text/css" href="http://www.sample.com/css/style.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<img src="http://www.sample.com/image/logo.png" />
<img src="/image/logo.png" />

相対パス

  • htmlファイルからの相対パスで記述
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="./css/style.css" />
<img src="image/logo.png" />
<img src="./image/logo.png" />

CSSファイル

  • URI (Uniform Resource Identifier) を指定する場合は、url()関数を使用する
  • 指定するファイルへのパスは、ダブルクォーテーション、または、シングルクォーテーションの引用符で囲むが省略することもできる

絶対パス

background-image:url("http://www.sample.com/image/logo.png");

相対パス

  • CSSファイルからの相対パスで記述
background-image:url(../image/logo.png);
background-image:url("../image/logo.png");
background-image:url('../image/logo.png');

メモ

  • 基本的に下位へのパスは相対パスがよい(親ディレクトリ毎移動しても影響がでないため)
  • 公開ディレクトリ直下のCSS等へのパスは絶対パスが良い(階層を移動しても影響がでないため)