Style.css of Angular not applying to certain Component - css

I have a component, when I add the css code to the .css file of that component, it works. But if I move the code to style.css, it's not working.
My html
<div class="content-body mat-elevation-z8">
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="transKey">
<mat-header-cell *matHeaderCellDef mat-sort-header> trans no </mat-header-cell>
<mat-cell *matCellDef="let row">
<a (click)="showDetail(row.url, row.transKey)" >
{{row.transKey}}
</a>
</mat-cell>
</ng-container>
</mat-table>
</div>
My CSS
.content-body a {
cursor: pointer;
text-decoration: none;
color: #4c90c7
}
.content-body a:hover {
color: #346092;
}
.content-body a:visited {
text-decoration: none;
color: #4c90c7
}
However, not all components are not working, below is my file structure. Style.css works on 'dashboard' html, but not 'trans-msg-his-dialog' html. I'm wondering why, if it has something to do with the module.ts of trans-msg.
Can anyone help? Thank you.
File structure :
UPDATE
Following StepUp's comment, I checked Chrome Inspector and found following:
'dashboard' which works:
'trans-msg-his-dialog' which is not working:
However, I'm not sure what the first section is, I can't find them in my css. I'm wondering if it has something to do with Bootstrap?
a:not([href]):not([tabindex]) {
color: inherit;
text-decoration: none;
}
UPDATE2
Computed style is like following, however I can't find 'rgba(0, 0, 0, 0.87)' in any of my css. I also tried to move those a-related css to the bottome of style.css, but no luck.

Try to remove styles from the your.component.css which overlapps in styles declared in styles.css and all global styles should be applied.
Or you override your styles in styles.css by declaring new classes which are placed lower than your desired styles.
Some your global styles are not applied because of CSS specifity rule.
Read a great article at mdn about CSS speicifity.
Try to avoid using !important keyword by using CSS specifity rules. It's almost never a good idea to use !important.
UPDATE:
Chrome Developer tools shows that CSS properties 'trans-msg-his-dialog' are overridden. It can be seen by struck-through lines at CSS properties.
You can see which properties won by clicking Computed style tab:
Or try to move these styles to the bottom of style.css file:
.content-body a {
cursor: pointer;
text-decoration: none;
color: #4c90c7
}
.content-body a:hover {
color: #346092;
}
.content-body a:visited {
text-decoration: none;
color: #4c90c7
}
UPDATE 1:
Now we know that Bootstrap style has too strong selectors and overrides your anchor:
a:not([href]):not([tabindex]) {
color: inherit;
text-decoration: none;
}
We see that if <a> tag does not have href or tabindex attributes, then it will have color: inherit and text-decoration: none;. So try to add href attribute to your anchor tag:
Button

enter image description here
In case anyone encounters this problem in the future, (link is angular.json file). I have seen that several people say just use !important, but !important can come with bugs. So, I figured this out on my own and decided to reorder the styles in the angular.json file. This put precedence on styles.css over bootstrap.
It worked well for me!
SIDENOTE: Also, if you are using bootstrap, realize that bootstrap classes come with some styling already, so that may be why you are encountering problems as well. You are using styles.css but bootstrap class still have the styles on them.

Related

React Router Link changing css

For some reason the React Router Link is significantly changing my css. Is there a way I can remove any styling impact from the Link?
Without Link:
With Link:
This is the code for the Link. It has no style features.
```
<Link to={`/link/${item.id}`}>
```
style={{textDecoration: 'none'}}
Use this Npm : https://www.npmjs.com/package/react-router-bootstrap
import { LinkContainer } from 'react-router-bootstrap'
<LinkContainer to="/foo/bar">
<Button>Foo</Button>
</LinkContainer>
This will work same as the link without effecting your css
Since Link get's transpiled to an <a>, you can use css to style <a> all and change all links color to white:
a {
color: #FFF;
}
a:hover {
color: #00F
}
Or add a .link class to each Link:
<Link to="/" className="link" />
...
.link {
color: #FFF;
}
.link:hover {
color: #00F
}
Interestingly enough, adding css to the inside of the Link item changed the formatting. I copied the css of the images from what they previously were to the styling of the image inside the link and it looks the same.

