Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

css - How to change or override base font size in a Bootstrap 4 template?

I've created a Java/Spring Boot webapp using Thymeleaf HTML page templates and Bootstrap 4. I based the page templates on a free Bootstrap 4 site template named 'Simple Sidebar': https://startbootstrap.com/template/simple-sidebar. This is working well for me so far.

I'd like to modify (or override) the CSS provided in the 'Simple Sidebar' Bootstrap template such that the default text font size is smaller across the whole template. The application I'm building is for internal use at a company, and will be viewed only on PC browsers. I need it to fit a lot of information on the screen at one time, so the font size is key.

QUESTIONS:

  1. Is this kind of change possible in a Bootstrap 4 template?
  2. If so, do I need to modify the CSS files that came with the template, or is there a way to override their CSS classes with a custom CSS file that I add to my project? (I'd prefer not to modify the CSS files that came with the Bootstrap template if I can avoid it)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a helpful tutorial here:
https://bootstrapcreative.com/can-adjust-text-size-bootstrap-responsive-design

They recommended one approach where font-size be overridden based on the category of the window/screen size where the page is being displayed. I tried this approach in my own application and it gave me exactly the kind of flexibility that I wanted.

As a test, I used the following ridiculously large values for some commonly used HTML element types. The text displayed VERY large inside those elements, as expected.

/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) {
  a {font-size:3.5rem;} /*1rem = 16px*/
  td {font-size:3.5rem;} /*1rem = 16px*/
  p {font-size:3.5rem;} /*1rem = 16px*/
  h1 {font-size:3.5rem;} /*1rem = 16px*/
}

/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {
  a {font-size:5rem;} /*1rem = 16px*/
  td {font-size:5rem;} /*1rem = 16px*/
  p {font-size:5rem;} /*1rem = 16px*/
  h1 {font-size:5rem;} /*1rem = 16px*/
}

It also works the other way with very smaller values like this:

/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) {
  a {font-size:0.5rem;} /*1rem = 16px*/
  td {font-size:0.5rem;} /*1rem = 16px*/
  p {font-size:0.5rem;} /*1rem = 16px*/
  h1 {font-size:0.5rem;} /*1rem = 16px*/
}

/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {
  a {font-size:0.75rem;} /*1rem = 16px*/
  td {font-size:0.75rem;} /*1rem = 16px*/
  p {font-size:0.75rem;} /*1rem = 16px*/
  h1 {font-size:0.75rem;} /*1rem = 16px*/
}

I love how the font size will automatically adjust itself on my PC if I shrink the browser window down. Of course, it only works properly if one provides sane values for the various window sizes.

I was able to get this working without modifying any of the files that were included in the Bootstrap template bundle. I put the sample CSS above into a file named my-custom-styles.css, and I put that file into the same directory as the simple-sidebar.css file that comes with the Bootstrap template.

Finally, my HTML template needs this inside the <head> block:

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet"
        th:href="@{/vendor/bootstrap/css/bootstrap.min.css}"
        href="../../static/vendor/bootstrap/css/bootstrap.min.css" />
  <!-- Custom styles for this Boostrap template -->
  <link rel="stylesheet"
        th:href="@{/css/simple-sidebar.css}"
        href="../../static/css/simple-sidebar.css" />
  <!-- Custom styles for my application that override the Bootstrap template -->
  <link rel="stylesheet"
        th:href="@{/css/my-custom-styles.css}"
        href="../static/css/my-custom-styles.css" />

NOTE: The th:href attributes are used by the Thymeleaf template library and are not included in the final HTML that is sent to the client browser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...