{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ui.add_style`\n", "\n", "## 向页面添加样式定义\n", "\n", "此函数可用于将 CSS、SCSS 或 SASS 样式定义添加到 HTML 页面的头部。\n", "\n", "- `content`:样式内容(字符串或文件路径)\n", "- `indented`:内容是否缩进(SASS)或不缩进(SCSS/CSS)(默认:`False`)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.add_style('''\n", " .red {\n", " color: red;\n", " }\n", "''')\n", "ui.label('This is red with CSS.').classes('red')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用 SCSS 定义样式\n", "\n", "您也可以使用 SCSS 来定义样式。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "ui.add_style('''\n", " .green {\n", " background-color: lightgreen;\n", " .blue {\n", " color: blue;\n", " }\n", " }\n", "''')\n", "with ui.element().classes('green'):\n", " ui.label('This is blue on green with SCSS.').classes('blue')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用 SASS\n", "\n", "您也可以通过将 `indented` 参数设置为 `True` 来使用缩进的 SASS 语法。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "ui.add_style('''\n", " .yellow\n", " background-color: yellow\n", " .purple\n", " color: purple\n", "''', indented=True)\n", "with ui.element().classes('yellow'):\n", " ui.label('This is purple on yellow with SASS.').classes('purple')\n", "\n", "# ui.run()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }