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 分钟前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
0思必得032 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice34 分钟前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶36035 分钟前
php怎么实现订单接口状态轮询(二)
前端·php·接口
Charlie_lll42 分钟前
力扣解题-移动零
后端·算法·leetcode
大橙子额1 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
打工的小王2 小时前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
爱喝白开水a3 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
董世昌413 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
80530单词突击赢3 小时前
JavaWeb进阶:SpringBoot核心与Bean管理
java·spring boot·后端