Skip Parent Background in Child class while inherit other properties - css

I don't want child class to inherit the immediate parent class background color which has background color as white. Instead it should skip and take background color from parent component which is bluebackground. When I use the transparent background in child class it should show blue color not white. Rest of the property of parent (whitebackground class) should be inherited just not the white background in child class.
I cannot do any changes on Parent component just I can do changes using css.
app component :
<div class="bluebackground">
<app-test>
</app-test>
</div>
Component: test
BlueBackGround Start
<div class="whitebackground">
Some White Background Stuff that I need
<div class="child">
Here i want background which should skip the parent whitebackground and show blue color same as bluebackground class <br />
</div>
</div>
BlueBackGround End
Here is the Css
.child {
font-size: 2rem;
text-align: center;
color: green;
background:transparent;
}
.bluebackground{
background: blue;
font-size: 3rem;
}
.whitebackground {
background:white;
}
StackBlitz
https://stackblitz.com/edit/angular-ivy-hbfvjs

You could try to change the CSS to the following
.child {
font-size: 2rem;
text-align: center;
}
.bluebackground {
font-size: 3rem;
}
.whitebackground {
background:white;
color:yellow;
}
.bluebackground, .child {
background-color: blue;
}
Working example: Stackblitz
Update
There is a dirty way to include the CSS selectors from the app component to the child component.
test.component.ts
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css', '../app.component.css'] // <-- include `app.component.css` here
})
export class TestComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
Now you could use the bluebackground selector in the test component.
test.component.html
BlueBackGround Start
<div class="whitebackground">
Some White Background Stuff that I need
<div class="child bluebackground"> <!-- include `bluebackground` here -->
Here i want background which should skip the parent whitebackground and show blue color same as bluebackground class <br />
</div>
</div>
BlueBackGround End
I've modified your Stackblitz
Update: encapsulation
You could set the encapsulation to ViewEncapsulation.None in the app.component and rename the child selector to bluebackground in the child as well. Try the following
app.component.ts
import { Component, ViewEncapsulation } from '#angular/core';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ],
encapsulation: ViewEncapsulation.None // <-- set `ViewEncapsulation.None` here
})
export class AppComponent {
...
}
test.component.ts
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
test.component.css
/* overwrite/introduce values to the `bluebackground` specific to test component */
.bluebackground {
font-size: 2rem;
text-align: center;
}
.whitebackground {
background:white;
color:yellow;
}
test.component.html
BlueBackGround Start
<div class="whitebackground">
Some White Background Stuff that I need
<div class="bluebackground">
Here i want background which should skip the parent whitebackground and show blue color same as bluebackground class <br />
</div>
</div>
BlueBackGround End
Working example: Stackblitz

Try like this:
<div class="bluebackground">
Blue Background Start
<div class="whitebackground">
Some White Background Stuff that I need
<div class="child bluebackground">
Here i want background which should skip the parent whitebackground and show blue color same as mainbackground class <br />
</div>
</div>
Blue Background End
<div>
Working Demo

Thanks to Michael D for his inputs.
This is how I solved it
I had to move the white background color from whitebackground class to new class called newwhitebackground class.
Html
BlueBackGround Start
<div class="whitebackground">
<div class="newwhitebackground">
Some White Background Stuff that I need
</div>
<div class="child">
Here i want background which should skip the parent whitebackground and show blue color same as bluebackground class <br />
</div>
</div>
BlueBackGround End
CSS
.child {
font-size: 2rem;
text-align: center;
background:transparent;
}
.whitebackground {
color:yellow;
}
.newwhitebackground {
background-color:white;
}
Here is the stackblitz
https://stackblitz.com/edit/angular-ivy-pyhqwr

Related

CSS class properties not applied to react component

I have the following react component:
import "./App.css";
function App() {
return (
<div className="App">
<div className="TopBar">
<div style={{ color: "green" }}>Rooms</div>
<div>Chats</div>
<div>User</div>
</div>
</div>
);
}
export default App;
However, the TopBar class in App.css:
.TopBar {
display: flex;
flex-direction: row;
border-width: 1;
border-color: gray;
}
seems to not be applied. The divs are showing in a column rather than a row. The border isn't showing either. How to fix this?
For the border to render, you may need to first indicate the border style in your CSS. For example: border-style: solid or border: 1px solid gray.
Otherwise, your code should work. Here is a working sandbox using your code. https://codesandbox.io/s/react-component-styling-oy9m2

I have two span tags in a button, here the problem is those two span tags are behaving like a block element?

