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

相关推荐
踏浪无痕5 小时前
自定义 ClassLoader 动态加载:不重启就能加载新代码?
后端·面试·架构
lomocode5 小时前
改一个需求动 23 处代码?你可能踩进了这个坑
后端·设计模式
踏浪无痕5 小时前
别重蹈我们的覆辙:脚本引擎选错的两年代价
后端·面试·架构
Airene5 小时前
Vite 8 发布 beta 版本了,升级体验一下 Rolldown
前端·vite
何中应5 小时前
【面试题-4】JVM
java·jvm·后端·面试题
Oneslide5 小时前
如何在Kubernetes搭建RabbitMQ集群 部署篇
后端
冰暮流星5 小时前
css3如何引入外部字体
前端·css·css3
VX:Fegn08955 小时前
计算机毕业设计|基于springboot + vue非遗传承文化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
ByteCraze5 小时前
从零到一:构建一个实时语音翻译应用(Vue3 + Web Speech API)
前端·开源·github