8


I'm building a simple angular app with bootstrap. It seems that the base index.html won't load the styles.css. If I put the css code right into html tags it works like a charm. I tried to reboot the app and clear the browser cache, it seems to just ignore the style file. When I wrote the css file for the first time it worked, hence I thought that was a cache problem. I changed nothing of the Angular project base structure.
Here's my code.

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ReagApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <div class="container">
    <app-root></app-root>
  </div>
</body>
</html>

styles.css

body {
  background-color: #d9d9d9;
}

.container{
  width: 70%;
  max-width: 100%;
    background-color: white;
    margin-left: auto;
  margin-right: auto;
}

Here's the version that works instead.
index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ReagApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body style="  background-color: #d9d9d9;">
  <div class="container"
      style="width: 70%;
      max-width: 100%;
        background-color: white;
        margin-left: auto;
      margin-right: auto;">
    <app-root></app-root>
  </div>
</body>
</html>

.angular-cli.json

[other things]
    "prefix": "app",
          "styles": [
            "styles.css",
            "../node_modules/bootstrap/dist/css/bootstrap.min.css",
            "../node_modules/font-awesome/css/font-awesome.css"
          ],
          "scripts": [
            "../node_modules/jquery/dist/jquery.min.js",
            "../node_modules/tether/dist/js/tether.min.js",
            "../node_modules/bootstrap/dist/js/bootstrap.min.js"
          ],
          "environmentSource": "environments/environment.ts",
[other things]

The code is pretty trivial but still.
Thanks in advance.

5
  • What are you using to build your app? angular-cli? Commented Jul 28, 2017 at 14:22
  • 1
    if you are using angular CLI, you have to mention the path in styles array under .angular-cli.json. Commented Jul 28, 2017 at 14:23
  • I did that, I will update the question with the content of .angular-cli.json Commented Jul 28, 2017 at 14:25
  • Where in your folder structure is your styles.css? Commented Jul 28, 2017 at 17:02
  • @DeborahK in the same folder of index.html, src/app. Commented Jul 30, 2017 at 14:31

3 Answers 3

5

Check your angular-cli.json, under "apps" you should see something like this

"styles": [
    "styles.css"
  ]

add it if its not there

Sign up to request clarification or add additional context in comments.

Comments

2

You need to reference your stylesheet in yout .angular-cli.json like this :

"styles": [
"path/to/style.css"
],

Comments

0

Well, in your index.html, I don't see any <link> to that file... are you sure you didn't leave it out? You still need to link the stylesheet, Angular won't do that automatically for you.

Try:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ReagApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <app-root></app-root>
  </div>
</body>
</html>

1 Comment

(-1 for the answer): Angular does it automatically if you are using angular-cli. You just have to specify the styles file path in the angular-cli.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.