I have an ion-list which have a button and card inside it, now I want to change the color of the button and card on hover but can't find a way to do it.
Here is my code. Thanks in advance
Html
<div [hidden] = hidden >
<ion-list>
<ion-card >
<button inset = true *ngFor="let item of items" (click)="open($event, item.name)" >
<ion-card>
<h2> <b>{{ item.name }} </b> </h2>
<p> Recommended for</p>
{{item.Description}}
</ion-card>
<br>
<br>
</button>
</ion-card>
</ion-list>
</div>
CSS
ion-card{
display: flex;
flex-direction: column;
width: 100% !important;
margin: 0 !important;
background-color:white;
box-shadow: none;
}
button.item:hover a{
background-color: slategray;
box-shadow: inset 0px 0px 0px 1px red;
}
First of all u can't hover on device but u can attach click events for that u need to use ngStyle in ur components to change color or show if its clicked.
<div [hidden] = hidden >
<ion-list *ngFor="let item of items">
<ion-card >
<ion-button (click)="open($event, item.name)" fill="clear" >
<ion-icon slot="icon-only" [name]="buttonIcon" [ngStyle]="{'color':color1}"></ion-icon>
</ion-button>
<ion-card>
<h2> <b>{{ item.name }} </b> </h2>
<p> Recommended for</p>
{{item.Description}}
</ion-card>
<br>
<br>
</button>
</ion-card>
</ion-list>
</div>
inside ur TS
buttonIcon: string = "md-add";
color1: string = "black";
open(event,item){
this.buttonIcon = "done-all"; // icon change name if u want to change icon too
this.color1 ="red"; // any color u want it to change to
//rest of your code
}
U can use the same ngstyle on ur ion card as well to change color.
U can also use ion-ripple-effect to show some sort of click feedback on cards.
Related
I am trying to change the style of the specific button I click in the *ngFor in the ion-col. Currently when I click on a button, the styles for all buttons changes at the same time.
Here are my codes:
<ion-col *ngFor="let item of displayRestaurant[0].seatTimeSlotAndDiscount | slice:indexStart:indexEnd; let i=index" no-padding>
<button ion-button style="border-radius:100px;height:70px;width:70px;text-align:center;" (click)="clickedDiscount(item)" [ngClass]="clickedDiscountBoolean ? 'discountClicked' : 'discountUnclicked'">
<p style="font-size: 10px; color:white;">{{item.percentageDiscount}}</p>
<p style="font-size:10px;color:white;">{{item.timeOfDiscount}}</p>
</button>
</ion-col>
SCSS:
.discountClicked{
border: 3px solid orange;
}
.discountUnclicked{
border:none;
}
.ts:
clickedDiscount(discount:Discount){
this.clickedDiscountBoolean = !this.clickedDiscountBoolean;
}
Originally it is like this:
Here is what my current codes do after I clicked on any of the buttons (The new style is applied to all):
What I am looking for is, when I click on any of the buttons, only that button's style will change, and the rest will not.
All your buttons look up for same condition. You need to unify that condition to be true for only one button. You can use index value for that :
<ion-col *ngFor="let item of displayRestaurant[0].seatTimeSlotAndDiscount | slice:indexStart:indexEnd; let i=index" no-padding>
<button ion-button style="border-radius:100px;height:70px;width:70px;text-align:center;"
(click)="clickedDiscount(item,i)"
[ngClass]="clickedIndex === i ? 'discountClicked' : 'discountUnclicked'">
<p style="font-size: 10px; color:white;">{{item.percentageDiscount}}</p>
<p style="font-size:10px;color:white;">{{item.timeOfDiscount}}</p>
</button>
</ion-col>
Then in your component
clickedDiscount(discount:Discount,index:number){
this.clickedIndex = index;
}
<button mat-raised-button color="warn">
<mat-icon>delete</mat-icon>
Remove Driver
</button>
How to add padding in between mat-icon and button title?
I want to keep the button title in the middle of the button and keep the icon in the left corner.
You can achieve this by adding a padding to your button text instead of the icon.
Add a class for the button text in the template.
<button mat-raised-button color="warn">
<mat-icon>delete</mat-icon>
<span class="button-text">Remove Driver</span>
</button>
Then define button-text in the CSS.
.button-text {
padding-left: 50px;
padding-right: 74px; /* Add 24px to align the text in the center since the icon's width is 24px */
}
For that you need to add span tag for that under button title
<button mat-raised-button color="warn">
<mat-icon>delete</mat-icon>
<span class="button-title">Remove Driver</span>
</button>
Then just add this .button-title in your CSS file for Styling
.button-title{
padding: 5px 50px 5px 50px;
}
This is Example You Can edit or preview Code Here on StackBlitz
I have the following code:
<ion-button expand=“full” (click)=“do()“>
<ion-icon name=“cart” slot=“start”></ion-icon>
TEXT
</ion-button>
I want the content (the TEXT and the ion-icon) to be aligned to the left and tried:
ion-button {
display: flex;
justify-content: flex-start;
}
I thought this would result into aligning the content to the left but nothing happens. It seems that the css is totally ignored.
You can align the text and icon to the left if you add a div inside your button and use Ionic 4 CSS Utilities (in your case text-left / text-start).
<ion-button expand=“full” (click)=“do()“>
<div text-left>
<ion-icon name=“cart” slot=“start”></ion-icon>
TEXT
</div>
</ion-button>
Make sure to give the div the full width of the button
ion-button > div {
width: 100%;
}
Documentation:
Ionic 4 CSS Utilities
Wrap your inner in span and add in css
.inner-span{
margin-left:auto;
}
As below (use " instead “)
<ion-button expand="full" (click)="do()">
<span class="inner-span">
<ion-icon name="cart" slot="start"></ion-icon>
TEXT
</span>
</ion-button>
See working code
THE SITUATION:
In my Ionic 2 app I need to have the menu button on two lines: icon and text.
The problem is that somehow the ion-button directive force the button to be on one line.
I need the ion-button directive to be sure the button has always the proper formatting and positioning responsively.
I just need that button to be on two lines.
This the html and css of my attempt:
THE HTML:
<ion-header>
<ion-navbar>
<button ion-button menuToggle="left" start>
<ion-icon name="menu"></ion-icon>
<br />
<p class="menuSubTitle">MENU</p>
</button>
<ion-title>
HomePage
</ion-title>
<button ion-button menuToggle="right" end>
<ion-icon name="person"></ion-icon>
<br />
<p lass="menuSubTitle">LOGIN</p>
</button>
</ion-navbar>
</ion-header>
THE CSS:
.menuSubTitle {
font-size: 0.6em;
float:left;
display: block;
clear: both;
}
THE QUESTION:
How can I make a ion-button on two lines?
Thanks!
You are along the right lines. A slight modification is needed for it to work.
Here is my markup:
<button ion-button block color="menu-o">
<div>
<ion-icon name="flash"></ion-icon>
<label>Flash</label>
</div>
</button>
The inner <div> inside the <button> is the trick. The only thing needed for this markup is to set the <label> element to display: block.
Since <p> is already a block level element. It may just work as is.
This will do the trick:
.button-inner {
flex-flow: column;
}
This will change the element ordering from horizontal to vertical.
You have to change the button height if you want it a bit prettier. (e.g. with icon margin)
.button-icon-top {
height: 5em;
.button-inner {
flex-flow: column;
.icon {
margin-bottom: 10px;
font-size: 2em;
}
}
}
Just add the class name to your button.
<button ion-button full large class="button-icon-top">
<ion-icon name="logo-twitter"></ion-icon>
<span>Click me</span>
</button>
I am using ionic and wanted vertical span elements to have a certain distance between.
Here is a demo:
http://play.ionic.io/app/8682286f77a8
<ion-item class="item item-avatar icon-row">
<img src="img/sale_icon.jpg">
<span style="color:#078d00;font-size:22px; margin-bottom: 1opx"> {{ '36,000' }}</span>
<br>
<span style="color:#4b4b4b;font-size:18px"> Sale</span>
</ion-item>
However, margin-bottom is not working at all.
I could use another br between these spans and that works but it create more distance than I really want to.
What does really look wrong with it in above piece of code?
Just add display: inherit; property to span. It will work.
Plus, whenever you add some styling in ionic framework, don't forget to add !important with styling, because elements in ionic directives inherit styling from parent element.
Add display:inline-block to the span. It will make them respect the margin-bottom property.
<ion-item class="item item-avatar icon-row">
<img src="img/sale_icon.jpg">
<span style="color:#078d00;font-size:22px; margin-bottom: 10px;display:inline-block;"> {{ '36,000' }}</span>
<br>
<span style="color:#4b4b4b;font-size:18px;display:inline-block"> Sale</span>
</ion-item>
I've updated your code -
span {
display: list-item;
list-style-type: none;
}
now margin-bottom works well!!
Full Code -
<ion-item class="item item-avatar icon-row">
<img src="img/sale_icon.jpg">
<span style="color:#078d00;font-size:22px; margin-bottom:10px;"> {{ '36,000' }}</span>
<span style="color:#4b4b4b;font-size:18px;"> Sale</span>
</ion-item>
/* CSS */
span {
display: list-item;
list-style-type: none;
}
Working Demo, with display: list-item you don't need to add <br> after the span.