There are a number of ways to go about styling your Ionic 2 applications, and I have created content around these methods in the past. You can find that content below:
- A Guide to Styling an Ionic 2 Application
- Tips & Tricks for Styling Ionic 2 Applications
- Screencast: Hacking CSS in Ionic 2
One particular method that I have only briefly covered is using the CSS Utility Attributes that Ionic provides. Utility attributes are those little attributes you can add to HTML elements in Ionic 2 like
icon-only. Adding these attributes to an element will change its behaviour or style in some way, and even though you can usually achieve the same result manually with your own CSS rules, using these attributes can save you a lot of time and effort.
The attributes I mentioned above would look like this when applied:
<button ion-button icon-only><ion-icon name="add"></ion-icon></button>
In this post, I am going to introduce you to some of the key utility attributes that Ionic provides. This will be organised into somewhat of a cheat sheet. This isn't intended to be a complete list, it is primarily composed of attributes that I frequently use and find helpful. If there is an attribute that is missing that you think should be listed, feel free to leave it in the comments section.
I am going to categorise the attributes into the elements that they belong to, but first, I wanted to list some generic attributes that can be applied to multiple different elements.
disabled– Can be applied to most input components to disable interaction (also adds disabled styling)
ion-fixed– Adds element to the fixed content area such that it won't be affected by scrolling
tappable– Can be added to elements to remove the 300ms tap delay on mobile. Not required for
<a>. Should be avoided where possible, as using
<a>for tappable elements is better for accessibility.
no-lines– Removes borders from the list
inset– Applies inset styling to the list (adds margin on all sides)
detail-push– These can be used to hide or display the arrow icon on a list item
item-content– These attributes decide where, in relation to other elements, that the item should be placed
text-wrap– Will force text inside of item to wrap onto next line
ion-item is a bit of a special case, in that as well as being used as its own element, it can also be added as an attribute to other elements. Commonly, it is added to a
button to make a clickable list item:
but it can also be applied to any element that you would like to apply the
ion-item styling to. As I mentioned before, it is better to use a
<a> element for tappable elements, so it is preferable to use
<button ion-item></button> instead of
<ion-item tappable></ion-item> even though both are essentially the same.
sticky– Will cause item dividers to stick to the top of the screen when scrolled past (e.g. like in an alphabetised list). Note that this has limited browser support.
- autofocus – Will automatically give focus to the input element
fixed– Positions label to the left of the input element
floating– Causes label to float up and shrink when input is being entered
stacked– Positions label above the input element
ion-button– Applies the Ionic button styling to the button. This should always be included
icon-only– Should be used on buttons that only contain an
icon-left– Positions an
<ion-icon>inside of the button
full– Makes the button full width
round– Styles the button as a block button or round button
outline– Styles the button with a coloured border instead of a solid fill colour
small– Creates smaller or larger buttons
no-padding– Adds or removes padding from the content area
padding-bottom– Adds padding to a specific side of the content area
padding-vertical– Applies padding to the left and right, or top and bottom of the content area
margin– Adds a margin to the content area
margin-bottom– Adds a margin to a specific side of the content area
margin-vertical– Applies a margin to the left and right, or top and bottom of the content area
width-10,20,25,33,34,50,66,67,75,80,90– Controls the width of a grid column
offset-10,20,25,33,34,50,66,67,75,80,90– Offsets a column by the specified amount (adds a left margin)
center– Vertically aligns items inside of a column to the top, bottom, or center of the column
wrap– Will cause columns to wrap onto the next line when there is no space left
center– Vertically aligns items inside of a row to the top, bottom, or center of the row
responsive-lg– Makes the grid responsive by changing the
columnat a certain breakpoint. This will cause columns to display underneath each other, instead of side-by-side. This would allow you to create different layouts on tablets and phones, for example.
- no-border – Removes the border from the header element
- transparent – Adds transparent background to header
end– Will position the buttons contained within the element appropriately
Any more to add? Leave a comment below!