I'm trying to update Radio Button colors of an ant design radio mentioned in this link in my REACT App.
I tried using the suggestion in this post to update the colors.
But my colors are not updating. I am not sure what I'm doing incorrectly here.
Also, Its a REACT project and my package.json has a dependency for "antd": "^4.5.0"
and the import for antd.css exists in app.tsx like this
// Global styles
import "antd/dist/antd.css";
This is a code I have in one of the component files of the project.
File name: MyRadio.tsx
import { Radio } from "antd";
import styles from "./mystyles.module.scss";
return (
<Radio.Group
className={styles.toggle}
>
<Radio id="RDC" value="C">
C
</Radio>
<Radio id="RDI" value="I">
I
</Radio>
</Radio.Group>
);
Here is how mystyles.module.scss looks like:
.toggle {
width: 244px;
background: #ffffff;
border: 1px solid #d9d9d9;
box-sizing: border-box;
display: flex;
align-items: center;
float: left;
}
/* Followed suggestion from other post but didnt see colors updating, when I un-comment the following code
.ant-radio-checked .ant-radio-inner {
border-color: red !important ;
}
.ant-radio-checked .ant-radio-inner:after {
background-color: red;
}
.ant-radio:hover .ant-radio-inner {
border-color: red;
}
*/
Update 1
You need to override the below classes. It should work. Use !important only if your css is overridden by the existing classes.
You need to import "antd/dist/antd.css"; to get these in the console and then override the css
https://codesandbox.io/s/nameless-dream-4ojr4
.ant-radio-inner:after {
background: red !important;
}
.ant-radio-checked .ant-radio-inner,
.ant-radio:hover .ant-radio-inner,
.ant-radio-wrapper:hover,
.ant-radio-input:focus .ant-radio-inner {
border-color: red !important;
}
Edit - Included the codesandbox link and the hover css
I can not write comment (too low reputation), so i will write an "answer". You can try to change theme of ant-design https://ant.design/docs/react/customize-theme (This default blue color is primary one). However it requires some changes and new color will be applied globally, so its not too good for existing big projects.
Hi I just started using Material UI and am having a hard time styling the components. I am building a sign in page and would like my Submit button to be all the way to the bottom right. If someone can help me out that would be greatly appreciated because it seems to be inheriting styles from everywhere else but where I would like to!
I have tried adding
textAlign: 'right'
to buttonStyle and that does not work. I have also tried adding
text-align: right;
to my .form-button CSS.
The only thing that affects anything is removing the .App
Login.js
<div className='form-container'>
...
<Button
style={buttonStyle}
className='form-button'
variant='contained'>
Log-In
</Button>
</div>
...
const buttonStyle = {
backgroundColor: '#527354'
};
App.css
.App {
text-align: center;
}
.form-button {
width: 83px;
height: 36px;
box-shadow: 0px 1px 3px #00000033;
}
.MuiButton-label {
color: var(--primary-white);
font-family: 'Open Sans', sans-serif;
}
.form-container {
max-width: 400px;
margin: 2rem auto;
overflow: hidden;
padding: 0 2rem;
}
Another main goal would be to avoid inline styling because I do prefer keeping it within my style sheet. But if not possible or too overly difficult, I will inline style (as I did with the background-color).
As keikai has mentioned in the comment, you may check the Documentation in this link material-ui.com/styles/basics for overriding style.
For 'it seems to be inheriting styles from everywhere else'
I will suggest you to use styled-components instead of global css import, which mess up everywhere. Try this,
npm install --save styled-components
It creates a css class that only apply to the component.
Sample code:
import styled from 'styled-components'
const MyDiv = styled.div`// can be span, section, etc
// add your style here for the div
your div style(optional)
// your class css inside the div
.form-container {
max-width: 400px;
margin: 2rem auto;
overflow: hidden;
padding: 0 2rem;
}
// add more class if you have any
`
Then wrap your component with
// your newly created styled div
<MyDiv>
// component that needs your style
<MyComponent />
</MyDiv>
Your style will only be applied to MyDiv and MyComponent, and nothing else.
It may took awhile to get used to it, but it is extremely useful.
I've been digging through the Ant-Design node_module trying to change the default color and default width of an active tab but have had no luck. Anyone know how to override it?
The problem is that I don't know which element has the border to begin with. Any help is very welcomed.
you can go with:
.ant-tabs-tab.ant-tabs-tab-active {
border-bottom: 2px solid #BF2D30 !important;
z-index: 2;
}
UPDATE
This style will do as expected.
.ant-tabs-ink-bar {
height: 5px;
background: red !important;
}
Check https://pro.ant.design/docs/style#Override-the-component-style on how to override style .
Refer dis answer too Antd: How to override style of a single instance of a component
To figure out on what needs to be changed on your own, Inspect the element in browser.
You need use tabBarStyle props.
See docs: https://ant.design/components/tabs/
I solve this problem with this code:
import './styles.less';
const [tabIndex, setTabIndex] = useState('0');
const borderClass = ['redBorder', 'greenBorder', 'blueGreyBorder'];
<Tabs
className={`tabs ${borderClass[tabIndex]}`}
defaultActiveKey={tabIndex}
onChange={onSetTabIndex}
>
// And in the styles.less:
` .tabs {
margin-top: 17px;
width: 100%;
}
.redBorder{
.ant-tabs-ink-bar {
background-color: #e94747;
}
}
.greenBorder{
.ant-tabs-ink-bar {
background-color: #24ad52;
}
}
.blueGreyBorder{
.ant-tabs-ink-bar {
background-color: #5b708b;
}
}`
with the state we can change the class and use the cascade css to solve our problem
I am attempting to override the default max-width of the snackbar component in Angular Material.
The CSS applied by Angular Material is shown below:
.mat-snack-bar-container {
border-radius: 2px;
box-sizing: border-box;
display: block;
margin: 24px;
max-width: 568px;
min-width: 288px;
padding: 14px 24px;
transform: translateY(100%) translateY(24px);
}
I have tried overriding using the same style in my style.css file but this style is overridden by the default style.
.mat-snack-bar-container {
max-width: 800px;
}
I have found an answer to a similar question but I know the answer to that question is now deprecated (/deep/ is deprecated).
Is there a best practices solution to this?
To do this properly, you need to set the View Encapsulation to None on your component:
#Component({
templateUrl: './my.component.html' ,
styleUrls: ['./my.component.css'],
encapsulation: ViewEncapsulation.None,
})
Then in your component css you can just do this:
.mat-snack-bar-container {
max-width: 800px;
}
From the official docs:
View Encapsulation = None means that Angular does no view
encapsulation. Angular adds the CSS to the global styles. The scoping
rules, isolations, and protections discussed earlier don't apply. This
is essentially the same as pasting the component's styles into the
HTML.
Put css in your styles.scss or styles.css
.snackbar {
max-width: 90% !important;
margin-left: auto !important; // center align from left
margin-right: auto !important; // center align from right
margin-bottom: 1rem !important;
padding: 10px !important; // spacing between the text and boundary
background-color: green;
color: white;
.mat-button-wrapper {
color: black !important; // action text color
}
}
Note: make sure you have set !important with every style, without it, style wouldn't work.
in component.ts
this.snackbar.open(this.resMsg.message, 'OK', {
panelClass: 'snackbar'
})
Verified for #angular/material v7.0.x:
CSS !important modifier does the trick.
Put this is src/styles.scss (the app's global css):
.mat-snack-bar-container {
max-width: 100% !important;
}
Also we tweak its font:
/* Overrides SnackBar CSS in Material Design's .mat-simple-snackbar class */
/* Original sizes: font: 24px, height: 47.952px */
.mat-simple-snackbar {
display: flex;
font-size: 28px !important; // 28px is double, 42px for triple
min-height: 70px !important; // 70px for double, 90px for triple
align-items: center !important;
justify-content: center !important;
}
As of June 30, 2019, using Angular Material 8.0.1 with Angular 8.0.3, the following SCSS and typescript seems to work for overriding the color of the action button in an Angular Material snackbar *without using !important *:
styles.scss (not the extremely long duration, which allowed me to inspect the styling before it disappeared):
$snackBarTextColor: white;
$snackBarBackgroundNormal: #087a51;
$snackBarActionColor: lightgray;
.snackBarInfo {
background-color: $snackBarBackgroundNormal;
color: $snackBarTextColor;
}
.mat-simple-snackbar > span {
font-weight: bold;
}
.mat-simple-snackbar-action {
.mat-button {
.mat-button-wrapper {
color: $snackBarActionColor;
}
}
}
app.module.ts:
import { MAT_SNACK_BAR_DEFAULT_OPTIONS } from '#angular/material/snack-bar';
providers: [
{
provide: MAT_SNACK_BAR_DEFAULT_OPTIONS,
useValue: {
duration: 41000,
horizontalPosition: 'center',
verticalPosition: 'bottom',
panelClass: 'snackBarInfo'
}
}
]
I remember working in a project with web-designers, and they had a money-jar, where devs had to put a coin in if they used the !important statement. ;)
The other solutions did not work for me, unless i set the .cdk-overlay-pane (using material 11):
.cdk-overlay-pane {
width: 100%;
}
.mat-snack-bar-container {
max-width: 100%;
width: 100%;
}
Not sure when Material introduced this (must be a new thing judging by all the answers to this thread), but you can now override the mat-snack-bar-container's styles by passing a parameter in the _snackBar.open(), like this:
component.ts
openSnackBar(message: string, action: string) {
this._snackBar.open(message, action {
panelClass: 'my-custom-container-class',
});
}
component.scss
::ng-deep .my-custom-container-class{
max-width: 100% !important;
min-width: 0% !important;
min-height: 0 !important;
padding: 0 !important;
margin: 32px !important;
box-shadow: none;
}
I'm afraid you still have to use the ng-deep and the !importants; but at least you no longer need to do ViewEncapsulation None.
The way to go.
encapsulation: ViewEncapsulation.None
Here is a stackblick to demonstrate
Angular 10 and without special tricks:
use panelClass when opening the snackbar, for example:
this.snackBar.open("VeryLongTextWithoutThePossibilityOfBreakingAutomatically", "X", {
duration: 0,
panelClass: 'notif-error'
});
duration: 0 is only for debugging.
write this into your style.css
.notif-error {
background-color: red;
}
.mat-snack-bar-container.notif-error {
max-width: 100%;
}
Now because of css-specificity, this will be the most specific rule.
Beware that there should not be space between .mat-snack-bar-container and .notif-error.
This rule will apply to elements which has both the .mat-snack-bar-container and .notif-error classes.
This also works when your .notif-error class is empty.
Using vw works for me,on both bigger and small screen size
.mat-snack-bar-container {
margin-right: auto !important;
margin-left: auto !important;
width: 80vw !important;
max-width: 100vw !important;
}
FYI Starting with Angular Material v15 with the migradtion to MDC, the class .mat-snack-bar-container has been renamed to
.mat-mdc-snack-bar-container
I also had to use some of the new inner MDC snackbar classes to properly recolor the snackbar starting in v15:
SCSS:
// Classname used in the MatSnackBarConfig obj of MatSnackBar.open():
// panelClass: ['my-snackbar-class']
my-snackbar-class.mat-mdc-snack-bar-container {
.mdc-snackbar__surface {
// Background color of entire snackbar:
background-color: red;
.mdc-snackbar__label {
// Color of snackbar text:
color: white;
}
button {
// Color of snackbar button text:
color: white !important;
}
}
}
I want my loadingController wrapper to be shown with a customized css style but the css's rules doesn't apply to the element (the loadingController wrapper).
I have this in my component:
ionViewDidLoad() {
let loader = this.loadingController.create({
spinner: 'bubbles',
content: 'getting data...',
cssClass: 'loadingwrapper'
});
loader.present().then(() => {
//some stuff
...
loader.dismiss();
});
}
and this in my css file:
.loadingwrapper{
width: 77% !important;
height: 15% !important;
color: black !important;
font-size: 1.25em !important;
background-color: aliceblue !important;
border-radius: 10px !important;
}
In spite of doing this (I've even tried whithout "!important"), the changes (none of them) doesn't apply to the loading wrapper and it shows a bit awful.
Not sure where you are applying the css but if you are applying the css in the page component file you going to have a hard time, because the loading controller sits outside the page selector. So if your page component name is Foobar and you have a .scss file foobar.scss
page-foobar{
.loadingwrapper{
// not going to work
}
}
you can either add it globally to your app/app.scss file or ( i think this will work )
.md,.ios,.wp{
page-foobar{
.loadingwrapper{
// styles!
}
}
}
You have to do it globally inside the variables.scss file.
Android
$loading-md-border-radius:10px;
ios
$loading-ios-border-radius: 10px
Windows
$loading-wp-border-radius: 10px
You can see global variable list here.