Showing posts with label Ionic CSS Components. Show all posts
Showing posts with label Ionic CSS Components. Show all posts

Thursday, January 17, 2019

Ionic - Header

The Ionic header bar is located on top of the screen. It can contain title, icons, buttons or some other elements on top of it. There are predefined classes of headers that you can use. You can check all of it in this tutorial.

Adding Header

The main class for all the bars you might use in your app is bar. This class will always be applied to all the bars in your app. All bar subclasses will use the prefix – bar.
If you want to create a header, you need to add bar-header after your main bar class. Open your www/index.html file and add the header class inside your body tag. We are adding a header to the index.html body because we want it to be available on every screen in the app.
Since bar-header class has default (white) styling applied, we will add the title on top of it, so you can differentiate it from the rest of your screen.
<div class = "bar bar-header">
   <h1 class = "title">Header</h1>
</div>
The above code will produce the following screen −
Ionic Header

Header Colors

If you want to style your header, you just need to add the appropriate color class to it. When you style your elements, you need to add your main element class as prefix to your color class. Since we are styling the header bar, the prefix class will be bar and the color class that we want to use in this example is positive (blue).
<div class = "bar bar-header bar-positive">
   <h1 class = "title">Header</h1>
</div>
The above code will produce the following screen −
Ionic Header Color
You can use any of the following nine classes to give a color of your choice to your app header −
Color ClassDescriptionResult
bar-lightTo be used for white color 
bar-stableTo be used for light grey color 
bar-positiveTo be used for blue color 
bar-calmTo be used for light blue color 
bar-balancedTo be used for green color 
bar-energizedTo be used for yellow color 
bar-assertiveTo be used for red color 
bar-royalTo be used for violet color 
bar-darkTo be used for black color 

Header Elements

We can add other elements inside the header. The following code is an example to add a menu button and a home button inside a header. We will also add icons on top of our header buttons.
<div class = "bar bar-header bar-positive">
   <button class = "button icon ion-navicon"></button>
   <h1 class = "title">Header Buttons</h1>
   <button class = "button icon ion-home"></button>
</div>
The above code will produce the following screen −
Ionic Header Buttons

Sub Header

You can create a sub header that will be located just below the header bar. The following example will show how to add a header and a sub header to your app. Here, we have styled the sub header with an "assertive" (red) color class.
<div class = "bar bar-header bar-positive">
   <button class = "button icon ion-navicon"></button>
   <h1 class = "title">Header Buttons</h1>
   <button class = "button icon ion-home"></button>
</div>

<div class = "bar bar-subheader bar-assertive">
   <h2 class = "title">Sub Header</h2>
</div>
The above code will produce the following screen −
Ionic Sub Header
When your route is changed to any of the app screens, you will notice that the header and the sub header are covering some content as shown in the screenshot below.
Ionic Hidden Content
To fix this you need to add a ‘has-header’ or a ‘has-subheader’ class to the ion-content tags of your screens. Open one of your HTML files from www/templates and add the has-subheader class to the ion-content. If you only use header in your app, you will need to add the has-header class instead.
<ion-content class = "padding has-subheader">
The above code will produce the following screen −
Ionic Sub Header

Ionic - Content


Almost every mobile app contains some fundamental elements. Usually those elements include a header and a footer that will cover the top and the bottom part of the screen. All the other elements will be placed between these two. Ionic provides ion-content element that serves as a container that will wrap all the other elements that we want to create.
This container has some unique characteristics, but since this is a JavaScript based component which we will cover in the later part of this tutorial
<div class = "bar bar-header"> 
   <h1 class = "title">Header</h1> 
</div>
 
<div class = "list"> 
   <label class = "item item-input"> 
      <input type = "text" placeholder = "Placeholder 1" /> 
   </label>
   
   <label class = "item item-input"> 
      <input type = "text" placeholder = "Placeholder 2" /> 
   </label> 
</div>

<div class = "bar bar-footer"> 
   <h1 class = "title">Footer</h1> 
</div>

Wednesday, January 16, 2019

Ionic - Colors

Before we start with actual elements available in the Ionic framework, let us have a little understanding on how Ionic makes use of colors for different elements.

Ionic Color Classes

Ionic framework gives us a set of nine predefined color classes. You can use these colors or you can override it with your own styling.
The following table shows the default set of nine colors provided by Ionic. We will use these colors for styling different Ionic elements in this tutorial. For now, you can check all the colors as shown below −
ClassDescriptionResult
lightTo be used for white color
stableTo be used for light grey color
positiveTo be used for blue color
calmTo be used for light blue color
balancedTo be used for green color
energizedTo be used for yellow color
assertiveTo be used for red color
royalTo be used for violet color
darkTo be used for black color

Ionic Color Usage

Ionic makes use of different classes for each element. For example, a header element will have bar class and a button will have a button class. To simplify the usage, we use different colors by prefixing element class in a color name.
For example, to create a blue color header, we will use a bar-calm as follows −
<div class = "bar bar-header bar-calm">
   ...
</div>
Similarly, to create a grey color button, we will use button-stable class as follows.
<div class = "button button-stable">
   ...
</div>
You can also use Ionic color class like any other CSS class. We will now style two paragraphs with a balanced (green) and an energized (yellow) color.
<p class = "balanced">Paragraph 1...</p>
<p class = "energized">Paragraph 2...</p>
The above code will produce the following screen −
Ionic Colors
We will discuss in detail in the subsequent chapters, when we create different elements using different classes.

Customizing Colors with CSS

When you want to change some of the Ionic default colors using CSS, you can do it by editing the lib/css/ionic.css file. In some cases, this approach is not very productive because every element (header, button, footer...) uses its own classes for styling.
Therefore, if you want to change the color of the "light" class to orange, you would need to search through all the elements that use this class and change it. This is useful when you want to change the color of a single element, but not very practical for changing color of all elements because it would use too much time.

Customizing colors using SASS

SASS (which is the short form of – Software as a Service) provides an easier way to change the color for all the elements at once. If you want to use SASS, open your project in the command window and type −
C:\Users\Username\Desktop\tutorialApp> ionic setup sass
This will set up SASS for your project. Now you can the change default colors by opening the scss/ionic.app.scss file and then typing in the following code before this line – @import "www/lib/ionic/scss/ionic";
We will change the balanced color to dark blue and the energized color to orange. The two paragraphs that we used above are now dark blue and orange.
$balanced: #000066 !default;
$energized: #FFA500 !default;
Now, if you use the following example −
<p class = "balanced">Paragraph 1...</p>
<p class = "energized">Paragraph 2...</p>
The above code will produce the following screen −
Ionic Colors SCCS
All the Ionic elements that are using these classes will change to dark blue and orange. Take into consideration that you do not need to use Ionic default color classes. You can always style elements the way you want.

Important Note

The www/css/style.css file will be removed from the header of the index.html after you install SASS. You will need to link it manually if you still want to use it. Open index.html and then add the following code inside the header.
<link href = "css/style.css" rel = "stylesheet">