There are two ways to add a background in Bootstrap: using built-in CSS classes, such as .bg- (monochrome background) and .bg-gradient- (gradient background); using custom CSS rules, such as setting background-color and background-image properties.
How to add background in Bootstrap
Bootstrap is a popular front-end framework that provides an easy way to add a background to an HTML element. There are two main ways to do this:
Method 1: Using CSS Classes
Bootstrap provides a series of built-in CSS classes that add different Type of background:
.bg-*
: Adds a solid color background to the element, where *
can be any color, for example bg- primary
, bg-warning
, etc. .bg-gradient-*
: Add a gradient background to the element, where *
can be primary
, secondary
, success
, etc. .bg-image
: Add an image background to the element. The URL or path to the image needs to be specified. How to use:
<code class="html"><div class="bg-primary">This is a blue background.</div> <div class="bg-gradient-primary">This is a blue gradient background.</div> <div class="bg-image" style="background-image: url('image.png');">This is a background image.</div></code>
Method 2: Use custom CSS
If you need more granular Control, you can also use custom CSS rules to add backgrounds to elements:
<code class="css">body { background-color: #ccc; } .my-background { background-image: url('image.png'); background-repeat: no-repeat; background-position: center center; background-size: cover; }</code>
Usage:
<code class="html"><body class="my-background"></body></code>
Please note that custom CSS rules have higher priority than Bootstrap's built-in CSS classes.
The above is the detailed content of How to add background to bootstrap. For more information, please follow other related articles on the PHP Chinese website!