how to make button stay highlighted after click on screen? - css

I have added two button in modal window in which one button is highlighted but when-ever i am going to click on screen it goes to normal.How can i make it stay highlighted.
I am using following code:
<button type="button" class="btn btn-default">New</button>
<button type="button" class="btn btn-default">Used</button>
<button type="button" class="btn btn-default" autofocus>Any</button>

So you want JSFiddle?
CSS
.active {
color: #F00;
}
JS
$('button').on('click', function() {
$('button').removeClass('active');
$(this).addClass('active');
}

HTML :
<button id="highlight" ..... ></button>
CSS :
#highlight, #highlight:hover, #highlight:active, #highlight:focus {
color://any-color;
// more css
}
NOTE : this is using the pseudo classes hover, focus and active, to keep the same style of the button unchanged when any event on the button occurs.

Try
var yourbuttons = document.getElementsByClassName('highlight-hover');
for (var i = yourbuttons.length - 1; i >= 0; i--) {
var currentbtn;
yourbuttons[i].onclick=function(){
if(currentbtn){
currentbtn.classList.remove("highlight");
}
this.classList.add("highlight");
currentbtn=this;
}
};
There's not really a css only method if you want the button to stay highlighted when you click another part of the screen. :active or :focus remove their styles when the element is not longer active or in focus
Here's a working version of what it seems you want because i'm bored at work https://jsfiddle.net/mksty8eq/
When you click on one button it stays highlighted until another button is clicked

First setting autofocus inside modal will not work always. You should set focus on button via javascript. Refer http://getbootstrap.com/javascript/#modals .
$('#myModal').on('shown.bs.modal', function () {
$('#button3').focus()
})
:focus and :active are pseudo-classes that allows us to define specific CSS rules depending on state of the element. In bootstrap .active and .focus classes has same CSS style as :focus and :active.
So add .active or .focus to the button that you want to highlight always.
<button type="button" class="btn btn-default active">Any</button>

Related

How can a custom class override the btn and btn-danger classes of React-bootstrap without using IDs or !important and by only using classNames

I am using React-bootstrap to style my button, But I am unable to override the btn and btn-danger classes provided by bootstrap. How can I make my class slotTiming-box-button override the btn and btn-danger classes. I want to do this without using ids, or !important. I am allowed to use classNames only.
React code snippet
<Button
className="slotTiming-box-button"
variant="danger"
>
Click Me
</Button>
HTML element formed
<button type="button" class="slotTiming-box-button btn btn-danger">Click Me</button>
An image of the Computed section(in Developer Tools) of the Button is here.
I have tried the answer here, but it wasnt much clear.
Just use CSS for the custom class...
.slotTiming-box-button {
background-color: #990000;
border-color: #661111;
}
.slotTiming-box-button:hover {
background-color: #dd2222;
}
https://codeply.com/p/tXdGmQucvD

Angular Material How to disable an svg icon

Similar to the classic bootstrap disabled state -
<button type="button" class="btn btn-lg btn-primary" disabled>Primary button</button>
I'd like to simulate a disabled state on an Angular Materials svg icon using cursor: not-allowed css. But I want to disable the click event.
.toolbar-icon-disabled {
fill: gray;
cursor: not-allowed;
pointer-events: none;
}
<mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="true"></mat-icon>
Problem here is that I CANNOT use both not-allowed and pointer-events: none, as they appear to be mutually exclusive.
ex/ Here you can see that my left-most icon appears disabled, as the fill color is gray; however, it's still clickable if I add cursor: not-allowed.
I couldn't copy/paste the not-allowed circle from my screen, but here's an example:
Suggested work around:
1) Put the mat-icon in another anchor tag and add curson not allowed to that anchor.
HTML:
<a [class.linkDisabled]="true"><mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="true"></mat-icon></a>
CSS:
.toolbar-icon-disabled {pointer-events: none;}
.linkDisabled {cursor: not-allowed;}
2) keep the 'cursor: not-allowed;' in the css and i am assuming that there will be an event listener on the icon to do some action. Go into that method, check for the disabled condition and return if true. For example:
html:
<mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="flgDisabled" (click)="onClick()"></mat-icon>
method:
onClick() {
if(this.flgDisabled) {
return
}
console.log('called');
}

