How to call multiple classes of CSS in a single class? - html

I have made one button:
<input type="button" class="btn-custom btn btn-info" />
CSS Code
.btn-info {
background-color: #6fb3e0!important;
border-color: #6fb3e0;
}
.btn{padding:5px 10px}
.btn-custom{height:20px;}
I want to combine the style of btn-custom and btn btn-info in single class called .btnCustom.
In short I want the same result as when I try this:
<input type="button" class="btnCustom" />

you can try like this -
.btnCustom, .btn-info {
background-color: #6fb3e0!important;
border-color: #6fb3e0;
}
.btnCustom ,.btn{
padding:5px 10px;
}
.btnCustom ,.btn-custom{
height:20px;
}
<input type="button" class="btnCustom"/>

Hi now try to less css than define those class in one class as like this
Than try to this way
.btnCustom{
.btn();
.btn-info();
height:20px;
}

You need a preprocessor such as less or sass
.class1 {
color: #333;
}
.class2 {
#extend .class1;
border-color: green;
}
Otherwise you have to stick to
<input type="button" class="btn-custom btn btn-info" />

You need to use some preprocessor like less or sass. (You can use the plain CSS too. Just use the Generated CSS code from SASS/SCSS code).
Using sass you can do it like:
HTML Code
<input type="button" class="btnCustom" />
SASS/SCSS Code
.btn-info {
background-color: #6fb3e0!important;
border-color: #6fb3e0;
}
.btn{
padding:5px 10px
}
.btn-custom{
#extend .btn;
#extend .btn-info;
height:20px;
}
Generated CSS Code after compiling the SASS/SCSS Code
.btn-info, .btn-custom {
background-color: #6fb3e0!important;
border-color: #6fb3e0;
}
.btn, .btn-custom{
padding:5px 10px
}
.btn-custom{
height:20px;
}
Of course if you don't want to use any preprocessor, you can use the CSS code generated after compilation directly.

You can use Css like this,
.btnCustom, .btn-info {
background-color: #6fb3e0!important;
border-color: #6fb3e0;
}
.btnCustom ,.btn{
padding:5px 10px;
}
.btnCustom ,.btn-custom{
height:20px;
}
and Html
<input type="button" class="btn-custom btn btn-info" />
Its work in My Website.

You can actually combine all the classes into a single selector like so;
.btn-custom.btn.btn-info {
}
That will only select elements with all those classes.
You can read more about that selector at css-tricks.
Will that do what you want?
There are some good other answers. Using less or sass will likely do what you want.
Be careful with how you use commas in your selectors.

Related

I cannot give style class to material-menu (Angular 2+)

I would like to set style class to mat-menu. But I have tried everything, and it seems that it doesn't pick it up unless it's in Chrome Inspect.
html
<mat-menu class="matMenuColor" #menu="matMenu">
<form #logoutForm ngNoForm action="{{logoutUrl}}" method="POST">
<input type="hidden" name="token" value="{{token}}">
<input type="hidden" name="goto" value="{{redirectUrl}}">
</form>
</mat-menu>
css
.matMenuColor {
background-color: green;
}
Chrome Inspect:
.mat-menu-content:not(:empty) {
padding-top: 8px;
padding-bottom: 8px;
background-color: green;
}
The quickest solution is to use :ng-deep
::ng-deep .matMenuColor {
background: green;
}
But you can also try to change it by customize your theme.scss
Here you can find the documentation about theming Angular Material
https://material.angular.io/guide/theming
UPDATE
If you don't want to use ::ng-deep because it's obsolete try this this:
.mat-menu-panel > div.mat-menu-content {
background-color: green;
}
You can target this way
.mat-menu-panel.matMenuColor {
background: green;
}
.mat-menu-panel is the class name for mat-menu tag which is default.
::ng-deep is getting obsolete! Dont use it anymore! (https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep)
The best way is to create an extra css file like mat-menu.css.
Then you need to import it inside styles.css like:
#import 'mat-menu.css';
After that you need to import the styles.css in your components css like:
#import '../../../styles.css';
Inside the mat-menu.css you can style everything you want.
Here is an example of a mat-accordion (this is sass, not css):
mat-accordion
mat-expansion-panel
border-bottom: 1px $grey-700 solid
color: $grey-300
font-size: 18px
mat-expansion-panel-header
mat-panel-title
color: $primary-color
font-size: 18px

