The css of Mini App is basically the same as the css of web. This article does not explain the usage of css. Based on your understanding of css for the web, this article describes some style-related considerations.
This article focuses on styling considerations for vue pages.
Common css units supported by Mini App
include px, rpx
The vue page supports the following common H5 units:
The following is a detailed description of rpx
:
Designers generally only provide drawing with one resolution while providing design drawing.
If you develop strictly according to the px marked by the design icon, the interface is easily deformed on mobile phones of different widths.
Width deformation is dominant. Generally, the height is not easy to go wrong on account of the scroll bar. As a result, a strong demand for dynamic width unit is triggered.
rpx is a unit relative to the reference width, which can be adapted to the screen width. Mini App
The specified screen reference width is 750rpx.
Developers can calculate the rpx value of page elements based on the reference width of design draft. The conversion formula between design draft 1px and frame style 1rpx is as follows:
Design draft 1px / Design draft base width = Frame style 1rpx / 750rpx
In other words, the formula for calculating the width of the page element width in Mini App
is as follows:
750 * The width of the element in the design draft / the base width of the design draft
For example:
Mini App
should be set to: 750 * 100 / 750
, the result is: 100rpx.
Mini App
should be set to: 750 * 100 / 640
, the result is: 117rpx.
Mini App
should be set to: 750 * 200 / 375
, the result is: 400rpx.Tips
Use the @import
statement to import the external style sheet, @import
is followed by the relative path of the external style sheet to be imported, and ;
indicates the end of the statement.
Sample code:
<style>
@import "../../common/neu.css";
.neu-card {
box-shadow: none;
}
</style>
Frame components support the use of style and class attributes to control the style of components.
<view :style="{color:color}" />
<view class="normal_view" />
Currently supported selectors are:
selector | sample | sample description |
---|---|---|
.class | .intro | Select all components with class="intro" |
#id | #firstname | Select the component with id="firstname" |
element | view | Select all view components |
element, element | view, checkbox | Selects all document view components and all checkbox components |
::after | view::after | Insert content after the view component, only valid for vue pages |
::before | view::before | Insert content before the view component, only valid for vue pages |
Notice:
The *
selector cannot be used in Mini App
.
page
is equivalent to the body
node, for example:
page {
background-color: #ccc;
}
The styles defined in App.vue are global styles, which act on every page. The styles defined in the vue file in the pages directory are local styles, which only act on the corresponding pages and will cover the same selectors in App.vue.
Notice:
@import
statement, which also applies to each page.Mini App provides built-in CSS variables
CSS Variables | Description | Mini App | H5 |
---|---|---|---|
--status-bar-height | system status bar height | system status bar height | 0 |
--window-top | Distance of content area from top | 0 | Height of NavigationBar |
--window-bottom | Distance of content area from bottom | 0 | Height of TabBar |
Notice:
"navigationStyle":"custom"
to cancel the native navigation bar, because the form is immersive, it occupies the position of the status bar. At this point, you can use a view with a height of var(--status-bar-height)
at the top of the page to avoid the page content from appearing in the status bar.--window-bottom
, whichever side is fixed above the tabbar.Code block
The quick way to write css variables is: type hei in css, and you can see 3 css variables in the candidate assistant. (HBuilderX 1.9.6 and above are supported)
Example 1 - normal page using css variables:
<template>
<page-meta>
<navigation-bar />
</page-meta>
<view>
<view class="status_bar">
<!-- Here is the status bar -->
</view>
<view>
Text under the status bar
</view>
</view>
</template>
<style>
.status_bar {
height: var(--status-bar-height);
width: 100%;
}
</style>
<template>
<view>
<view class="toTop">
<!-- An up arrow can be put here, which shifts up 10px from the bottom tabbar-->
</view>
</view>
</template>
<style>
.toTop {
bottom: calc(var(--window-bottom) + 10px);
}
</style>
The height of the following components in Mini App
is fixed and cannot be modified:
Components | Description | App | H5 |
---|---|---|---|
NavigationBar | Navigation Bar | 44px | 44px |
TabBar | Bottom Tab | 50px | 50px |
In order to support cross-platform, the framework recommends using Flex layout. For Flex layout, please refer to the external document A Complete Guide to Flexbox and so on.
Mini App
supports setting a background image in css. The usage method is basically the same as that of a normal web
project, but the following points should be noted:
Mini App
is compiled to a platform that does not support local background images, it will be automatically converted to base64 format;.test2 {
background-image: url('~@/static/logo.png');
}
Mini App
supports the use of font icons in the same way as normal web
projects. Note the following:
https
.
Mini App
will automatically convert it to base64 format;@font-face {
font-family: test1-icon;
src: url('~@/static/iconfont.ttf');
}
var domModule = weex.requireModule('dom');
domModule.addRule('fontFace', {
fontFamily: 'fontFamilyName',
src: "url('https://...')",
});
Example:
<template>
<view>
<view>
<text class="test"></text>
<text class="test"></text>
<text class="test"></text>
</view>
</view>
</template>
<style>
@font-face {
font-family: 'iconfont';
src: url('https://at.alicdn.com/t/font_865816_17gjspmmrkti.ttf') format('truetype');
}
.test {
font-family: iconfont;
margin-left: 20rpx;
}
</style>