Hide button in css

How can i hide button in css it the button text is read more.
I have a add to cart button on web site product archive page. But when the product will go out of stock the check changes to read more. I want to hie the button the the text changes to read more.
HTML
<input type="button" value="read more"/>
CSS
input[value="read more"]{ display:none; }
input[value="read more"]{
display:none;
}
<input type="button" value="read more"/>
<input type="button" value="add to cart"/>
You should add display: none; to yor button style to hide.
You should use a style to rub out the button styles, but it will still keep the button text there. Apply a css class such as this to the button
.icon-button {
border: none;
background:none;
}

Twitter-Bootstrap button color on-loading state

I'm currently working on a web project and I changed the Bootstrap's primary button color to orange. Now almost all button states work fine: active state, hover state, etc - except the loading state.
Once it enters the loading state, it returns to its default color which is blue.
I was searching on how to do it but seems I can't seem to find it. Can anyone help me out with this or if this question already exists, please redirect me to it. Preferably, I'd like to do it without javascript. Thanks!
This one rather can't be done without using JavaScript.
First way, if you aren't afraid of JS: jsfiddle
HTML:
<button type="button" id="btn1" data-loading-text="Hm.." class="btn btn-primary">
Loading state
</button>
CSS:
.btn-primary,
.btn-primary:hover,
.btn-primary:active {
border: none;
margin: 5px;
background-color: #b2d025; /* here */
}
JS:
$("#btn1").click(function() {
var $btn = $(this);
$btn.button('loading');
$(this).css('background-color','#b2d025'); /* and here */
setTimeout(function () {
$btn.button('reset');
}, 1000);
});
Second way (less js, more css, using !important): jsfiddle
You can use Inline CSS
<button type="button" id="btn1" data-loading-text="Hm.." class="btn btn-primary" style="background-color:red">
Loading state
</button>
Note : This will gives your answer but hover and active effects will not work
Did you try using bootstrap's customize
I think they have an option for changing the color of primary button. Look for btn-primary-color
If you don't want to use javascript than you can try this..
.btn-primary,
.btn-primary:hover,
.btn-primary:focus,
.btn-primary:focus:hover {
border: none;
background-color: #b2d025;
}
Hope this helps...

How to add class to all elements inside div?

I have 100 buttons inside a div:
<div class="button_group">
<button> ... </button>
<button> ... </button>
<button> ... </button>
...
</div>
I want to style all buttons in the bootstrap style. So, I need to change everywhere < button > to < button class="btn" >. I want to do it without actually adding class="btn" 100 times for each < button > element. So, I need some CSS rule that says that all buttons inside the div are of class "btn". So, I need something like this:
.button_group button {
... add class="btn" to all elements ...
}
Is it possible at all? What is the correct syntax?
Thank you!!
HTML
<div class="button_group">
<button> ... </button>
<button> ... </button>
<button> ... </button>
</div>
CSS
.button_group > button {width: 100px; height: 50px;}
And the fiddle https://jsfiddle.net/049Lj4LL/4/
There is no need to ad class="btn" to every button if they are the same, just add a class to the parent div like you have class="button_group" to be able to identify that specific group of buttons. Like this https://jsfiddle.net/049Lj4LL/6/ and like this https://jsfiddle.net/049Lj4LL/7/
Assuming you have a jquery-like library, you would add a class btn to all the <button> elements like that :
$('div.button_group button').addClass('btn');
Edit: In case you are using the sass or less version of bootstrap, which I encourage you, you can try to extend the btn bootstrap class. Contrary to the javascript solution mentioned above, that won't add the class btn to the elements, but they will inherit the styles of the btn class. Here is the sass solution :
.button_group {
button {
#extend .btn;
}
}
Use javascript.
In JS, you can use the childNodes property of the div element, that you can access using document.getElementByClassName. In the array document.getElementByClassName("button_group").childNodes are initialized all the button tags written in the div. Set the class name with .className. Add the JS code to your HTML with the script tag:
<script>
for(let i=0; i<document.getElementByClassName("button_group").childNodes.length; i++){
document.getElementByClassName("button_group").childNodes[i].className="theClassNameYouDesire";
}
</script>

Resources