LoginSignup
15
14

More than 5 years have passed since last update.

Bootstrapに負けてCSSが反映されない

Last updated at Posted at 2018-10-12

課題

外部ファイル読み込みのコードを書いているにもかかわらずCSSが読み込めない
BootStrapのコードが反映される

原因

読み込みの順番により決まる優先順位がBootStrapのほうが高かったため。

解決

記述の順番を変えてやる。

views/layouts/application.html.erb
  <head>
    --省略--
  ★Bootstrapの読み込み
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

  ★CSSのスタイルシートの読み込み
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
  </head>

CSSは同じスタイルを指定したとき、後から記述したものの方が優先されるようになっている。だからCSSのスタイルシートの読み込みを後に持ってくることで上書きできるようになる。

注意

優先順位は読み込みの順番だけではないので、その都度気を付けていく必要がある。

15
14
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
15
14