Customizing Bootstrap CSS template

前端 未结 9 1305
面向向阳花
面向向阳花 2020-11-22 09:03

I am just getting started with Bootstrap from Twitter and am wondering what the ‘best practices’ is for customization. I want to develop a system that will take advantage of

9条回答
  •  时光取名叫无心
    2020-11-22 09:50

    Update 2019 - Bootstrap 4

    I'm revisiting this Bootstrap customization question for 4.x, which now utilizes SASS instead of LESS. In general, there are 2 ways to customize Bootstrap...

    1. Simple CSS Overrides

    One way to customize is simply using CSS to override Bootstrap CSS. For maintainability, CSS customizations are put in a separate custom.css file, so that the bootstrap.css remains unmodified. The reference to the custom.css follows after the bootstrap.css for the overrides to work...

    
    
    

    Just add whatever changes are needed in the custom CSS. For example...

        /* remove rounding from cards, buttons and inputs */
        .card, .btn, .form-control {
            border-radius: 0;
        }
    

    Before (bootstrap.css)

    After (with custom.css)

    When making customizations, you should understand CSS Specificity. Overrides in the custom.css need to use selectors that are the same specificity as (or more specific) the bootstrap.css.

    Note there is no need to use !important in the custom CSS, unless you're overriding one of the Bootstrap Utility classes. CSS specificity always works for one CSS class to override another.


    2. Customize using SASS

    If you're familiar with SASS (and you should be to use this method), you can customize Bootstrap with your own custom.scss. There is a section in the Bootstrap docs that explains this, however the docs don't explain how to utilize existing variables in your custom.scss. For example, let's change the body background-color to #eeeeee, and change/override the blue primary contextual color to Bootstrap's $purple variable...

    /* custom.scss */    
    
    /* import the necessary Bootstrap files */
    @import "bootstrap/functions";
    @import "bootstrap/variables";
    
    /* -------begin customization-------- */   
    
    /* simply assign the value */ 
    $body-bg: #eeeeee;
    
    /* use a variable to override primary */
    $theme-colors: (
      primary: $purple
    );
    /* -------end customization-------- */  
    
    /* finally, import Bootstrap to set the changes! */
    @import "bootstrap";
    

    This also works to create new custom classes. For example, here I add purple to the theme colors which creates all the CSS for btn-purple, text-purple, bg-purple, alert-purple, etc...

    /* add a new purple custom color */
    $theme-colors: (
      purple: $purple
    );
    

    https://www.codeply.com/go/7XonykXFvP

    With SASS you must @import bootstrap after the customizations to make them work! Once the SASS is compiled to CSS (this must be done using a SASS compiler node-sass, gulp-sass, npm webpack, etc..), the resulting CSS is the customized Bootstrap. If you're not familiar with SASS, you can customize Bootstrap using a tool like this theme builder I created.

    Custom Bootstrap Demo (SASS)

    Note: Unlike 3.x, Bootstrap 4.x doesn't offer an official customizer tool. You can however, download the grid only CSS or use another 4.x custom build tool to re-build the Bootstrap 4 CSS as desired.


    Related:
    How to extend/modify (customize) Bootstrap 4 with SASS
    How to change the bootstrap primary color?
    How to create new set of color styles in Bootstrap 4 with sass
    How to Customize Bootstrap

提交回复
热议问题