What is CSS ??
I think you know about CSS, but still let me tell you that CSS stands for Cascading Style Sheets. CSS describes how HTML (XAML) elements should be displayed on the screen, paper ,or other media. CSS saves a lot of work. It can control the layout of many web pages at once. We can use it in three ways through inline, internal and external.
Now, let's come back to Xamarin.forms. Here I am telling you how to use CSS on xaml.
- Here we create a project solution whose name is xaml with CSS. Go to vs2017 and select file then choose new and select project.
- If you want to use shortcut key, press Ctr+Shift+N.
- Now here a dialog box appears, and then select cross-platform, give the app a name, then choose a path where you want to place your project.
- Now one more dialog box appears here, choose a blank app and then select .netStenderd(PCL) then press enter.
- Before working on CSS please make sure your xamarin forms package is updated. CSS Styling is supported after 2.6.0 or higher. This feature allows you to use CSS xamarin application in the web app.
- And if not updated, please update to the latest version.
- Now, in this project we create a folder named Assets, you can give the name as whatever you like.
- Now here we are adding a CSS file, go to the Assets folder and then right click on the folder, select Add then click on new items.
- Here appears a dialog box and then select the web (this option is showing on the left side) and then select CSS file. Give it a name StylingwithCSS and press enter.
Firstly we can change the Build Action EmbeddedResource, Default to set the content so, the process is: In vs2017 right click on CSS file, go to properties, then change to build action. And In Mac xamarin studio, right click on CSS file, then choose build action, then hit the EmbeddedResource.
CSS Benifits
Here are some benefits of CSS in Zamarin.Forms,
- Essentially, HTML for both Web and XAML for Xamarin.Forms makes both visual trees.
- Many Xamarin developers come from an ASP.NET background so it is good for asp developers.
- CSS is powerful and loved by many people.
- Developers have to reuse skills before creating web applications.
- Compared to XAML style, CSS can be easy to learn and reduces code.
- CSS needs to be mapped with built-in XAML styles.
- All inline XAML markup for style can be moved to CSS files.
- CSS style offers some unique advantages with styling inheritance.
- CSS Web and Xamarin.Forms provide the ability to sharecodesof styles between apps.
- CSS usage is another option and developers do not complain about flexibility.
Element Property Suport
CSS can access any styling property for XML elements. Here are some of the most common styling properties familiar with XAML developers,- color
- background-color
- font-family
- font-size
- font-style
- height
- width
- border-color
- border-width
- visibility
- opacity
- text-align
- margin - left | right | top | bottom
- padding - left | right | top | bottom
CSS selectors determine which element to be used and these asset values can be set to XAML.
Style Classes
As you can expect, you can assign a CSS class to XAML elements and define the style in your CSS file - in terms of property value disputes, the last style wins. Consider the following XML,
- <Label Text="Welcome to Xamarin Forms!" StyleClass="MyLabel" />
XML
The Label has the .MyLabel class applied, which is as follows,
- .MyLabel {
- color: red;
- }
CSS
Here's how it looks at runtime

Raj DoshiPosted Dec 13, 2018, 9:12 PM
We are using button with background color in our form. But it is rendering to device with greyish shade like disabled. It is happening to one type of device which is supporting api level 19. Our application is supporting from 19 to 26. Can you suggest what could be cause?