Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Variables and Angular 5

I'm trying to use css varible in my Angular application, inside a child component, and it's just not working. In the browser's Elements tool (I'm using Chrome latest version) the var() doesn't do anything. expamle:

css file:

:root {
  --width: 43.75%;
}

#content {
  display: grid;
  grid-template-columns: repeat(2, var(--width));
  grid-auto-rows: 90%;
  grid-gap: 3px;
  align-content: center;
  justify-content: center;
  border: 2px solid black;
  width: 400px;
  height: 250px;
  margin: 0 auto;
}

And thats how it's shown in the browser: enter image description here

Is CSS Variables can work with Angulat at all? Thank you

like image 493
TalGabay Avatar asked Jan 23 '18 08:01

TalGabay


People also ask

Can I use CSS in angular?

Angular applications are styled with standard CSS. That means you can apply everything you know about CSS stylesheets, selectors, rules, and media queries directly to Angular applications. Additionally, Angular can bundle component styles with components, enabling a more modular design than regular stylesheets.

Can I use variables in CSS?

CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries. A good way to use CSS variables is when it comes to the colors of your design.

Can we use both CSS and SCSS in angular?

With the help of Angular CLI, you can install CSS or SCSS on your project and start working on that in a suitable way. If you are working with the CSS or SCSS in your angular project then it is very easy for you as compared to most other frameworks.

Can I use Sass with angular?

The Angular CLI can help you stylev12 introduced the option to use inline Sass in your Angular components. The CLI now has an option to provide a inlineStyleLanguage and compiles Sass directly from your Angular components into styling.


2 Answers

I know this is old, but a better solution than ::ng-deep is to use :host instead. This will avoid issues later on when ::ng-deep is deprecated.

:host {
  --width: 43.75%;
}
like image 189
J Hutch Avatar answered Oct 18 '22 07:10

J Hutch


Your root selector probably gets also attribute with angular component id.

Try this

::ng-deep :root {
  --width: 43.75%;
}
like image 21
Vlad S. Avatar answered Oct 18 '22 06:10

Vlad S.