I am working on a Reactjs project, for that I am using the Bootstrap framework for designing. In that I have one div, in that div I have One button, In that button I have two Span tags. In first span tags I have Icon and In Second Span tag I have text. My problem is In output the text is coming under the Icon, But what I am expecting is Icon and text should be side by side. So try to help me to debug this issue.
This is App.js
import React from 'react';
import './App.css';
import Navbar from './Navbar/Navbar';
import FlightsButton from './FlightsButton/FlightsButton';
function App() {
return (
<div className="App">
<FlightsButton></FlightsButton>
</div>
);
}
export default App;
This is App.css
There is no css in App.css
This is FlightsButton.js
import React, { Component } from 'react'
import './FlightsButton.css'
import ReusableButtons from '../ReusableButtons/ReusableButtons';
class FlightsButton extends Component {
render() {
return (
<div className='container-fluid'>
<div className='row'>
<div className='col-12'>
<div className='one mt-3 d-inline-block'>
<button type='button' className='buttonOne rounded-top'>
<span className='flightStyle'>
<i class="fa fa-fighter-jet fa-sm flightLogo"></i>
</span>
<span className='flightContent pl-2'>
Flights
</span>
</button>
</div>
<div className='two mt-3 d-inline-block'>
<ReusableButtons>
<span className='hotelStyle'>
<i class="fa fa-bed fa-sm hotelLogo"></i>
</span>
<span className='hotelContent pl-2'>
Hotels
</span>
</ReusableButtons>
</div>
</div>
</div>
</div>
)
}
}
export default FlightsButton
This is FlightsButton.css
.buttonOne {
background-color: #008ca8;
border: none;
padding: 0.50%;
padding-left: 0.50%;
padding-right: 0.50%;
}
.flightLogo {
color:white;
}
.hotelLogo {
color:white;
}
.hotelContent {
color: white;
font-weight: 700;
font-family: -apple-system,BlinkMacSystemFont,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,Arial,sans-serif;
}
.flightContent {
color: white;
font-weight: 700;
font-family: -apple-system,BlinkMacSystemFont,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,Arial,sans-serif;
}
.one {
margin-right: 1%;
/* display: inline-block; */
}
.two {
/* display: inline-block; */
}
This is ReusableButtons,js
import React, { Component } from 'react';
import './ReusableButtons.css';
class ReusableButtons extends Component {
render() {
return (
<div className='buttonUsage'>
<button type='button' className='customButtonStyle rounded-top'>
{this.props.children}
</button>
</div>
)
}
}
export default ReusableButtons
This is ReusableButtons.css
.customButtonStyle {
background-color: #00b2d6;
border: none;
padding: 0.50%;
padding-left: 0.50%;
padding-right: 0.50%;
}
I am not aware of react but I used only HTML and CSS from your code and checked it. It looks fine.
But still, if you facing the problem, then increase the button width this will help you.

Angular 6 component, apply css to class applied with hostbinding?

I normally use a global SCSS file in my apps, but today I wanted to make some scss scoped to just my component.
I have set a class on the component via #HostBinding (of class.card).
I can style items inside my component, such as the caard-body, but how can I apply overrides to this component's card class?
In other words, I have several cards on the page. I have styles that I only want to apply to this card, to make it look different. So I'd like to apply css such as
.card { background-color: violet; }
So that only this card is violet. I want to make this css part of this component, so everywhere I use it it will automatically be a violet card. (In reality I want to more than a simple thing, but you get the idea)
My component TS:
import { Component, Input, HostBinding } from '#angular/core';
#Component({
selector: 'app-reviews',
templateUrl: './reviews.component.html',
styleUrls: ['./reviews.component.scss']
})
export class ReviewsComponent {
#HostBinding('class.card')
true;
#Input()
review;
constructor() {}
}
My Component SCSS:
.card-header {
border: 0;
color: #cf0989;
font-family: 'Helvetica Neue', Arial, sans-serif;
font-size: 2rem;
font-weight: normal;
}
.card-body {
...
}
My component HTML:
<div class="card-header">Testimonials</div>
<div class="card-body">
<div class="review-image">
<img class="rounded-circle"
alt="{{review.acf.article_author.post_title}}"
src="{{review.acf.article_author.acf.image}}">
</div>
<div class="review-content">
<p class="review">
<span [innerHTML]="review.content.rendered"></span>
</p>
<p class="reviewer-name">{{review.acf.article_author.post_title}}</p>
</div>
</div>
The selectors in my CSS for .card-body and .card-header work fine, but I cannot style this .card. Adding this, for example does nothing:
.card { background-color: violet: }
How can I create SCSS selectors in the component's SCSS file that also apply to the class applied to the component via #HostBinding? Can I? Should I?
It looks like I need to use :host, is that correct?
:host {
&.card {
padding: 2rem;
}
}

