We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

CSS

Cascading Style Sheets (CSS) is a language used to describe the HML page structure. All HML components have default styles. You can customize styles for these components using CSS to design various pages.

Style Import

PhonePC/2in1TabletTVWearableLite Wearable

CSS files can be imported using the @import statement. This facilitates module management and code reuse.

Style Declaration

PhonePC/2in1TabletTVWearableLite Wearable

The .css file with the same name as the .hml file in each page directory describes the styles of components on the HML page, determining how the components will be displayed.

  1. Internal style: The style and class attributes can be used to specify the component style. Sample code:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <!-- index.hml -->
    2. <div class="container">
    3. <text style="color: red">Hello World</text>
    4. </div>
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* index.css */
    2. .container {
    3. justify-content: center;
    4. }
  2. External style files: You need to import the files. For example, create a style.css file in the common directory and import the file at the beginning of index.css.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* style.css */
    2. .title {
    3. font-size: 50px;
    4. }
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* index.css */
    2. @import '../../common/style.css';
    3. .container {
    4. justify-content: center;
    5. }

Selectors

PhonePC/2in1TabletTVWearableLite Wearable

A CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors.

Expand
Selector Example Description
.class .container Selects all components whose class is container.
#id #titleId Selects all components whose id is titleId.
, .title, .content Selects all components whose class is title or content.

Example:

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- Pagelayoutexample.hml -->
  2. <div id="containerId" class="container">
  3. <text id="titleId" class="title">Title</text>
  4. <div class="content">
  5. <text id="contentId">Content</text>
  6. </div>
  7. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* Page style xxx.css */
  2. /* Set the style for the components whose class is title. */
  3. .title {
  4. font-size: 30px;
  5. }
  6. /* Set the style for the components whose id is contentId. */
  7. #contentId {
  8. font-size: 20px;
  9. }
  10. /* Set padding for all components of the title or content class to 5 px. */
  11. .title, .content {
  12. padding: 5px;
  13. }

Pseudo-classes

PhonePC/2in1TabletTVWearableLite Wearable

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected elements.

Expand
Name Available Components Description
:active

input[type="button"]

Selects the element activated by a user, for example, a pressed button. Only the background-color and background-image attributes can be set for the pseudo-class selector on mini-system wearables.
:checked input[type="checkbox", type="radio"] Selects the element whose checked attribute is true. Only the background-color and background-image attributes can be set for the pseudo-class selector on mini-system wearables.

The following is an example for you to use the :active pseudo-class to control the style when a user presses the button.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- index.hml -->
  2. <div class="container">
  3. <input type="button" class="button" value="Button"></input>
  4. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* index.css */
  2. .button:active {
  3. background-color: #888888;/* After the button is activated, the background color is changed to #888888. */
  4. }

Precompiled Styles

PhonePC/2in1TabletTVWearableLite Wearable

Precompilation is a program that uses specific syntax to generate CSS files. It provides variables and calculation, helping you define component styles more conveniently. Currently, Less, Sass, and Scss are supported. To use precompiled styles, change the suffix of the original .css file. For example, change index.css to index.less, index.sass, or index.scss.

  • The following index.less file is changed from index.css.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* index.less */
    2. /* Define a variable. */
    3. @colorBackground: #000000;
    4. .container {
    5. background-color: @colorBackground; /* Use the variable defined in the .less file. */
    6. }
  • Reference a precompiled style file. For example, if the style.scss file is located in the common directory, change the original index.css file to index.scss and import style.scss.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* style.scss */
    2. /* Define a variable. */
    3. $colorBackground: #000000;

    Reference the precompiled style file in index.scss:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. /* index.scss */
    2. /* Import style.scss. */
    3. @import '../../common/style.scss';
    4. .container {
    5. background-color: $colorBackground; /* Use the variable defined in style.scss. */
    6. }
    NOTE

    Place precompiled style files in the common directory.

Search in References
Enter a keyword.