Bootstrap 设置背景颜色的方法
1. 内联方式
使用 style 属性直接在元素中设置背景颜色:
<code class="html"><div style="background-color: #ffffff;">内容</div></code>
2. CSS 类
创建具有所需背景颜色的 CSS 类,然后应用到元素:
<code class="css">.my-background {
background-color: #ffffff;
}</code><code class="html"><div class="my-background">内容</div></code>
3. Bootstrap 实用程序类
Bootstrap 提供了一系列实用程序类来设置背景颜色,每个类对应一种颜色:
.bg-primary.bg-secondary.bg-success.bg-danger.bg-warning.bg-info.bg-light.bg-dark<code class="html"><div class="bg-primary">内容</div></code>4. 自定义 CSS 变量
使用 CSS 变量定义背景颜色,并通过
--访问变量:<code class="css">:root { --my-background-color: #ffffff; }</code> <code class="html"><div style="background-color: var(--my-background-color);">内容</div></code>5. 背景渐变
使用
background-image属性创建背景渐变:<code class="css">.my-gradient { background-image: linear-gradient(to right, #ffffff, #000000); }</code><code class="html"><div class="my-gradient">内容</div></code>6. 背景图片
使用
background-image属性设置背景图片:<code class="css">.my-image { background-image: url("image.jpg"); }</code><code class="html"><div class="my-image">内容</div></code>



