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最灵活。

相关推荐
小满zs9 小时前
Go语言第三章(五谷轮回)
后端·go
qq_5896660511 小时前
TypeScript 完整入门教程
前端·javascript·typescript
霸道流氓气质13 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
用户0595401744614 小时前
LLM对话记忆测试踩坑实录:手工回归30分钟,自动化后2分钟发现3个隐藏Bug
前端·css
Ashley的成长之路16 小时前
前端性能优化实战手册·第2篇:资源加载策略全解
前端·性能优化·资源加载·http/3·性能优化实战·资源加载优化
浅水壁虎16 小时前
vue基础(第二章 )
前端·javascript·vue.js
界面开发小八哥16 小时前
界面控件DevExtreme v26.1新版亮点——支持Angular 22
前端·javascript·angular.js·devexpress·ui开发·devextreme
Getflare16 小时前
前端 + UI 设计 + AI:这不是三个工种,是一个新三角能力模型(附自检清单)
前端·人工智能·ui
oil欧哟16 小时前
我做了一个 Vibe Coding 术语学习站:VibeHub
前端·ai·agent·独立开发·vibe coding