Angular Material Tab active link to put drop down arrow

I'm beginner for the Angular & i try to do some Angular tab active line bar to replace some arrow but it's not working , anyone know how to do that correctly
stackblitz sample here
code
<mat-tab-group>
<mat-tab label="First"> Content 1 </mat-tab>
<mat-tab label="Second"> Content 2 </mat-tab>
<mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
css
/* active tab */
::ng-deep .mat-tab-list .mat-tab-labels .mat-tab-label-active {
color:#0f2241;
background-color: lightgrey;
}
/* ink bar */
::ng-deep .mat-ink-bar {
background-color:red !important; background: url('../../../assets/sg-img/drop-down-arrow.png');
}
Thanks
Instead of using ::ng-deep set the ViewEncapsulation of your component to none:
import {Component, ViewEncapsulation} from '#angular/core';
#Component({
selector: 'tab-group-stretched-example',
templateUrl: 'tab-group-stretched-example.html',
styleUrls: ['tab-group-stretched-example.css'],
encapsulation: ViewEncapsulation.None
})
export class TabGroupStretchedExample {}
Then use the following CSS (I had to use another arrow since you used a local reference in your stackblitz):
.example-stretched-tabs {
max-width: 800px;
}
/* active tab */
.mat-tab-list .mat-tab-labels .mat-tab-label-active {
color:#0f2241;
background-color: lightblue;
opacity: 1;
}
/* ink bar */
.mat-tab-group.mat-primary .mat-ink-bar {
background: none;
content: url('https://image.flaticon.com/icons/svg/60/60995.svg');
height: 10px;
}
Is this what you were looking for: stackblitz?

CSS is applied from the last object in CSS file instead of closest parent

I am building a web site that is basically made out of sections + rows + columns and to each element you can apply a color scheme.
If the color scheme is applied to the section, all the rows and columns within it will have the same color scheme, however, sometimes I want to add a color scheme to a single column to differentiate it, but in some cases the parent section color scheme css is placed AFTER the color scheme css for the column (in the css file), and then it applies the colors for the section instead of the columns.
Here is the code (simplified for the sake of example).
I could get around it in specific cases, using !important, but I am looking for a global solution.
CSS:
/* Grey */
.color-scheme-grey button {
background-color: #666666;
color: #ffffff;
}
/* Blue */
.color-scheme-blue button {
background-color: blue;
color: #ffffff;
}
HTML:
<div class="section color-scheme-blue">
<div class="row">
<div class="column color-scheme-grey">
<button>I am blue, but I wish I was grey!</button>
</div>
</div>
</div>
Thanks!
This is expected behaviour as a direct result of Cascade Precedence.
If two rules carry the same weight or specificity the rule declared last always wins and over-qualifies the other.
Use more Specificity
Consider declaring another element or class selector in the range of the contextual selectors already specified.
Example:
Added element selector (div) for more specificity...
div.color-scheme-grey button { ... }
Added a class selector (.section) for more specificity...
.section .color-scheme-grey button { ... }
Code Snippet Demonstrations:
1. Additional Class Selector:
/* Grey */
.section .color-scheme-grey button {
background-color: #666666;
color: #ffffff;
}
/* Blue */
.color-scheme-blue button {
background-color: blue;
color: #ffffff;
}
<div class="section color-scheme-blue">
<div class="row">
<div class="column color-scheme-grey">
<button>I am blue, but I wish I was grey!</button>
</div>
</div>
</div>
2. Additional Element Selector:
/* Grey */
div.color-scheme-grey button {
background-color: #666666;
color: #ffffff;
}
/* Blue */
.color-scheme-blue button {
background-color: blue;
color: #ffffff;
}
<div class="section color-scheme-blue">
<div class="row">
<div class="column color-scheme-grey">
<button>I am blue, but I wish I was grey!</button>
</div>
</div>
</div>
For Further Information regarding CSS Specificity:
Specificity - CSS | MDN
Specifics on CSS Specificity | CSS Tricks
Apply your styles from the parent like below.
.section .color-scheme-grey button {
background-color: #666666;
color: #ffffff;
}
DEMO
You could increase the specificity of the rules to counteract the order of the files:
/* Grey */
.color-scheme-grey button {
background-color: #666666;
color: #ffffff;
}
/* Blue */
.color-scheme-blue button {
background-color: blue;
color: #ffffff;
}
.color-scheme-blue .color-scheme-grey button {
background-color: #666666;
}
You need to be careful with this, though, as it can very quickly get out of hand with all the combinations!
Better would be to organise a set of formatting that applies to rows/sections and, separately, columns, and organise them appropriately in a file (sections first).

Resources