Missing visible-** and hidden-** in Bootstrap v4

后端 未结 10 2228
小蘑菇
小蘑菇 2020-11-22 06:39

In Bootstrap v3 I often use the hidden-** classes combined with clearfix to control multi column layouts at different screen widths. For example,

I could combine mu

相关标签:
10条回答
  • 2020-11-22 07:31

    Bootstrap 4 to hide whole content use this class '.d-none' it will be hide everything regardless of breakpoints same like previous bootstrap version class '.hidden'

    0 讨论(0)
  • 2020-11-22 07:33

    Unfortunately all classes hidden-*-up and hidden-*-down were removed from Bootstrap (as of Bootstrap Version 4 Beta, in Version 4 Alpha and Version 3 these classes still existed).

    Instead, new classes d-* should be used, as mentioned here: https://getbootstrap.com/docs/4.0/migration/#utilities

    I found out that the new approach is less useful under some circumstances. The old approach was to HIDE elements while the new approach is to SHOW elements. Showing elements is not that easy with CSS since you need to know if the element is displayed as block, inline, inline-block, table etc.

    You might want to restore the former "hidden-*" styles known from Bootstrap 3 with this CSS:

    /*\
     * Restore Bootstrap 3 "hidden" utility classes.
    \*/
    
    /* Breakpoint XS */
    @media (max-width: 575px)
    {
        .hidden-xs-down, .hidden-sm-down, .hidden-md-down, .hidden-lg-down, .hidden-xl-down, 
        .hidden-xs-up, 
        .hidden-unless-sm, .hidden-unless-md, .hidden-unless-lg, .hidden-unless-xl
        {
            display: none !important;
        }
    
    }
    
    /* Breakpoint SM */
    @media (min-width: 576px) and (max-width: 767px)
    {
        .hidden-sm-down, .hidden-md-down, .hidden-lg-down, .hidden-xl-down, 
        .hidden-xs-up, .hidden-sm-up, 
        .hidden-unless-xs, .hidden-unless-md, .hidden-unless-lg, .hidden-unless-xl
        {
            display: none !important;
        } 
    }
    
    /* Breakpoint MD */
    @media (min-width: 768px) and (max-width: 991px)
    {
        .hidden-md-down, .hidden-lg-down, .hidden-xl-down, 
        .hidden-xs-up, .hidden-sm-up, .hidden-md-up, 
        .hidden-unless-xs, .hidden-unless-sm, .hidden-unless-lg, .hidden-unless-xl
        {
            display: none !important;
        } 
    }
    
    /* Breakpoint LG */
    @media (min-width: 992px) and (max-width: 1199px)
    {
        .hidden-lg-down, .hidden-xl-down, 
        .hidden-xs-up, .hidden-sm-up, .hidden-md-up, .hidden-lg-up, 
        .hidden-unless-xs, .hidden-unless-sm, .hidden-unless-md, .hidden-unless-xl
        {
            display: none !important;
        } 
    }
    
    /* Breakpoint XL */
    @media (min-width: 1200px)
    {
        .hidden-xl-down, 
        .hidden-xs-up, .hidden-sm-up, .hidden-md-up, .hidden-lg-up, .hidden-xl-up, 
        .hidden-unless-xs, .hidden-unless-sm, .hidden-unless-md, .hidden-unless-lg
        {
            display: none !important;
        } 
    }
    

    The classes hidden-unless-* were not included in Bootstrap 3, but they are useful as well and should be self-explanatory.

    0 讨论(0)
  • 2020-11-22 07:37

    I do no expect that multiple div's is a good solution.

    I also think you can replace .visible-sm-block with .hidden-xs-down and .hidden-md-up (or .hidden-sm-down and .hidden-lg-up to act on the same media queries).

    hidden-sm-up compiles into:

    .visible-sm-block {
      display: none !important;
    }
    @media (min-width: 768px) and (max-width: 991px) {
      .visible-sm-block {
        display: block !important;
      }
    }
    

    .hidden-sm-down and .hidden-lg-up compiles into:

    @media (max-width: 768px) {
      .hidden-xs-down {
        display: none !important;
      }
    }
    @media (min-width: 991px) {
      .hidden-lg-up {
        display: none !important;
      }
    }
    

    Both situation should act the same.

    You situation become different when you try to replace .visible-sm-block and .visible-lg-block. Bootstrap v4 docs tell you:

    These classes don’t attempt to accommodate less common cases where an element’s visibility can’t be expressed as a single contiguous range of viewport breakpoint sizes; you will instead need to use custom CSS in such cases.

    .visible-sm-and-lg {
      display: none !important;
    }
    @media (min-width: 768px) and (max-width: 991px) {
      .visible-sm-and-lg {
        display: block !important;
      }
    }
    @media (min-width: 1200px) {
      .visible-sm-and-lg {
        display: block !important;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 07:43

    For bootstrap 4, here's a matrix image explaining the classes used to show / hide elements depends on the screen size:

    Source : Meduim : Bootstrap 4 Hidden & Visible

    0 讨论(0)
提交回复
热议问题