CSS fomaring alternately for class and id

I made a scheme for step-by-step instruction. Eventually there will be many options, buttons, way.. But at the moment I'm try to paint red the buttons that are inactive. But css is connected to the "class". How do you make an "id" that has the higher priority than the "class" used for text Formating?
JSFiddle
<input type="button" name="coughyes" class="next action-button" id="red" value="Yes" />
/*buttons*/
#msform .action-button {
width: 100px;
background: #27AE60;
font-weight: bold;
color: white;
border: 0 none;
border-radius: 1px;
cursor: pointer;
padding: 10px 5px;
margin: 10px 5px;
}
#red {
background-color: red;
}
You can use the same selector and append your ID.
#msform .action-button#red {
background-color: red;
}
Though you should use a class since you can only use an ID on the page once. Update your HTML and CSS to...
<input type="button" name="coughyes" class="red next action-button" value="Yes">
<input type="button" name="coughno" class="red next action-button" value="No">
#msform .action-button.red {
background-color: red;
}
Updated your fiddle http://jsfiddle.net/1x183c2t/3/
Or if you wanted to keep the same markup, you could simply change background-color: red; to background-color: red !important;.

CSS is there something like a "class subclass"?

I have 20 inputs in a page that are styled as buttons using the class property (that is, in the css I define borders, padding, width, etc).
But I want each one to have a different background color. In my current CSS I have 20 classes (one for each input) which are copies of all the style properties except for background-color.
It is working OK like this, but I feel somewhat uncomfortable repeting code. In there a very simple way to define most of the properties for all inputs (borders, padding, width...) and then specify the background-color, one for each input?
HTML
<input type="text" class="btn red" value="Red">
<input type="text" class="btn green" value="green">
<input type="text" class="btn blue" value="blue">
<input type="text" class="btn black" value="black">
<input type="text" class="btn orange" value="orange">
CSS
.btn{
border:1px solid grey;
width:50px;
height:10px;
padding:10px;
margin: 10px;
}
.red{
background-color:red;
}
.green{
background-color:green;
}
.blue{
background-color:blue;
}
.black{
background-color:black;
color:white;
}
.orange{
background-color:orange;
}
Here is the fiddle
You can use multiple classes on elements and use a common class on the elements for the shared styles and then a different class (or id) for the custom styles specific to that element.
e.g.
HTML
<input type="text" class="input-class red" />
<input type="text" class="input-class green" />
CSS
.input-class {
margin: 0;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
You could use a different selector to target all text inputs rather than having a common class aswell:
input[type="text"] {
margin: 0;
}
As well as the above, you can create more specific selectors like:
.input-class.red {
background-color: red;
}
.input-class.green {
background-color: green;
}
input[type="text"].red {
background-color: red;
}
input[type="text"].input-class.red {
background-color: red;
}
Which will only target elements that match those selectors, situations where this might be useful is when you might have a class with the same name elsewhere that you don't want to be affected.
For example you might have already:
.red {
color: red
}
So you don't want an input with red text on a red background so you can chain the class selectors together to be more specific.
Like your "subclass":
.class {
/* for <elem class="class"> only */
}
.class.subclass {
/* for <elem class="class subclass"> only */
}
Is there a very simple way to define most of the properties for all
inputs (borders, padding, width...) and then specify the
background-color, one for each input?
Firstly, define the styling for all inputs, e.g. (or use a shared class etc):
input{
border:...
padding:...
}
Then, if the inputs are all children of the same top level parent, you can isolate specific ones using the :nth-of-type selector:
input:nth-of-type(1){
background-color:red;
}
input:nth-of-type(2){
background-color:blue;
}
Alternatively, provide each with a relevant class designation
There is not in native CSS, but you have other ways of doing this at your disposal:
You can define a common class that holds all of the style that are common across the inputs:
.my-inputs {
border 1px solid #ccc;
padding: 15px;
}
and then you can modify that as needed
.my-inputs.city {
width: 100px;
}
.my-inputs.state {
width: 50px;
}
So that in your markup, you can do things like this:
<input type="text" class="my-inputs state">
Another way you can do this is to use a CSS preprocessor which does afford you the ability to do class inheritance. Take a look at the documentation for LESS and SASS:
http://lesscss.org/
http://sass-lang.com/

Basic HTML CSS. Why my CSS on Button does not work?

I'm making mock up site to learn. But my css on Buttons don't work, I can put any css and their appearance doesn't change.
HTML:
<div id="bottom">
<form method="get">
<button class="sButtons" type="submit" name="gSearch">Google Search</button>
<button class="sButtons" type="submit" name="gLucky">I'm Feeling Lucky!</button>
</div>
And the CSS
#sButtons{
overflow: visible;
padding: 0 7px;
width: 100px;
color: #ffffff;
background-color:black;
}
(I just put very random values into it just to see if there's a difference)
here's a link to the rest of my code https://gist.github.com/Bitvala/4c0600c03c3a215fd023
In CSS the Class selector is defined with a dot like so:
.Divclass{
position:absolute;
}
And the ID Selector is used with a #, like so:
#DivID{
position:absolute;
}
What you want to do is:
.sButtons{
overflow: visible;
padding: 0 7px;
width: 100px;
color: #ffffff;
background-color:black;
}
change #sButtons to .sButtons
or class to id

