单位

uniapp支持通用的css单位包括px与rpx。不推荐使用upx。

样式导入

使用@import语句可以导入外联样式表

  1. <style>
  2. @import "../../common/uni.css";
  3. .uni-card {
  4. box-shadow: none;
  5. }
  6. </style>

设置背景

在uniapp不能使用*选择器,page相当于body

  1. <!-- 设置页面背景颜色 -->
  2. page {
  3. background-color:#ccc;
  4. }

全局样式与局部样式

定义在 App.vue 中的样式为全局样式,作用于每一个页面。在 pages 目录下 的 vue 文件中定义的样式为局部样式,只作用在对应的页面,并会覆盖 App.vue 中相同的选择器。
在app.vue通过@import导入的样式同样作用于每个页面

css变量

uniapp提供内置的css变量

CSS变量 描述 App 小程序 H5
—status-bar-height 系统状态栏高度 系统状态栏高度
、nvue注意见下
25px 0
—window-top 内容区域距离顶部的距离 0 0 NavigationBar 的高度
—window-bottom 内容区域距离底部的距离 0 0 TabBar 的高度

快速书写css变量的方法是:在css中敲hei,在候选助手中即可看到3个css变量。(HBuilderX 1.9.6以上支持)

示例1 - 普通页面使用css变量:

  1. <template>
  2. <!-- HBuilderX 2.6.3+ 新增 page-meta, 详情:https://uniapp.dcloud.io/component/page-meta -->
  3. <page-meta>
  4. <navigation-bar />
  5. </page-meta>
  6. <view>
  7. <view class="status_bar">
  8. <!-- 这里是状态栏 -->
  9. </view>
  10. <view> 状态栏下的文字 </view>
  11. </view>
  12. </template>
  13. <style>
  14. .status_bar {
  15. height: var(--status-bar-height);
  16. width: 100%;
  17. }
  18. </style>
  1. <template>
  2. <view>
  3. <view class="toTop">
  4. <!-- 这里可以放一个向上箭头,它距离底部tabbar上浮10px-->
  5. </view>
  6. </view>
  7. </template>
  8. <style>
  9. .toTop {
  10. bottom: calc(var(--window-bottom) + 10px)
  11. }
  12. </style>

示例2 - nvue页面获取状态栏高度

  1. <template>
  2. <view class="content">
  3. <view :style="{ height: iStatusBarHeight + 'px'}"></view>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. iStatusBarHeight:0
  11. }
  12. },
  13. onLoad() {
  14. this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight
  15. }
  16. }
  17. </script>

uniapp支持在templat中嵌套block用来进行列表渲染和条件渲染。