Tips for integrating Angular 4 with SASS

As I embark on my inaugural project utilizing Angular4 and sass, I am grappling with the best approach for working with sass. My setup involves using angular-cli with a default style configuration set to utilize scss syntax. I have also configured the compilation process accordingly.

However, I have encountered an issue with the styleUrls option in the component, where specific styles are designated for use only within that component.

The dilemma arises when I specify a scss file in a particular component, as it fails to generate its own corresponding .css file or include it within the main .css file generated from the root of the project.

My project structure is organized as follows:

app
    app.module.ts
    app.routing.ts
    home
        home.component.ts
        home.html
        _home.scss
    client
        client.component.ts
        client.html
        _client.scss
scss
    _imports.scss
    colors
        _colors.scss
    ui
        _button.scss
        _table.scss
    //.. more folders and files
styles.scss
index.html

I aim to define the primary css style in the styles.scss file, which will later be injected into the index.html file during the build process. Additionally, I want the ability to generate a separate css file for each individual component, such as home and client, and exclusively apply those styles within their respective components.

Is there a way to achieve this desired functionality? Despite my attempts, I have yet to come across any other resources addressing this specific challenge!

Answer №1

If you're searching for it, what you need is the import directive:

@import "styles.scss";  
@import "css/styles.scss";  
@import url("http://example.com/css/styles.scss");

This function brings in the CSS from different CSS files into the main CSS file, adding each file's contents to the main file according to the order you specify.

Remember that you don't have to include the .scss file extension because SASS takes care of that automatically. You can simply import with @import "file". Also, there's no need to list each file separately for import; you can consolidate them all in a single import statement:

@import "styles.scss", "css/styles.scss", url("http://example.com/css/styles.scss");

You can even nest imports by using the @import within a selector:

.global-nav {  
  @import "nav-bkgd";
}

I hope this information proves useful! :)

Answer №2

To implement styling changes in an Angular 4.4+ project using ng-cli, simply adjust the settings within the .angular-cli.json file as shown below:

{
  ...
  "apps": [
    ...
    "styles": [
        "styles.scss"
      ],
      ...
  ]
  ...
  "defaults": {
    "styleExt": "scss",
    "component": {}
  }
}

If needed, modify the file extensions within each component from .css to .scss like so:

@Component({
    selector: 'app-your-component',
    styleUrls: ['./your-component.component.scss']
})

Answer №3

If you want to use scss in your project, you can specify it during project creation.

ng new my-project --style scss

Answer №4

I encountered a similar issue recently with my left navigation component. It seemed like the SCSS file was not being compiled, despite trying various solutions. I even started a new app using ng new command, but that didn't solve the problem. Eventually, I fixed it by changing my approach and thinking outside the box.

It appears that using the same component selector inside its relative .scss file may cause issues. Instead, wrapping the content in a div with a suitable className and using that className as the root wrapping of the .scss file content is a better approach.

Let me illustrate this with an example:

app/component/leftnav
  |leftnav.scss
  |leftnav.html ==> simply `left nav works!`
  |leftnav.ts ==> selector leftnav
app/
  |app.html
  |app.scss

In the original leftnav.scss file:

leftnav{
    background:red;
}

In app.html:

<leftnav></leftnav>

To resolve the issue, I added a wrapping div with the className .leftnav inside leftnav.html and used it as the root wrapping in my leftnav.scss instead of the component selector.

So my new leftnav.scss contains:

.leftnav{
    background:red; 
}

And it worked flawlessly!

Answer №5

If your project is already up and running, using ng new my-sassy-app --style=scss won't work. Instead, you need to update the default style extension by using ng set defaults.styleExt scss. This will modify the angular.cli.json file as shown below:

"defaults": {
  "styleExt": "scss",
  "component": {
 }
}

Next, go ahead and change all existing css files to use the scss extension. You should notice that now scss is functioning properly. Remember to also update the file extensions in your component definitions from css to scss. Check out the source for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Spaces between dotted outlines featuring a gradient backdrop

What causes the top edge gaps of the dashed border to be filled with blue color? Why are the bottom edge gaps of the dashed border filled with white color? ...even when the background is set as a linear white-to-blue top-to-bottom vertical gradient. ...

Using CSS to add color to the border of a shape

Is there a way to make only the top, shaped part of this polygon have a 2px blue outline? .graph { clip-path:polygon(0 78%, 9% 67%, 32% 77%, 56% 60%, 69% 30%, 88% 40%, 100% 20%, 100% 100%,0 100%); border:none; height:200px; width:100%; ...

Retrieve the top-level property name in an object literal when using *ngFor

I am currently utilizing lodash for grouping items by their 'module' property. The code snippet I am using is as follows: _.groupBy(_.flatten(this.postReleaseArr), 'module'); This code returns the following image: https://i.sstatic.ne ...

Error Encountered when Deploying Angular App on Heroku - Issue with Application, H10 Status Code

I have been struggling to deploy my Angular 7 application on Heroku for a few days now and keep running into Application errors even after a successful build. I recently realized that I had the entire /dist folder in .gitignore, so I removed it, but the ap ...

Is it possible to target a specific element within one of two classes that share the same name using CSS or XPath in Webdriver.IO?

Currently, I am using Webdriver.io and facing a challenge in selecting an element within the "text-fields-container" class. This particular element happens to be a password field, and both classes share the same name. Can someone guide me on how to go abou ...

Tips for properly filling empty spaces in a flexbox layout using HTML

Is there a way to fill in the gap using CSS so that boxes automatically take up the empty spaces, even if they are of different sizes? How can I make sure the boxes adjust automatically as the screen size changes? .c1 { display: flex; flex-wrap: w ...

Tips for positioning the Google Custom Search bar

I am looking to position the Google custom search box on the left side of my website. Currently, I am using a website builder called imxprs and have implemented this code for my custom search box. <script> (function() { var cx = '013012 ...

Bottom-anchored horizontal navigation bar for seamless scrolling experience

Is there a way to create a navbar with horizontal scrolling at the bottom of the page without compromising its scrollability? When I use position: fixed;, the navbar becomes non-scrollable. div.scrollmenu { background-color: #333; overflow: auto; ...

`The search bar and search button`

On mobile, I have a search field and a button working fine. However, when typing in something and hitting "search" on the phone's keyboard, it leads to a 404 error. But clicking the "search" button works as expected. I would like to be able to hit "se ...

While translating in Angular's dark mode, I noticed that the background property disappears from the card-content

`I implemented a toggle-switch in my Angular project using <mat-slide-toggle></mat-slide-toggle> to switch between dark mode and light mode. However, when I switch to dark mode and translate my application, the background property of the card-c ...

retrieving and presenting information stored in a Firebase repository

I am currently working on retrieving data from a firebase database and storing it in an array called courses that I have declared within my class. Here's the code I have so far: import { AngularFireDatabase, AngularFireList } from 'angularfire2 ...

Displaying a single div while concealing the remaining two divs upon pressing a button

There are 3 hidden DIVs on a webpage. Clicking on a heading should display one DIV while hiding the other 2. Below is the code I used to achieve this, but it doesn't work properly on all browsers like iPad and iPhone. Is there a more efficient way to ...

Sort slider items using Show/Hide feature in bxSlider

I am using a bxslider on my website with specific settings: $('#carousel').bxSlider({ slideWidth: 146, minSlides: 2, maxSlides: 6, speed:500, adaptiveHeight: true, moveSlides:3, infiniteLoop : false, hideContr ...

Is it possible to generate a 3D building structure using an existing image with Jquery and Css?

Have you checked out this website made in flash? Do you think we could achieve the same using jquery and css? Visit here If you click on any building labeled "Release" You will see a hover animation effect on one of the buildings. I would like to replica ...

Extension for VS Code that displays the properties of a model

Is there a VS Code extension available that allows me to view the properties of a model directly from the HTML markup? For instance, when typing ngModel and then pressing the dot (.) key, it would display its properties similar to how it works in a TypeScr ...

Navigate through the page and once you reach a specific point, scroll the element

I'm feeling a bit stuck on how to achieve this particular effect. Imagine a web page where, as a user scrolls down, a specific element responds to the mouse scroll input by appearing to move along with the scrolling motion. Once that element reaches ...

Ensure that breadcrumb links remain on a single line by using text truncation in Bootstrap 5

For creating breadcrumbs in Bootstrap 5, the documentation suggests the following code: <nav aria-label="breadcrumb"> <ol class="breadcrumb"> <li class="breadcrumb-item"><a href="#">Home ...

Are you experiencing issues with modal contents extending beyond the modal on smaller screens?

I recently installed a modal plugin called blockUI for my image uploading needs. Although I have styled and positioned everything accordingly, I am facing an issue: Whenever I resize the browser screen, switch to my iPhone, or use another screen, some con ...

What are some creative ways to customize radio buttons using images?

I am looking to customize the appearance of my radio buttons by using images for both selected and unselected states. Here is the CSS code I have implemented: input[type="radio"] { background: url("https://example.com/path/to/unselec ...

Swap out the traditional submit button on your form with a stylish Font Awesome

Can anyone assist with replacing the submit button with a Font Awesome icon? I've tried it myself, but the icon is not displaying on the button. Here is what I have attempted so far: <form action="{{route('destroycourse', $course->id) ...