English
uni-app 有 vue 页面、nvue 页面、uvue页面。
The css of uni-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.
本文重点介绍 vue 页面,也就是webview中的样式注意事项。
uni-app 支持less、sass、scss、stylus等预处理器。
参考: css预处理器
Common css units supported by uni-app
include px, rpx
The vue page supports the following common H5 units, but not in nvue:
nvue doesn't support percent units yet.
On the App side, the units involved in the titleNView in pages.json or the plus api written in the page only support px. Note that rpx is not supported at this time
In nvue, uni-app mode (Introduction to different compilation modes of nvue) can use px and rpx, the performance is basically the same as in vue, and [dynamicRpx] is also enabled ](/collocation/pages?id=globalstyle) can adapt to dynamic changes in screen size. The weex mode currently follows the unit of weex, which is special:
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.
WeChat applet designed rpx to solve this problem. uni-app
supports rpx
on both the App side and H5 side, and can configure the calculation method of different screen widths. For details, please refer to: [rpx calculation configuration](https://uniapp.dcloud.io/collocation/pages ?id=globalstyle).
rpx is a unit relative to the reference width, which can be adapted to the screen width. uni-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 uni-app
is as follows:
750 * The width of the element in the design draft / the base width of the design draft
For example:
uni-app
should be set to: 750 * 100 / 750
, the result is: 100rpx.uni-app
should be set to: 750 * 100 / 640
, the result is: 117rpx.uni-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/uni.css";
.uni-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 uni-app
.
Only class selectors are supported in WeChat Mini Program custom components
page
is equivalent to the body
node, for example:
<!-- 设置页面背景颜色,使用 scoped 会导致失效 -- >
page {
background-color: #ccc;
}
web端可以使用html
、body
、:root
等选择器。由于页面的css样式隔离,且html节点并未添加data-xxx属性,html
、:root
写在页面style内无效,只能写在App.vue内。
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.uni-app provides built-in CSS variables
CSS Variables | Description | App | Mini Programs | H5 |
---|---|---|---|---|
--status-bar-height | system status bar height | system status bar height, nvue note below | 25px | 0 |
--window-top | Distance of content area from top | 0 | 0 | Height of NavigationBar |
--window-bottom | Distance of content area from bottom | 0 | 0 | Height of TabBar |
Notice:
var(--status-bar-height)
This variable is a fixed 25px
in the WeChat applet environment, and the actual status bar height of the mobile phone in the App."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.--status-bar-height
variable on the App side. The alternative is to obtain the status bar height through uni.getSystemInfoSync().statusBarHeight when the page is onLoad, and then use style binding to place a placeholder view sets the height. Sample code is provided belowCode 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>
<!-- HBuilderX 2.6.3+ added page-meta, details: https://uniapp.dcloud.io/component/page-meta -->
<page-meta>
<navigation-bar />
</page-meta>
<view>
<view class="status_bar">
<!-- Here is the status bar -->
</view>
<view>状态栏下的文字</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>
Example 2 - nvue page get status bar height
<template>
<view class="content">
<view :style="{ height: iStatusBarHeight + 'px'}"></view>
</view>
</template>
<script>
export default {
data() {
return {
iStatusBarHeight: 0,
};
},
onLoad() {
this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
},
};
</script>
The height of the following components in uni-app
is fixed and cannot be modified:
Components | Description | App | H5 |
---|---|---|---|
NavigationBar | Navigation Bar | 44px | 44px |
TabBar | Bottom Tab | Before HBuilderX 2.3.4, it was 56px. From 2.3.4 onwards, it is adjusted to be consistent with H5, and unified to 50px. (but the height can be changed independently) | 50px |
The heights of various mini-program platforms, including iOS and Android of the same mini-program platform, are also different.
##Flex layout
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, Ruanyifeng's flex tutorial and so on.
uni-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:
uni-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');
}
Notice
uni-app
supports the use of font icons in the same way as normal web
projects. Note the following:
https
.uni-app
will automatically convert it to base64 format;@font-face {
font-family: test1-icon;
src: url('~@/static/iconfont.ttf');
}
In nvue
, you cannot directly use css to import font files, you need to use the following methods to import font files in js. Importing fonts from local paths is not supported in nvue, please use the network link or base64
form. **The url
of the src
field must be enclosed in single quotes. **
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>