I started a ionic project based on the sidemenu template. I am trying to change the background color for each element of the side menu (I would like every item to be of a different color).
I tried to add a ionic color class:
<ion-side-menu side="left">
<ion-header-bar class="bar-stable">
<h1 class="title">Left</h1>
</ion-header-bar>
<ion-content>
<ion-list>
<ion-item class="positive-bg" nav-clear menu-close ng-click="login()">
Login
</ion-item>
<ion-item class="calm-bg" nav-clear menu-close href="#/app/search">
Search
</ion-item>
<ion-item class="assertive-bg" nav-clear menu-close href="#/app/browse">
Browse
</ion-item>
<ion-item nav-clear menu-close class="balanced-bg" href="#/app/playlists">
Playlists
</ion-item>
</ion-list>
</ion-content>
It works well on the Login element but not on the other elements. Removing the href attribute on other elements works... How can I have the background color and the href element?
The problem is that
ion-item
directive rendersa
element with class.item-content
inside if it hashref
attribute. The classes you apply toion-item
elements are defined as something like this:and at the same class ionic defines these styles for links inside as
Later rule has higher specificity so it takes precedence over simple
.assertive-bg
class rule.To workaround this issue you want to create few additional rules manually:
Demo: http://plnkr.co/edit/bbUel2goJGYy8fv0ltXm?p=preview
UPD. brandyshea provided much better solution of how styles should be overloaded without color values duplication. I will keep my answer for the sake of explanation it provides, though.
An
ion-item
with ahref
attribute renders differently. For more info on the why see dfsq's answerWhat you could do is instead of using the
ion-list
directive, use the classes:Demo on Codepen
There are a few ways to do this. Here is another way:
Add a custom class to your side-menu list:
And then tell item-content to inherit the background-color from the item:
Codepen demo