Styling
In the following article, we are demostrating different scenarios for setting CSS styles via classes, selectors, IDs and use cases for setting CSS via code-behind files.
Basics
The following example demonstrates, how to set up CSS class
, id
and state selector while using component behind via code-behind.
Border Basics
CSS Property | JavaScript | Property Description |
---|---|---|
border-color | borderColor | Sets a border color to the matched view’s. Accepts hex color value or Color instance. |
border-width | borderWidth | Sets a border width to the matched view’s. Accepts number value as a DIP (Device independent pixels). |
border-radius | borderRadius | Sets a border radius to the matched view’s. Accepts number value as a DIP (Device independent pixels). |
Setting border properties thought CSS class.
.border-props {
border-width: 3;
border-color: orangered;
border-radius: 20;
}
Border Code Behind
Borders can be set dynamically via code-behind implementation.
Border Radius
The border-radius
property allows us to create rounded corners for NativeScript elements.
This property can have from one, two or four values.
Rules about applying border-radius
values:
Four values -
border-radius: 15 50 30 5
; First value applies to the top-left corner, second value applies to the top-right corner, third value applies to bottom-right corner, and fourth value applies to the bottom-left corner.Two values -
border-radius: 5 10;
First value applies to top-left and bottom-right corners, and the second value applies to top-right and bottom-left corners.One value -
border-radius: 10;
The value applies to all four corners, which are rounded equally.
.no-top-left {
border-radius: 0 20 20 20;
}
.no-top-left-right {
border-radius: 0 0 20 20;
}
.no-bottom-left {
border-radius: 20 20 20 0;
}
.no-bottom-left-right {
border-radius: 20 20 0 0;
}
.radius-all-corners {
border-radius: 20;
}
.no-radius-at-all {
border-radius: 0;
}
.diagonal {
border-radius: 20 0;
}
.reverse-diagonal {
border-radius: 0 20;
}
Gradients
NativeScript supports creating linear gradients via the property linear-gradient
which can be applied to CSS properties background-image
and background
.
/* Setting linear gradients */
Label {
padding: 8;
color:white;
}
.bottom-gradient {
background: linear-gradient(to bottom, orangered, green, lightblue);
}
.left-gradient {
background: linear-gradient(to left, orangered, green, lightblue);
}
.right-gradient {
background: linear-gradient(to right, orangered, green, lightblue);
}
.degree-gradient {
background: linear-gradient(45deg, orangered, green, lightblue);
}
.two-color-gradient {
background: linear-gradient(-45deg, orangered, lightblue);
}
.background-image {
background-image:linear-gradient(to bottom, orangered, green, lightblue);
}