Btn are not getting the color

I have a button with the following code, but the CSS below is not working
i.e. the color and size is not changing
CSS:
.btn-default .act-buttons{
background-color: black!important;
color: white!important;
height: 28px;
width: 28px;
}
HTML:
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="submit" value="Save" class="btn btn-default act-buttons" />
</div>
</div>
Given your most recent html...
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="submit" value="Save" class="btn btn-default act-buttons" />
</div>
</div>
Try this css (note the lack of a space in .btn-default.act-buttons):
.btn-default.act-buttons{
background-color: black!important;
color: white!important;
height: 28px;
width: 28px;
}
This will target any element with both classes btn-default and act-buttons
.btn-default .act-buttons would target a parent element with the class btn-default which contains a child element with the class act-buttons
.btn-default,.act-buttons would target any element with either of the classes btn-default or act-buttons
your css is targeting any class named act-buttons which is inside an element which has a class btn-default
you could change it to
.btn-default, .act-buttons{...}
or
.btn-default.act-buttons{...} // no space between classes
#KyleMit's is the answer you'll learn the most from, and specifically, you want to pay attention to his note on the And Selector:"btn-default.act-buttons will find any elements that match both class selectors, meaning any element that has class="btn-default act-buttons"
So, using the And Selector, you should use
.btn-default.act-buttons{
background-color: black;
color: white;
like here: http://www.bootply.com/bFjCr8eQlY
ps - your height and width will need it's own fixing unless what you put in the button is exactly the right size, but that's another issue.
Overview of CSS Selectors:
First things first, make sure you're using the right selector:
Descendant selector: .btn-default .act-buttons
Will find any elements with the class act-buttons that are descendants of an element with the class btn-default
And Selector: .btn-default.act-buttons
Will find any elements that match both class selectors, meaning any element that has class="btn-default act-buttons"
Or Selector: .btn-default, act-buttons
Will find any elements that either have a class of btn-default or a class of act-buttons
Also, check that...
The html element has access to the selector
You've applied your custom styles after the bootstrap style sheet
You should always try to override standard styles with your own
then you don't need !important
Finally, make sure you're using act-buttons or action-buttons consistently
Here's a working demo in Fiddle
.btn.act-buttons{
background-color: rgb(0,0,0);
color: white;
width: 100px;
}
Screenshot:
well try this
<div class="form">
<input class="field button2" id="submit" type="button" value="Save"/>
</div>
and in your css
.button {
background-color: #FFFFFF;
padding: 2px 4px;
font: 13px sans-serif;
text-decoration: none;
border: 1px solid #000;
color: #000
}
.button:hover { background-color: #46000D; }
input.button2 {
background-color: #000000;
color: #ffffff
}
input.button2:hover { background-color: #ffffff; color: #000000; }
it will look better, i hope i helped you :-D