LoginSignup
0
0

More than 1 year has passed since last update.

ページごとに別のスタイルシートを読み込みたい

Posted at

やりたいこと

ページごとに違うスタイルシートを読み込む。
htmlからPHPにした際に、ヘッダーはheader.phpにまとめたので、動的に読み込むスタイルシートを変更させたい。

  • index.php のとき style.css
  • mypage.php のとき mypage.css

を読み込むようにする。

変数を使ってそれぞれにシート名を代入する

PHPの変数を使って動的に読み込む。

header.php
<link rel="stylesheet" href="../css/<?= $css_file ?>.css">
index.php
<?php
  $css_file = 'style';
  include ('header.php');
?>
<!-- html -->
mypage.php
<?php
  $css_file = 'mypage';
  include ('header.php');
?>
<!-- html -->

これでページごとに違うスタイルシートを読み込むことができた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0