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
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I wanted to make a calculator to apply what I learned recently about Javascript, this code works, the calculator looks nice but I had to use the !important property. Does that come with any risk? Is it a good method? Since I am a beginner I am not sure if the new information and shortcuts that I find online to make stuff easier to do, are actually good. Here is my entire code:
function calcNumbers(result) {
form.displayResult.value = form.displayResult.value + result;
}
function myFunction() {
document.getElementById("myForm").reset();
}
body,
html {
background: rgba(155, 70, 194, 0.479);
margin: 0;
padding: 0;
}
.container {
position: fixed;
top: 50%;
left: 20%;
transform: translate(-50%, -50%);
background: #E69A8DFF;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.2);
border-radius: 14px;
padding-bottom: 20px;
width: 320px;
}
.display {
width: 100%;
height: 60px;
padding: 40px 0;
background: #7FFFD4;
border-top-left-radius: 14px;
border-top-right-radius: 14px;
}
.buttons {
padding: 20px 20px 0 20px;
}
.row {
width: 280px;
float: left;
}
input[type=button] {
width: 60px;
height: 60px;
float: left;
padding: 0;
margin: 5px;
box-sizing: border-box;
background: #ecedef;
border: none;
font-size: 30px;
line-height: 30px;
border-radius: 50%;
font-weight: 700;
color: #5E5858;
cursor: pointer;
}
input[type=text] {
width: 270px;
height: 60px;
float: left;
padding: 0;
box-sizing: border-box;
border: none;
background: none;
color: #000000;
text-align: right;
font-weight: 700;
font-size: 60px;
line-height: 60px;
margin: 0 25px;
}
.red {
background: #ffffff !important;
color: #490050 !important;
}
.green {
background: #400164 !important;
color: #ffffff !important;
}
<div class="container">
<form name="form" id="myForm">
<div class="display">
<input type="text" placeholder="0" name="displayResult" />
</div>
<input type="button" onclick="myFunction()" value="C" class="red">
<div class="row">
<input type="button" name="b7" value="7" onClick="calcNumbers(b7.value)">
<input type="button" name="b8" value="8" onClick="calcNumbers(b8.value)">
<input type="button" name="b9" value="9" onClick="calcNumbers(b9.value)">
<input type="button" name="addb" value="+" onClick="calcNumbers(addb.value)">
</div>
<div class="row">
<input type="button" name="b4" value="4" onClick="calcNumbers(b4.value)">
<input type="button" name="b5" value="5" onClick="calcNumbers(b5.value)">
<input type="button" name="b6" value="6" onClick="calcNumbers(b6.value)">
<input type="button" name="subb" value="-" onClick="calcNumbers(subb.value)">
</div>
<div class="row">
<input type="button" name="b1" value="1" onClick="calcNumbers(b1.value)">
<input type="button" name="b2" value="2" onClick="calcNumbers(b2.value)">
<input type="button" name="b3" value="3" onClick="calcNumbers(b3.value)">
<input type="button" name="mulb" value="*" onClick="calcNumbers(mulb.value)">
</div>
<div class="row">
<input type="button" name="b0" value="0" onClick="calcNumbers(b0.value)">
<input type="button" name="potb" value="." onClick="calcNumbers(potb.value)">
<input type="button" name="divb" value="/" onClick="calcNumbers(divb.value)">
<input type="button" class="green" value="=" onClick="displayResult.value=eval(displayResult.value)">
</div>
</form>
</div>
Sometimes there's just no way around the !important operator (mostly when working with third party css files you want to override).
However, in your case you should be able to override styles just by making the selector more specific than others.
In your code you could change .red with input[type=button].red, that would make it more specific and thus it would override it! (That's the cascading part of Cascading Style Sheets).
Question for you: I don't see where those classes are being implemented -- are you using them? !important is used to override any existing properties of the element it's applied to, so you can use it to emphasize that you want this property to take priority always. There's nothing inherently bad about using it, though it's considered bad practice to overuse them (probably not an issue in your case). Also, note where you have them and keep that in mind in case you apply some styling that you can see in the future. The !important will override everything. Good luck on you project.
A bit of background about why you may have needed !important in the first place. It comes down to 'specificity', you can read more about the rules here - https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Normally !important can lead to a smell that your CSS isn't very well structured, and you should often be able to get the desired results with well defined CSS taking into account the rules of specificity. Once you start using !important when it's not really needed, you end up using it more places to override other !important styles, which is bad.
For the likes of a .red class, I would say thats a case where it may make sense to use !important as it feels like a CSS class that if it were applied, it should render something red, i.e. it is a class of high importance and wouldn't make sense to have a class of red assigned to an element, but that doesn't render red text, or whatever the class is designed to do.
The reason you think !important is required is that you are trying to change CSS properties (background-color and color) which have already been defined using this selector:
input[type=text]
Because this selector consists of an element selector input and and attribute selector [type=text], its specificity beats that of a simple .green CSS class. Understanding specificity is the groundwork for using CSS. There's no way around understanding that concept.
To avoid having to use !important, simply raise the selectors to the same level of specificity as what already is there:
input.green
and
input.red
That being said, both .red and .green are really, I mean, really bad choices for CSS class names. CSS class names should never contain information about a specific styling of any elements, but instead describe what the CSS class does functionally.
The reason for this is when your customer comes next week and wants blue instead of green buttons, you will either also have to change the HTML (which is almost always unwanted, and, with proper class names, also almost always unnecessary), or start doing what you did, which is keep using color names in the CSS and HTML that actually do not represent the color their names claim to represent.
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.
check out this JSFiddle
I want to apply different style to second button. I guess there is just a different sytax. Help.
<input type="button">
<input type="button" class="button2">
I dont want to use id, beacause it's to be used for some other purpose.
Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied.
Specificity is only based on the matching rules which are composed of
selectors of different sorts.
You can specificity the css selector using 'has' class add apply the rule to input button elements with class .button2:
input[type="button"] {
width: 500px;
background: red;
}
[type='button'].button2 {
width: 100px;
background: black;
}
<input type="button">
<input type="button" class="button2">
Also take a look Specificity.
<input type="button">
<input type="button" class="button2">
input[type="button"]{
width: 500px;
background: red;
}
input[type="button"].button2{
width: 100px;
background: black;
}
Fiddle: http://jsfiddle.net/0dyk2n3b/1/
Check this
http://jsfiddle.net/0dyk2n3b/2/
input.button2{
width: 100px;
background: black;
}
Slight variation on Arvind's answer, but without use of important:
input[type="button"] {
width: 500px;
background: red;
}
input[type="button"].button2 {
width: 100px;
background: black ;
}
<input type="button">
<input type="button" class="button2">
Yes, you can do this.
jsFiddle: http://jsfiddle.net/swapnilmotewar/0dyk2n3b/4/
HTML:
<input type="button">
<input type="button" class="button2">
CSS:
input[type="button"]{
width: 500px;
background: red;
}
input[type="button"].button2{
width: 100px;
background: green;
}
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
I have CSS that changes formatting when you hover over an element.
.test:hover { border: 1px solid red; }
<div class="test">blah</div>
In some cases, I don't want to apply CSS on hover. One way would be to just remove the CSS class from the div using jQuery, but that would break other things since I am also using that class to format its child elements.
Is there a way to remove 'hover' css styling from an element?
One method to do this is to add:
pointer-events: none;
to the element, you want to disable hover on.
(Note: this also disables javascript events on that element too, click events will actually fall through to the element behind ).
Browser Support ( 98.12% as of Jan 1, 2021 )
This seems to be much cleaner
/**
* This allows you to disable hover events for any elements
*/
.disabled {
pointer-events: none; /* <----------- */
opacity: 0.2;
}
.button {
border-radius: 30px;
padding: 10px 15px;
border: 2px solid #000;
color: #FFF;
background: #2D2D2D;
text-shadow: 1px 1px 0px #000;
cursor: pointer;
display: inline-block;
margin: 10px;
}
.button-red:hover {
background: red;
}
.button-green:hover {
background:green;
}
<div class="button button-red">I'm a red button hover over me</div>
<br />
<div class="button button-green">I'm a green button hover over me</div>
<br />
<div class="button button-red disabled">I'm a disabled red button</div>
<br />
<div class="button button-green disabled">I'm a disabled green button</div>
Use the :not pseudo-class to exclude the classes you don't want the hover to apply to:
FIDDLE
<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test nohover"> blah </div>
.test:not(.nohover):hover {
border: 1px solid red;
}
This does what you want in one css rule!
I would use two classes. Keep your test class and add a second class called testhover which you only add to those you want to hover - alongside the test class. This isn't directly what you asked but without more context it feels like the best solution and is possibly the cleanest and simplest way of doing it.
Example:
.test { border: 0px; }
.testhover:hover { border: 1px solid red; }
<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test testhover"> blah </div>
add a new .css class:
#test.nohover:hover { border: 0 }
and
<div id="test" class="nohover">blah</div>
The more "specific" css rule wins, so this border:0 version will override the generic one specified elsewhere.
I also had this problem, my solution was to have an element above the element i dont want a hover effect on:
.no-hover {
position: relative;
opacity: 0.65 !important;
display: inline-block;
}
.no-hover::before {
content: '';
background-color: transparent;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 60;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<button class="btn btn-primary">hover</button>
<span class="no-hover">
<button class="btn btn-primary ">no hover</button>
</span>
You want to keep the selector, so adding/removing it won't work. Instead of writing a hard and fast CSS selectors (or two), perhaps you can just use the original selector to apply new CSS rule to that element based on some criterion:
$(".test").hover(
if(some evaluation) {
$(this).css('border':0);
}
);