Unable to apply simple CSS to an angular Materials element

Here's the Stackblitz.
I'm trying to apply the CSS color: blue to the div with class mat-button-toggle-label-content, but its not getting applied.
A similar CSS is getting successfully applied to a parent element called mat-button-toggle-group.
Just apply color to mat-button-toggle and keep it inside mat-button-toggle-group
Working stackblitz
mat-button-toggle-group {
background-color: orange;
mat-button-toggle {
color: blue;
}
}
You can apply the style to .mat-button-toggle-label-content but you need to break Encapsulation.
Component styles are encapsulated. You can't access component's styles(classes, ids) from outside of the component. You need to pierce into that component and inject the styles like below
Note: /deep/ is deprecated and no more recommended. So you can go with above approach. And for more details check Component Styles
mat-button-toggle-group {
background-color: orange;
/deep/ .mat-button-toggle-label-content {
color: blue;
}
}
There are many reason for that !
Your CSS may not be inserted properly into code
The order of material design CSS take over the order of CSS
My solution is that you may need to put !important after color: blue;
it is : color: blue !important;
Just move it to styles.scss and it will work Stackblitz.

Having trouble overriting bootstrap's css

I'm having trouble overriding bootstrap's css code.
I have included my own stylesheet AFTER bootstrap's code, but it doesn't seem to be working.
I know that a soft override like simply having a style.css that reads:
a {
color: #c54bb7; (it's a sort of red)
text-decoration: none;
}
probably won't work as bootstrap has more specific css that has priority over that one. However, you can see in this picture the link text is green because of the simple a{} config in bootstrap, and that's it. Which means mine should have priority, as it is after the bootstrap css, correct? I'm not sure what i am doing wrong here. I want all links to be red.
https://i.gyazo.com/bf06b0a3990927ed019bf65873a84d42.png
You can use '!important' to force overriding.
a {
color: #c54bb7 !important; (it's a sort of red)
text-decoration: none !important;
}
Try !important. !important emphasizes that this specific css should be applied and overrides the bootstrap css.
a {
color: #c54bb7; !important
text-decoration: none;
}
Hope it helped :)
use a wrapper div for your entire html code
like
<body>
<div class="wrapper">
<div class="content_area">
content goes here...
</div>
</div>
and style using the wrapper class,
.wrapper a{
color: #c54bb7;
text-decoration: none;
}

How can I change link color and hover color in Bootstrap version 4?

