WXSS (WeiXin Style Sheets) is a set of style languages used to describe the component styles of WXML.
To cater to the vast number of front-end developers, WXSS possesses most of the characteristics of CSS. Here are some common WXSS syntaxes.
Style Rules Definition
Similar to CSS, you can specify node styles using selectors.
/* page.wxss */
/* Define the style for nodes whose class contains important-text */
.important-text {
font-size: 1.2em;
color: red;
}
The style rules above can be applied to the corresponding WXML.
<!-- page.wxml -->
<view class="important-text"> Important information </view>
Using Selectors Appropriately
WXSS supports most CSS selectors, but with a few exceptions: attribute selectors [...] do not take effect; pseudo-class and pseudo-element selectors with parameters are not supported.
As a best practice, only class selectors are recommended when writing stylesheets, as this often makes the code clearer. Here is a simple example:
.hint {
padding: 5px;
}
.important-text {
font-size: 1.2em;
color: red;
}
.minor-text {
font-size: 0.8em;
color: #666;
}
<!-- page.wxml -->
<view class="hint important-text"> Important information </view>
<view class="hint"> Information </view>
<view class="hint minor-text"> Notes </view>
Inline Styles
You can also write style rules directly in the style attribute of nodes within WXML. For example:
<!-- You can write styles in style="...", but use with caution in practice -->
<view style="font-size: 1.2em; color: red;"> Important Information </view>
From a best practices perspective, style attributes should not contain long style rules, as this significantly reduces code readability. Typically, this approach should only be used when data binding is required, for example:
<!-- page.wxml -->
<view style="color: {{color}};"> {{colorName}} </view>
// page.js
Page({
data: {
color: 'yellow',
colorName: 'Yellow',
},
})
Style Import
Similar to CSS, you can use @import to import other WXSS files. This is a simple way to share style code across pages.
/** common.wxss **/
.hint {
padding: 5px;
}
/** page-a.wxss **/
@import "common.wxss"; /* Equivalent to inserting the contents of common.wxss here */
.minor-text {
font-size: 0.8em;
color: #666;
}
/** page-b.wxss **/
@import "common.wxss"; /* Equivalent to inserting the contents of common.wxss here */
.important-text {
font-size: 1.2em;
color: red;
}
Global Styles
Styles in the app.wxss file are global styles and generally apply to all pages (pages can also be configured to prevent app.wxss from taking effect).
From a best practices perspective, app.wxss is only suitable for styles that are essential for the vast majority of pages. Using the @import style import method is often more flexible.
Length Units Based on Screen Width Proportion
In practice, the dimensions of some page content need to be determined based on screen size.
In this case, you can use the CSS standard vw length unit. 1vw equals one percent of the total page width. For example, if the page width is 375px, 1vw is equivalent to 3.75px.
Due to historical reasons, WXSS also supports a similar length unit rpx, which represents 1 / 750 of the total page width. For example, if the page width is 375px, 1rpx is equivalent to 0.5px. However, the calculation rules for rpx sometimes round the converted values, leading to precision issues. To avoid this, you may consider enabling the convertRpxToVw option. Additionally, since the CSS standard vw unit is now widely supported, we recommend prioritizing the use of vw.
Note: Considering support for large-screen devices such as mobile phones and PCs, it is not recommended to abuse screen-width-proportional units like vw and rpx; if unavoidable, please combine them with responsive layout methods such as Media Query. For details, refer to Responsive Display Area Changes.
Impact of the Rendering Engine on WXSS
Please note that the rendering engine imposes additional restrictions on WXSS syntax:
- If using the default WebView rendering engine, since the built-in WebView engines vary across different operating systems, please be aware that stylesheet compatibility may also be affected by different OS versions;
- If using the Skyline rendering engine, please refer to its relevant documentation for additional details.