How can I change the Bootstrap default font family using font from Google?
Solution 1:
First of all, you can't import fonts to CSS that way.
You can add this code in HTML head:
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'>
or to import it in CSS file like this:
@import url("http://fonts.googleapis.com/css?family=Oswald:400,300,700");
Then, in your css, you can edit the body's font-family:
body {
font-family: 'Oswald', sans-serif !important;
}
Solution 2:
If you use Sass, there are Bootstrap variables are defined with !default
, among which you'll find font families. You can just set the variables in your own .scss
file before including the Bootstrap Sass file and !default
will not overwrite yours. Here's a good explanation of how !default
works: https://thoughtbot.com/blog/sass-default.
Here's an untested example using Bootstrap 4, npm, Gulp, gulp-sass and gulp-cssmin to give you an idea how you could hook this up together.
package.json
{
"devDependencies": {
"bootstrap": "4.0.0-alpha.6",
"gulp": "3.9.1",
"gulp-sass": "3.1.0",
"gulp-cssmin": "0.2.0"
}
}
mysite.scss
@import "./myvariables";
// Bootstrap
@import "bootstrap/scss/variables";
// ... need to include other bootstrap files here. Check node_modules\bootstrap\scss\bootstrap.scss for a list
_myvariables.scss
// For a list of Bootstrap variables you can override, look at node_modules\bootstrap\scss\_variables.scss
// These are the defaults, but you can override any values
$font-family-sans-serif: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !default;
$font-family-serif: Georgia, "Times New Roman", Times, serif !default;
$font-family-monospace: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !default;
$font-family-base: $font-family-sans-serif !default;
gulpfile.js
var gulp = require("gulp"),
sass = require("gulp-sass"),
cssmin = require("gulp-cssmin");
gulp.task("transpile:sass", function() {
return gulp.src("./mysite.scss")
.pipe(sass({ includePaths: "./node_modules" }).on("error", sass.logError))
.pipe(cssmin())
.pipe(gulp.dest("./css/"));
});
index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="mysite.css" />
</head>
<body>
...
</body>
</html>
Solution 3:
I think the best and cleanest way would be to get a custom download of bootstrap.
http://getbootstrap.com/customize/
You can then change the font-defaults in the Typography (in that link). This then gives you a .Less file that you can make further changes to defaults with later.
Solution 4:
If you have a custom.css
file, in there, just do something like:
font-family: "Oswald", Helvetica, Arial, sans-serif!important;