Styling FAQ
This document addresses common styling-related questions and issues in uni-app development.
Basic Styling Issues
Q: How to set global styles?
A: You can set global styles in App.vue
:
<style>
/* Global styles */
page {
background-color: #f5f5f5;
font-size: 14px;
}
.container {
padding: 20px;
}
/* Global class styles */
.text-center {
text-align: center;
}
.flex-row {
display: flex;
flex-direction: row;
}
</style>
Q: How to use CSS variables?
A: uni-app supports CSS custom properties (CSS variables):
<template>
<view class="container">
<text class="title">Title</text>
<text class="content">Content</text>
</view>
</template>
<style>
:root {
--primary-color: #007aff;
--text-color: #333;
--border-radius: 8px;
}
.container {
color: var(--text-color);
}
.title {
color: var(--primary-color);
border-radius: var(--border-radius);
}
</style>
Q: How to import external CSS files?
A: You can import CSS files using @import
:
<style>
@import url('./common.css');
@import './styles/base.css';
.custom-class {
/* Custom styles */
}
</style>
Layout Issues
Q: How to implement responsive layout?
A: Use media queries and flexible units:
<template>
<view class="responsive-container">
<view class="item">Item 1</view>
<view class="item">Item 2</view>
<view class="item">Item 3</view>
</view>
</template>
<style>
.responsive-container {
display: flex;
flex-wrap: wrap;
padding: 20rpx;
}
.item {
width: 100%;
margin-bottom: 20rpx;
}
/* Tablet */
@media screen and (min-width: 768px) {
.item {
width: 48%;
margin-right: 2%;
}
}
/* Desktop */
@media screen and (min-width: 1024px) {
.item {
width: 31%;
margin-right: 2%;
}
}
</style>
Q: How to center elements vertically and horizontally?
A: Use flexbox for centering:
<template>
<view class="center-container">
<view class="centered-content">Centered Content</view>
</view>
</template>
<style>
.center-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.centered-content {
padding: 20rpx;
background-color: #f0f0f0;
border-radius: 10rpx;
}
</style>
Q: How to implement sticky positioning?
A: Use position: sticky
:
<template>
<view class="container">
<view class="sticky-header">Sticky Header</view>
<view class="content">
<!-- Long content -->
</view>
</view>
</template>
<style>
.sticky-header {
position: sticky;
top: 0;
background-color: #fff;
padding: 20rpx;
border-bottom: 1px solid #eee;
z-index: 100;
}
.content {
height: 2000rpx; /* Long content for scrolling */
padding: 20rpx;
}
</style>
Font and Icon Issues
Q: How to use custom fonts?
A: Import and use custom fonts:
<style>
@font-face {
font-family: 'CustomFont';
src: url('./fonts/custom-font.ttf') format('truetype');
}
.custom-text {
font-family: 'CustomFont', sans-serif;
font-size: 16px;
}
</style>
Q: How to use icon fonts?
A: Import icon font files and use them:
<template>
<text class="iconfont icon-home"></text>
<text class="iconfont icon-user"></text>
</template>
<style>
@font-face {
font-family: 'iconfont';
src: url('./fonts/iconfont.ttf') format('truetype');
}
.iconfont {
font-family: 'iconfont' !important;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-home:before {
content: "\e600";
}
.icon-user:before {
content: "\e601";
}
</style>
Q: How to adjust font size for different platforms?
A: Use conditional compilation:
<style>
.text {
/* #ifdef APP-PLUS */
font-size: 16px;
/* #endif */
/* #ifdef H5 */
font-size: 14px;
/* #endif */
/* #ifdef MP-WEIXIN */
font-size: 30rpx;
/* #endif */
}
</style>
Animation and Transition Effects
Q: How to implement CSS animations?
A: Use CSS animations and transitions:
<template>
<view class="animated-box" @click="toggleAnimation">
Click me
</view>
</template>
<script>
export default {
data() {
return {
isAnimating: false
}
},
methods: {
toggleAnimation() {
this.isAnimating = !this.isAnimating;
}
}
}
</script>
<style>
.animated-box {
width: 100px;
height: 100px;
background-color: #007aff;
transition: all 0.3s ease;
cursor: pointer;
}
.animated-box:hover {
transform: scale(1.1);
background-color: #0056cc;
}
/* Keyframe animation */
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
.bounce {
animation: bounce 1s infinite;
}
</style>
Q: How to implement page transition animations?
A: Use uni-app's page transition API:
// In pages.json
{
"pages": [
{
"path": "pages/index/index",
"style": {
"animationType": "slide-in-right",
"animationDuration": 300
}
}
]
}
<!-- In page component -->
<template>
<view class="page-container">
<!-- Page content -->
</view>
</template>
<style>
.page-container {
opacity: 0;
transform: translateX(100%);
animation: slideIn 0.3s ease-out forwards;
}
@keyframes slideIn {
to {
opacity: 1;
transform: translateX(0);
}
}
</style>
Style Compatibility Issues
Q: How to handle style differences across platforms?
A: Use conditional compilation and platform-specific styles:
<style>
.button {
padding: 10px 20px;
border-radius: 5px;
/* #ifdef APP-PLUS */
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
/* #endif */
/* #ifdef H5 */
cursor: pointer;
user-select: none;
/* #endif */
/* #ifdef MP-WEIXIN */
border: 1px solid #ddd;
/* #endif */
}
</style>
Q: How to handle rpx unit compatibility?
A: Use rpx for responsive design, px for fixed sizes:
<style>
.container {
width: 750rpx; /* Full width on all devices */
padding: 20rpx; /* Responsive padding */
}
.fixed-element {
width: 1px; /* Fixed 1px border */
height: 44px; /* Fixed navigation height */
}
</style>
Q: How to handle safe area adaptation?
A: Use CSS environment variables:
<style>
.safe-area-container {
padding-top: constant(safe-area-inset-top);
padding-top: env(safe-area-inset-top);
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
.bottom-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
</style>
Common Style Implementations
Q: How to implement a card layout?
A: Create reusable card components:
<template>
<view class="card">
<view class="card-header">
<text class="card-title">Card Title</text>
</view>
<view class="card-body">
<text class="card-content">Card content goes here...</text>
</view>
<view class="card-footer">
<button class="card-button">Action</button>
</view>
</view>
</template>
<style>
.card {
background-color: #fff;
border-radius: 8rpx;
box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
margin: 20rpx;
overflow: hidden;
}
.card-header {
padding: 30rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.card-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.card-body {
padding: 30rpx;
}
.card-content {
font-size: 28rpx;
color: #666;
line-height: 1.5;
}
.card-footer {
padding: 20rpx 30rpx;
background-color: #f9f9f9;
text-align: right;
}
.card-button {
background-color: #007aff;
color: #fff;
border: none;
padding: 10rpx 20rpx;
border-radius: 4rpx;
font-size: 26rpx;
}
</style>
Q: How to implement a grid layout?
A: Use CSS Grid or Flexbox:
<template>
<view class="grid-container">
<view class="grid-item" v-for="item in items" :key="item.id">
{{ item.title }}
</view>
</view>
</template>
<style>
/* CSS Grid approach */
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200rpx, 1fr));
gap: 20rpx;
padding: 20rpx;
}
.grid-item {
background-color: #f0f0f0;
padding: 30rpx;
border-radius: 8rpx;
text-align: center;
}
/* Flexbox approach */
.flex-grid {
display: flex;
flex-wrap: wrap;
margin: -10rpx;
}
.flex-item {
flex: 0 0 calc(50% - 20rpx);
margin: 10rpx;
background-color: #f0f0f0;
padding: 30rpx;
border-radius: 8rpx;
}
</style>
Q: How to implement a loading spinner?
A: Create CSS-based loading animations:
<template>
<view class="loading-container" v-if="loading">
<view class="spinner"></view>
<text class="loading-text">Loading...</text>
</view>
</template>
<style>
.loading-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 40rpx;
}
.spinner {
width: 40rpx;
height: 40rpx;
border: 4rpx solid #f3f3f3;
border-top: 4rpx solid #007aff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading-text {
margin-top: 20rpx;
font-size: 28rpx;
color: #666;
}
</style>
Best Practices
1. Use Semantic Class Names
/* Good */
.product-card { }
.user-avatar { }
.navigation-menu { }
/* Avoid */
.red-box { }
.big-text { }
.left-item { }
2. Organize Styles Hierarchically
<style>
/* Base styles */
.container { }
/* Component styles */
.header { }
.header-title { }
.header-nav { }
/* State styles */
.is-active { }
.is-disabled { }
.is-loading { }
/* Modifier styles */
.button--primary { }
.button--secondary { }
.button--large { }
</style>
3. Use CSS Custom Properties for Theming
<style>
:root {
--color-primary: #007aff;
--color-secondary: #34c759;
--color-danger: #ff3b30;
--spacing-small: 10rpx;
--spacing-medium: 20rpx;
--spacing-large: 40rpx;
}
.theme-dark {
--color-primary: #0a84ff;
--color-background: #1c1c1e;
--color-text: #ffffff;
}
</style>
4. Optimize for Performance
<style>
/* Use transform for animations */
.slide-enter-active {
transform: translateX(100%);
transition: transform 0.3s ease;
}
.slide-enter-to {
transform: translateX(0);
}
/* Avoid expensive properties in animations */
/* Bad */
.expensive-animation {
animation: badAnimation 1s ease;
}
@keyframes badAnimation {
from { width: 0; height: 0; }
to { width: 100px; height: 100px; }
}
/* Good */
.efficient-animation {
animation: goodAnimation 1s ease;
}
@keyframes goodAnimation {
from { transform: scale(0); }
to { transform: scale(1); }
}
</style>
Troubleshooting
Common Issues and Solutions
- Styles not applying: Check selector specificity and CSS syntax
- Layout breaking on different devices: Use responsive units (rpx, %, vw/vh)
- Animations not smooth: Use transform and opacity properties
- Font not loading: Verify font file path and format support
- Z-index issues: Establish proper stacking context
Debugging Tips
- Use browser developer tools for H5 platform
- Check uni-app console for style warnings
- Test on multiple devices and platforms
- Validate CSS syntax and properties
- Use conditional compilation for platform-specific fixes
For more styling guidance, refer to the Components Documentation and Best Practices Guide.