I've added angular material to my project and after creating a custom theme I wanted to change the style of .mat-fab.
_theme.scss:
#use '~#angular/material' as mat;
#include mat.core();
$wb-nightblue: ( ... );
$wb-yellow: ( ... );
$wb-primary: mat.define-palette($wb-nightblue);
$wb-accent: mat.define-palette($wb-yellow, 500, 300, 800);
$wb-warn: mat.define-palette(mat.$red-palette);
$wb-theme: mat.define-dark-theme((color: (primary: $wb-primary, accent: $wb-accent, warn: $wb-warn)));
#include mat.all-component-themes($wb-theme);
.mat-fab {
border-radius: 3px;
}
styles.scss:
/* You can add global styles to this file, and also import other style files */
#import '_theme';
The mat-fab button still doesn't show my custom border-radius, however. Taking a look at the page with the dev-tools I can see that my css-rule exists, but it is overwritten by the default material style. Apparently, angular material adds four <style>-tags to the end of the HTML header, just after my stylesheet gets added by angular, which then overwrite my added style.
...
<link rel="stylesheet" href="styles.css">
<style>/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJhcHAuY29tcG9uZW50LnNjc3MifQ== */</style>
<style>.mat-button .mat-button-focu...</style> // contains a lot of angular material button related styles.
<style>.mat-icon{background-repeat:...</style> // contains some angular material icon related styles.
<style>/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJtYXAuY29tcG9uZW50LnNjc3MifQ== */</style>
</head>
Now this construct makes it of course pretty much impossible for me to overwrite default button styles without resorting to !important. I don't know what the sourceMappingURL styles are doing but I guessed they're responsible for the other two tags being added. I've tried to look for them in my project but couldn't find anything. Google wasn't any help either. If I just remove the styles in the html via developer tools, the buttons then lack the proper material style so they are required, but I'd like to have my styles.css placed at the end of the HTML head, so I can overwrite the parts I want.
I've also checked angular.json for any style entries but the only one is my styles.css, which isn't any surprise, since I'd have other stylesheet links in there instead of the direct <style>-tags.
Is there a way to get my stylesheet to the end of the head?
UPDATE
The reason the below does not work has nothing to do with Angular, but with CSS.
.mat-fab {
border-radius: 3px;
}
Basically, CSS applies styles according to how specific they are.
If you want a style to be applied over another one, you need to be more specific about it.
You can read more on this here.
Now onto possible solutions, which are three:
The important!:
A way to make your styles always apply over another is the use of the important! attribute.
This means that your style will only be overwritten by another style with an important! that is more specific that yours.
Given that Angular Material avoids important! there is little change that this happens. The solution would then be:
.mat-fab {
border-radius: 3px !important;
}
Being more specific with material styles:
Lots of people see the use of important! has an indicator that the CSS was poorly written. An alternative to this is simply being more specific with material on what styles we want to overwrite, like so:
.mat-button-base.mat-fab {
border-radius: 3px;
}
In this case we are using Material's own class to specific that we want to apply our style not just to the mat-fab but to a html element that contains both mat-fab and mat-button-base.
The mat-button-base class is a class that all buttons from Angular Material share.
Define your own class and combine it:
Similar to the previous sugestion, instead of using angular material, you can create your own class and combine it with the mat-fab like so:
.border-3.mat-fab {
border-radius: 3px;
}
And in the html you would have:
<button mat-fab class="border-3">
<mat-icon><!-- Icon here --></mat-icon>
</button>
This approach is clearer if somethings you will use the original material style and sometimes your own styling.
Keep in mind that in all cases, the styles need to be defined in a global style sheet.
According to the Official Documentation if you want to override the style of material component, you should create a file with all your custom styles, them pass it to the styles array of your angular.json.
The above describes how to find it:
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"app-name": {
...
"architect": {
"build": {
"options": {
...
// Add the file here.
"styles": [
// By default, Angular adds the material theme you choose and the src/style.scss file, see below
"./node_modules/#angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"
],
}
}
}
}
}
}
The file you are edditing is related to theming (color palettes and what not).
An example of this is the src/style.scss file. This file is created by default to allow you to create css that will be applied to all HTML Elements and components.
With the above in mind, I would advise that you add your code in the src/style.scss file like below:
/* You can add global styles to this file, and also import other style files */
.mat-fab {
border-radius: 3px;
}
I have Asp.net Core 5.0.1 app with multiple MVC views. I also have a CSS file, generated by an app. I want this file to be unmodified (as it will be changed in future using same app). I want separate CSS file, which styles certain elements (eg input or button) to be styled using classes from the generated CSS. I dont want to write class on each input or button etc element (there are 35 views needs to be styled).
For example if generated file has class dx-theme-text-color I want a CSS file which has something like input { color:.dx-theme-text-color}
How can I achieve this?
To clarify: the question is - how to use a class from one CSS in another by name not copy/pasting values etc
I can only think of #extend from SASS:
.dx-theme-text-color {
border: 1px solid red;
}
input, button {
#extend .dx-theme-text-color;
}
You can use css variables.
define css variavles in global scope:
:root {
--my-custom-color: #000;
}
use variables in every css file like this:
.my-element {
color: var(--my-costum-color)
}
You can also use css pre-proccesors like sass(scss), less and etc.
Description:
I just copy and pasted login page from other module and redesigned it.
Let's say structure is like below now:
HTML
login.html
login_redesign.html
CSS
login.css
login_redesign.css
I am including both css file in some global file in sequence of login.css first and login_redesign.css second.
Senario:
I am working on login_redesign.css
Now, to avoid my changes overriding login.css component (as I am loading login_redesign.css at last), I added
class = "new_login_page"
at top level element of login_redesign.html
login_redesign.html:
<div class = "main new_login_page">
<div>....
</div>
login.html:
<div class = "main">... </div>
Also, added that class in my login_redesign.css file as:
.new_login_page.main {
.header {
width:320px;
}
}
While, original page has as below:
.main {
.header {
width:1020px;
}
}
My concern is as follow:
What if in future if someone add back-ground color property in original css file i.e. login.css as below:
.main {
.header {
width:320px;
}
.body {
.background-color:orange // this line was there for both the codes
.leftHalf {
background-color:white; // new line added
}
}
}
It will give white color to my file's (login_redesign.html's) leftHalf part as well, because there is no background-color property defined inside .leftHalf in lgoin_redesign.css file.
How can I prevent this changes to my file?
One more level of complexity: both login.html and login_redesign.html are using css from root level file 'common.css' which is loaded even before login.css
With this added dependency, I want to block changes from login.html but not from common.css
Thank you for reading though the whole question and the answer may be really simple for this question as I am really just reading through stackoverflow to get my task done on css, which I have not worked with much.
Under wwwroot in the css folder I have created a new css file with just the following class (i'm just trying to change the colour of the navbar for a particular area, not the whole site).
.navbar-inverse .recruiter {
background-color: #278e00;
border-color: #196700;
}
I've minified the file and I've also added the file to the budleconfig.json file:
{
"outputFileName": "wwwroot/css/recruiter.min.css",
"inputFiles": [
"wwwroot/css/recruiter.css"
]
}
in the _Layout file for the area that I want the navbar to override I have made a reference too the minified version as the last css file that is refernced and I've appended the recruiter class to my div tag. When I run the code the default bootstrap still seems to take precedence pressing F12 (Chrome) doesn't show my css file being referenced at all
I am trying to make a global theme for a polymer app.
I've defined some variables inside a theme file like this.
#global-theme.html
<style is="custom-style">
:root {
--default-primary-color: #00BBD3;
--primary-background-color: #FFF;
}
:root paper-button.primary {
color: var(--primary-background-color);
background-color: var(--default-primary-color);
}
</style>
Then in another element I use a paper-button with the .primary class... but no .primary class style is applied.
<paper-button class="primary" id="search" on-click="onSearch">Search</paper-button>
If I just put paper-button.primary definition in a normal .css file then it works except that I obviously can't use the variables when it's not inside the polymer system which defeats the purpose.
This all works fine when in development but doesn't work when vulcanized for production
How do I define the global styles correctly so that any paper-button with .primary class has my custom styles applied from within the custom-style definition that also works when vulcanized?
----- update -----
I've looked into my vulcanized html file (that is built with the default yeoman/polymer starter kit gulp tasks) and replaced my custom styles that were flattened there with a link to the actual element like <link rel="import" href="../custom/my-polymer-theme/my-polymer-theme.html"> it works!!
Why would flattening/vulcanization cause this to happen when it's exactly the same code and how would I get around it?
---- FIXED ----
Problem was a boundary between a mixin & variable when vulcanized...
:root {
/* dark theme mixin */
--dark-theme-colors: {
color: #fff;
background-color: var(--secondary-text-color);
};
--dark-theme-secondary-text-color: var(--divider-color);
...
}
When this was flattened to vulcanized html file it doesn't work any more.
To fix it was as simple as closing off the :root bracket and opening a new one.