Nicegui 组件放在页面中间

可以使用以下方法将 组件 放在页面中间:

方法1:使用 ui.column() 居中对齐

python 复制代码
with ui.column().classes('absolute-center items-center'):
    with ui.card():
        ui.label('卡片内容')
        # 其他卡片内容...

方法2:使用 CSS 类

python 复制代码
with ui.card().classes('mx-auto my-auto'):
    ui.label('卡片内容')
    # 其他卡片内容...

方法3:更灵活的布局控制

python 复制代码
with ui.row().classes('w-full h-screen justify-center items-center'):
    with ui.card().classes('w-96'):
        ui.label('卡片内容')
        ui.input(label='输入框')
        ui.button('提交')

方法4:响应式居中

python 复制代码
with ui.column().classes('absolute-center items-center w-full max-w-md'):
    with ui.card().classes('w-full'):
        ui.label('居中卡片')
        with ui.row():
            ui.button('确定')
            ui.button('取消')

方法5:使用网格布局

python 复制代码
with ui.grid(columns=1).classes('h-screen place-items-center'):
    with ui.card():
        ui.label('网格居中卡片')
        # 卡片内容...

完整示例

python 复制代码
from nicegui import ui

# 方法1:推荐使用
with ui.column().classes('absolute-center items-center'):
    with ui.card().classes('w-96 shadow-lg'):
        ui.label('登录').classes('text-2xl font-bold text-center')
        ui.input(label='用户名').classes('w-full')
        ui.input(label='密码', password=True).classes('w-full')
        ui.button('登录', color='primary').classes('w-full')

ui.run()

说明:

  • absolute-center:水平和垂直居中
  • items-center:水平居中(在 column 中)
  • justify-center:垂直居中(在 row 中)
  • mx-auto:水平居中
  • my-auto:垂直居中
  • h-screen:全屏高度
  • w-full:全宽度

选择适合你需求的方法即可。方法1和2最简洁,方法3最灵活。

相关推荐
奋进的芋圆2 分钟前
TokenRetryHelper 详解与 Spring Boot 迁移方案
java·spring boot·后端
我的写法有点潮16 分钟前
JS中对象是怎么运算的呢
前端·javascript·面试
云上小朱16 分钟前
软件部署-在k8s部署Hadoop集群
后端
悠哉摸鱼大王17 分钟前
NV12 转 RGB 完整指南
前端·javascript
一壶纱18 分钟前
UniApp + Pinia 数据持久化
前端·数据库·uni-app
双向3320 分钟前
【RAG+LLM实战指南】如何用检索增强生成破解AI幻觉难题?
前端
镜花水月linyi20 分钟前
Cookie、Session、JWT 的区别?
后端·面试
海云前端120 分钟前
前端人必懂的浏览器指纹:不止是技术,更是求职加分项
前端
青莲84321 分钟前
Java内存模型(JMM)与JVM内存区域完整详解
android·前端·面试
用户03048059126324 分钟前
Spring Boot 配置文件加载大揭秘:优先级覆盖与互补合并机制详解
java·后端