In my design, I want to change my link and hover color in a specific part. I try my best but I can't do that. I am new in bootstrap. How can I change it for Bootstrap 4? A simple code is here-
<div class="card" style="max-width: 320px;">
<div class="card-header text-center"><h3>Navigation</h3></div>
<div class="card-block">
<ul class="nav navbar-nav" style="font-size: 1.50em;">
<li>Home</li>
<li>Documents</li>
<li>Videos</li>
<li>Gallery</li>
<li>Download</li>
</ul>
</div>
</div>
The CSS code of Bootstrap 4 is compiled with Sass (SCSS) instead of Less now.
Bootstrap 4 ships with a grunt build chain.
The "best" way to customize Bootstrap is using the default build chain.
download and unzip the source code
navigate to the bootstrap (bootstrap-4-dev) folder
run npm install in your console
run grunt dist to recompile your CSS code
To change the colors to can edit both scss/bootstrap.scss or scss/_variables.scss now.
The examples below edit scss/bootstrap.scss, make sure you redeclare the variables at the begin of the scss/bootstrap.scss file.
The color of the .nav-link and nav-link:hover is set by the default colors for the a selectors, you can changes these colors in scss/bootstrap.scss as follows:
$link-color: #f00; //red
$link-hover-color: #0f0; //green
// Core variables and mixins
#import "variables";
#import "mixins";
....
Notice that the above change the colors of all your links. To change the colors of only .nav .nav-link or even .card .nav .nav-link you will have to compile CSS code with a higher specificity. Do not use !important
Also notice that Bootstrap currently is in a alpha state, so you should not use it for production. Variables to declare the colors of the .nav-link do not exist yet, but possible do in future, see also: https://github.com/twbs/bootstrap/issues/18630
To change the color of the colors of all .nav .nav-links in your code use the follow SCSS code at the end of your scss/bootstrap.scss file:
....
// Utility classes
#import "utilities";
.nav-link {
color: #f00; //red
#include hover-focus {
color: #0f0; //green
}
}
To modify the colors of only the .nav-links inside the .cards you should create CSS code with a higher specificity as follows:
....
// Utility classes
#import "utilities";
.card .nav-link {
color: #f00; //red
#include hover-focus {
color: #0f0; //green
}
}
Of course you can also create your own CSS code at the end of the compiled bootstrap.css file. Depending of your needs use higher specificity;
Change all links:
a {color: #f00;}
a:hover {color: #0f0;}
HTML:
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap-4-dev/dist/css/bootstrap.css">
<style>
a {color: #f00;}
a:hover {color: #0f0;}
</style>
Or with higher specificity:
.nav-link {color: #f00;}
.nav-link:hover {color: #0f0;}
Or even:
.card .nav-link {color: #f00;}
.card .nav-link:hover {color: #0f0;}
You can override the bootstrap classes by adding a custom css file like mystyle.css and place it just after bootstrap in the head section:
<!-- Bootstrap CSS -->
<link href="https://fonts.googleapis.com/css?family=Lato:400,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="path/mystyle.css" type="text/css"/>
and inside mystyle.css:
.navbar-light .navbar-nav .nav-link {
color: whitesmoke;
}
.navbar-light .navbar-nav .nav-link:hover {
color: whitesmoke;
}
Two ways (which is actually one):
1. Adding your own styles
Actually bootstrap allows being overwritten in almost every case, therefore if you set something in your own .css it will overrule the style set in bootstrap.css
So adding this to your own .css:
.navbar ul li a {
color:#fff;}
.navbar ul li a:hover {
color:#000;}
You will see it works as a charm.
2. You can go and find everything set in bootstrap.css
I highly discourage you doing so unless it is really necessary, since every styling can be overwritten and it will create a cleaner structure for your styling.
2020 - Google brought me here for something similar.
Bootstrap 4.5
For those who would like to change only the link color or the < a > tag upon hover, just apply a custom class, say hover_black as follows;
Note - my links are white but black when hovered upon
//CSS
.text_white { color: white; }
a.hover_black:hover { color: black !important; }
// HTML
<a class="text_white hover_black"> Link </a>
In my design, I want to change my link and hover color in a specific part.
You are describing 3 things that can be tackled all at once within BS4 with sass. If you have access to the .scss file of your custom project and able to compile it, then I would recommend the following approach...
For this particular case, you can create a custom mixin like so:
#mixin my-variant($bgcolorOff, $borcolorOff, $bgcolorOn, $borcolorOn, $bgcolorAct, $borcolorAct, $txtcolorOff, $txtcolorOn, $txtsize, $txtalign){
#include button-variant($bgcolorOff, $borcolorOff, $bgcolorOn, $borcolorOn, $bgcolorAct, $borcolorAct);
color:#333; /*set a fallback color*/
font-weight:normal; /*customize other things about your like so like*/
text-transform:none; /*customize other things about your like so like*/
text-decoration:none; /*customize other things about your like so like*/
text-align:$txtalign; /*reference parameter values like so*/
}
I am assuming your have hex colors assigned to sass variables already like this:
$m-color-red: #da291c;
$m-color-blue: #004c97;
..etc..
If so, call your mixin from any specific location in your sass file. Sense this is your first time, notice how the following parameter $m-color-white represents the value for $bgcolorOff parameter above. So pay close attention to where you put your colors to help define your custom variant.
.m-variant {
#include my-variant($m-color-white, $m-color-grey-light, $m-color-off-white, $m-color-grey-light, $m-color-blue, $m-color-grey-light, $m-color-grey-light, $m-color-grey-light, 1.200em, 'left');
}
Finally, when you create your buttons, or anchor links, you can add the following to your element structures:
<a class="btn btn-sm m-3 m-variant ">my custom button</a>
4 easy steps. After you do this the first time, every other time is easy. By taking this approach, you take full control of your link and hover colors. Re-use and/or create as many variants as you like.
Hope this helps
Add this to your CSS:
a.nav-link {color:#FFFFFF;} !important
a.nav-link:hover {color:#F00F00; text-decoration:none;} !important
Don't include the important tags at first though, see if there are any conflicts before adding them in. I personally prefer to just do a search & find of the relevant classes and parent divs to clear any conflicts or change the class name I'm using.
I'm just a noob, but Bootstrap 4 has a class called "nav-link" that you use in the link element tag. I was using a dark, primary colored navbar and the links were the same color. This solved it for me.
<ul class="navbar-nav">
<li class="nav-item">
Create New Request
</li>
</ul>
these CSS work for me
.nav-pills .nav-link {
color: #fff;
cursor: default;
background-color: #868686;
}
.nav-pills .nav-link.active {
color: #000;
cursor: default;
background-color: #afafaf;
font-weight: bold;
}
If you just want to make the hover color the same as the non-hover state, use this variable:
$emphasized-link-hover-darken-percentage: 0;

adding style to LinkButton without overriding css

So it looks like if I add style such as background color to LinkButton in my .cs code, it overrides any css I have that applies to it.
is there any way to add style rather than replace it in my code behind? Thanks!
I am using link button as a menu, so active linkButton should have different background color.
so my solution was when the user clicks on the link button in my event handler I do something like:
lnkView.BackColor = System.Drawing.Color.FromName("#369");
But then my hover style which I have in my css will no longer work:
.navlist a:hover
{
color: #fff;
background-color: #369;
text-decoration: none;
}
in my aspx:
<ul class="navlist">
<li><asp:LinkButton ID="lnkView" runat="server">view</asp:LinkButton></li>
<li><asp:LinkButton ID="lnkCreateNew" runat="server">create new</asp:LinkButton></li>
</ul>
EDIT: Your question is unclear, but you appear to be mis-understanding CSS. Adding background-color to the style property will not cause it to completely ignore any CSS rules. Rather, it will override any CSS rules for the background-color property, but will not affect any other rules.
If you don't want to override the background-color property from the CSS rule, add the !important flag to the CSS rule in :hover, like this:
background-color: #369 !important;
Also, change the color so that the change wil be noticable.
Alternatively, you could add a new CSS rule for .navlist a:link .Active with your background color, then add the Active class in code. (lnkView.CssClass += "Active")
By the way, instead of calling Color.FromName, you should write Color.FromArgb(0x33, 0x66, 0x99).
Any inline styles will always override any inherited styles from the head of a document or an external css file. The only other option would be to add a javascript function that overrides the style of the object after DOM ready or Window ready event.
Not sure of your use here, as your question isn't real clear. However this could be an option as well.
Have two css styles:
.navlistafteranaction a:link
{
color: #fff;
background-color: #123;
text-decoration: none;
}
.navlist a:link
{
color: #fff;
background-color: #123;
text-decoration: none;
}
Then in your code behind just switch your CSSClass:
lnkbtn.CssClass = "navlistafteranaction";
This would change the class to have whatever style you wanted after the fact.
If I understand what you want correctly, you want different styles applied based on whether the linkbutton is being hovered over? So, have the style you have but also have:
.navlist a:link
{
color: #fff;
background-color: #123;
text-decoration: none;
}
if you want a third color, for after a link is visited, define that with a:visited. Is that what you're after?

Resources