0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bootstrap.css をここだけ使いたい 〜 Laravel @scss ディレクティブの応用 〜

0
Posted at

人のサイトをいじっていて、あーここだけbootstrap.css の form-control とか適用したいなぁってときがあります。

でも bootstrap.css を読み込んじゃったら、ページ全体に適用されちゃって困りますね。このdivの中でだけ適用したい、みたいな。

そこで、以前投稿した @scss ディレクティブを応用して、bootstrap.css を指定箇所(具体的には .my-bootstrap 内)でのみ適用する方法を紹介します。

準備

@scss ディレクティブ
を準備します。

設定

route/web.php

Route::get('{scssPath}', function ($scssPath) {

    $cacheDays = 7;
    $scssPath = strtr($scssPath, ['.scss'=>'']);

    $response = \Cache::remember($scssPath, 60 * 24 * $cacheDays,
        function () use ($scssPath) {
            return view($scssPath)->render();
        });

    return response($response)->header('Content-Type', 'text/css');

})->where('scssPath', '(.*).scss');

resources/views/scss/bootstrap.blade.php
@scss
.my-bootstrap {
{!! file_get_contents('/path/to/bootstrap.4.1.3.css') !!}
}
@endscss

hoge.html
<link rel="stylesheet" href="/scss/bootstrap.scss" type="text/css" media="all" />

<input type="text" class="form-control" name="hoge" value="適用されない" >

<div class="my-bootstrap">
  <input type="text" class="form-control" name="hoge" value="適用される" >
</div>

できましたー

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?