Skip to content

Commit 8297b3e

Browse files
Bootstrap customization guide
1 parent 003803e commit 8297b3e

File tree

6 files changed

+11939
-51
lines changed

6 files changed

+11939
-51
lines changed

docs/customization.qmd

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,100 @@ To generate the HTML pages to be returned from our GET routes, we use Jinja2 tem
117117

118118
With Jinja2, we can use the `{% block %}` tag to define content blocks, and the `{% extends %}` tag to extend a base template. We can also use the `{% include %}` tag to include a component in a parent template. See the [Jinja2 documentation on template inheritance](https://jinja.palletsprojects.com/en/stable/templates/#template-inheritance) for more details.
119119

120-
### Bootstrap Sass Files
121-
Install Node.js on your local machine if it is not there already.
122-
Install sass, gulp, and gulp-sass on your local machine:
120+
### Custom theming with Bootstrap Sass
121+
122+
[Install Node.js](https://nodejs.org/en/download/) on your local machine if it is not there already.
123+
124+
Install `bootstrap`, `sass`, `gulp`, and `gulp-sass` in your project:
125+
126+
```bash
127+
npm install --save-dev bootstrap sass gulp gulp-cli gulp-sass
128+
```
129+
130+
This will create a `node_modules` folder, a `package-lock.json` file, and a `package.json` file in the root directory of the project.
131+
132+
Create an `scss` folder and a basic `scss/styles.scss` file:
133+
123134
```bash
124-
npm install sass gulp gulp-sass
135+
mkdir scss
136+
touch scss/styles.scss
137+
```
138+
139+
Your custom styles will go in `scss/styles.scss`, along with `@import` statements to include the Bootstrap components you want. For example, the default CSS for the template was compiled from the following configuration, which imports all of Bootstrap and overrides the `$theme-colors` and `$font-family-base` variables:
140+
141+
```scss
142+
// styles.scss
143+
144+
// Include any default variable overrides here (functions won't be available)
145+
146+
// State colors
147+
$primary: #7464a1;
148+
$secondary: #64a19d;
149+
$success: #67c29c;
150+
$info: #1cabc4;
151+
$warning: #e4c662;
152+
$danger: #a16468;
153+
$light: #f8f9fa;
154+
$dark: #343a40;
155+
156+
// Bootstrap color map
157+
$theme-colors: (
158+
"primary": $primary,
159+
"secondary": $secondary,
160+
"success": $success,
161+
"info": $info,
162+
"warning": $warning,
163+
"danger": $danger,
164+
"light": $light,
165+
"dark": $dark
166+
);
167+
168+
$font-family-base: (
169+
"Nunito",
170+
-apple-system,
171+
BlinkMacSystemFont,
172+
"Segoe UI",
173+
Roboto,
174+
"Helvetica Neue",
175+
Arial,
176+
sans-serif,
177+
"Apple Color Emoji",
178+
"Segoe UI Emoji",
179+
"Segoe UI Symbol",
180+
"Noto Color Emoji"
181+
);
182+
183+
// Include all of Bootstrap
184+
185+
@import "../node_modules/bootstrap/scss/bootstrap";
125186
```
126-
Restart VS Code and press Run Task from the Terminal menu on the gulpfile.js. Choose gulp:default and select your option for output. This automates Sass compilation.
127187

128-
There is already a default styles.scss file in the `scss` folder. Here are customization instructions for Sass files that can be included in `sass`:
129-
[Boostrap Customization with Sass](https://getbootstrap.com/docs/5.3/customize/sass/)
188+
The most common use case for `styles.scss` is to define a custom color scheme and fonts, but it's also possible to other visual details such as border radius and box shadow depth. See the [Bootstrap Sass customization instructions](https://getbootstrap.com/docs/5.3/customize/sass/) and the many free templates available at [Start Bootstrap](https://startbootstrap.com) for examples.
130189

131-
There are a lot of free templates available online, such as on [Start Bootstrap]|[https://startbootstrap.com]
190+
To compile the Sass files, we use `gulp`. In the project root directory, create a `gulpfile.js` file with the following content:
191+
192+
```javascript
193+
const gulp = require('gulp');
194+
const sass = require('gulp-sass')(require('sass'));
195+
196+
// Define a task to compile Sass
197+
gulp.task('sass', function() {
198+
return gulp.src('scss/**/*.scss') // Source folder containing Sass files
199+
.pipe(sass().on('error', sass.logError))
200+
.pipe(gulp.dest('static/css')); // Destination folder for compiled CSS
201+
});
202+
203+
// Define a default task
204+
gulp.task('default', gulp.series('sass'));
205+
```
206+
207+
To compile the Sass file to `static/css`, run this command:
208+
209+
```bash
210+
npx gulp
211+
```
212+
213+
Note that this will overwrite the existing `static/css/styles.css` file, so if you want to define any custom CSS styles, you should do so in either the `scss/styles.scss` file or in `static/css/extras.css`.
132214

133215
#### Context variables
134216

@@ -150,6 +232,7 @@ In this example, the `welcome.html` template will receive two pieces of context:
150232
While this template includes comprehensive server-side validation through Pydantic models and custom validators, it's important to note that server-side validation should be treated as a fallback security measure. If users ever see the `validation_error.html` template, it indicates that our client-side validation has failed to catch invalid input before it reaches the server.
151233

152234
Best practices dictate implementing thorough client-side validation via JavaScript and/or HTML `input` element `pattern` attributes to:
235+
153236
- Provide immediate feedback to users
154237
- Reduce server load
155238
- Improve user experience by avoiding round-trips to the server

gulpfile.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

scss/styles.scss

Lines changed: 0 additions & 17 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)