【问题标题】:Bootstrap 3 breakpoints and media queriesBootstrap 3 断点和媒体查询
【发布时间】:2013-11-04 18:09:05
【问题描述】:

Bootstrap 3 media queries documentation 上面写着:

我们在 Less 文件中使用以下媒体查询来在我们的网格系统中创建关键断点。

超小型设备(手机,小于 768 像素): 没有媒体查询,因为这是 Bootstrap 中的默认设置

小型设备(平板电脑,768 像素及以上): @media (min-width: @screen-sm-min) { ... }

中型设备(台式机,992 像素及以上): @media (min-width: @screen-md-min) { ... }

大型设备(大型桌面,1200 像素及以上): @media (min-width: @screen-lg-min) { ... }

我们是否也应该能够在媒体查询中使用@screen-sm@screen-md@screen-lg 名称?因为它对我不起作用。我必须使用像素测量值,例如@media (min-width: 768px) {...},然后它才能工作。我做错了吗?

另外,对超小型设备的 480 像素的引用是错字吗?不应该说最多 767 像素吗?从文档中删除

【问题讨论】:

  • 这里是 BS4 中使用的选择器。 BS4 中没有“最低”设置,因为“特小”是默认设置。 IE。您将首先对 XS 尺寸进行编码,然后再使用这些媒体覆盖。 @media(min-width:576px){} @media(min-width:768px){} @media(min-width:992px){} @media(min-width:1200px){}

标签: twitter-bootstrap twitter-bootstrap-3 media-queries breakpoints


【解决方案1】:

Bootstrap 4 媒体查询

// Extra small devices (portrait phones, less than 576px)
// No media query since this is the default in Bootstrap

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) { ... }

// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

Bootstrap 4 在 Sass 中提供源 CSS,您可以通过 Sass Mixins 将其包含在内:

@include media-breakpoint-up(xs) { ... }
@include media-breakpoint-up(sm) { ... }
@include media-breakpoint-up(md) { ... }
@include media-breakpoint-up(lg) { ... }
@include media-breakpoint-up(xl) { ... }

// Example usage:
@include media-breakpoint-up(sm) {
  .some-class {
    display: block;
  }
}

Bootstrap 3 媒体查询

/*==========  Mobile First Method  ==========*/

/* Custom, iPhone Retina */ 
@media only screen and (min-width : 320px) {

}

/* Extra Small Devices, Phones */ 
@media only screen and (min-width : 480px) {

}

/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {

}

/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {

}

/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {

}



/*==========  Non-Mobile First Method  ==========*/

/* Large Devices, Wide Screens */
@media only screen and (max-width : 1200px) {

}

/* Medium Devices, Desktops */
@media only screen and (max-width : 992px) {

}

/* Small Devices, Tablets */
@media only screen and (max-width : 768px) {

}

/* Extra Small Devices, Phones */ 
@media only screen and (max-width : 480px) {

}

/* Custom, iPhone Retina */ 
@media only screen and (max-width : 320px) {

}

Bootstrap 2.3.2 媒体查询

@media only screen and (max-width : 1200px) {

}

@media only screen and (max-width : 979px) {

}

@media only screen and (max-width : 767px) {

}

@media only screen and (max-width : 480px) {

}

@media only screen and (max-width : 320px) {

}

资源来自:https://scotch.io/quick-tips/default-sizes-for-twitter-bootstraps-media-queries

【讨论】:

  • @CyrilDuchon-Doris,问题是关于 Bootstrap 3...所以我不这么认为。
  • 答案获得了30分并提到了Bootstrap 2。即使不使用Bootstrap 3,很多人也会看一下。我总是非常感谢那些编辑自己答案的人- 日期信息,即使稍微超出初始范围。
  • 那么 479 以下是不是特别小?
  • Bootstrap v4 甚至还不稳定。你知道吗?答案可能需要多次更新才能达到稳定版本。
  • 我相信这里有一个像素的错误,实际上可能有影响。对于 1200px 和 320px 的屏幕,最大媒体查询和最小媒体查询都会生效。所有最大媒体查询应为负 1 像素(例如 1199 像素)。最小和最大 320 像素的媒体查询对我来说真的没有意义,因为 AFAIK 实际上屏幕从 320 像素开始。
【解决方案2】:

Bootstrap 不能很好地记录媒体查询。 @screen-sm@screen-md@screen-lg这些变量实际上指的是LESS变量而不是简单的CSS。

当您自定义 Bootstrap 时,您可以更改媒体查询断点,并且当它编译时,@screen-xx 变量会更改为您定义为 screen-xx 的任何像素宽度。这就是这样的框架可以被编码一次然后由最终用户定制以满足他们的需求的方式。

这里有一个类似的问题,可能会更清楚:Bootstrap 3.0 Media queries

在您的 CSS 中,您仍然必须使用传统的媒体查询来覆盖或添加 Bootstrap 正在执行的操作。

关于您的第二个问题,这不是错字。一旦屏幕低于 768 像素,框架就会变得完全流畅并在任何设备宽度下调整大小,从而无需断点。存在 480px 处的断点是因为移动优化的布局发生了特定的变化。

要查看实际效果,请访问他们网站 (http://getbootstrap.com/examples/navbar-fixed-top/) 上的此示例,然后调整窗口大小以查看 768 像素后它如何处理设计。

【讨论】:

  • 要查看实际效果,请访问他们网站上的此示例,然后调整窗口大小以查看 768 像素后它如何处理设计。 // 那有什么意义用480px做什么?与 500 像素相比,我认为 480 像素没有任何不同。
  • 就在 Bootstrap 3 的变量系统上自然扩展而言,这应该是公认的答案,因为它是一种非常有效的方法。
【解决方案3】:

此问题已在https://github.com/twbs/bootstrap/issues/10203 中讨论过 到目前为止,由于兼容性原因,还没有更改 Grid 的计划。

你可以从这个fork获取Bootstrap,分支hs:https://github.com/antespi/bootstrap/tree/hs

这个分支在 480px 处给你一个额外的断点,所以你必须:

  1. 移动优先设计(XS,小于 480 像素)
  2. 在您的 HTML 中添加 HS(水平小型设备)类:col-hs-*、visible-hs ......并为水平移动设备设计(HS,小于 768 像素)
  3. 平板设备设计(SM,小于 992 像素)
  4. 桌面设备设计(MD,小于 1200 像素)
  5. 为大型设备设计(LG,超过 1200 像素)

首先设计移动端是理解 Bootstrap 3 的关键。这是 BootStrap 2.x 的主要变化。作为规则模板,您可以遵循以下规则(在 LESS 中):

.template {
    /* rules for mobile vertical (< 480) */

    @media (min-width: @screen-hs-min) {
       /* rules for mobile horizontal (480 > 768)  */
    }
    @media (min-width: @screen-sm-min) {
       /* rules for tablet (768 > 992) */
    }
    @media (min-width: @screen-md-min) {
       /* rules for desktop (992 > 1200) */
    }
    @media (min-width: @screen-lg-min) {
       /* rules for large (> 1200) */
    }
}

【讨论】:

  • 对不起,我不明白这个分叉的附加价值。据我了解,您这样做@screen-hs-min:@screen-xs;。为什么不在这里直接使用@screen-xs
  • 只是为了更好地理解。该变量为模板提供了视觉一致性。 Bootstrap 3 是移动优先,因此媒体查询之外的所有规则都适用于移动设备大小。然后,如果您需要 HS 的额外规则,您将在 min-width: @screen-hs-min 中写下,如果您需要 SM 的 estra 规则,您将在 min-width: @screen-sm-min 中写下,依此类推。这个 fork 用于在 480px 处添加一个新的断点。然后移动尺寸低于 480 像素,并在 480 像素和 768 像素之间出现新尺寸(HS)
  • 请注意,模板有一点错别字。 screen-hs-min 应该是 screen-xs-min
  • @eflat 这不是拼写错误,screen-hs-minscreen-xs-minscreen-sm-min 之间的新规则
【解决方案4】:

我知道这有点老了,但我想我会做出贡献。 根据@Sophy 的回答,这就是我添加 .xxs 断点的方法。 我没有处理 visible-inline、table.visible 等类。

/*==========  Mobile First Method  ==========*/

  .col-xxs-12, .col-xxs-11, .col-xxs-10, .col-xxs-9, .col-xxs-8, .col-xxs-7, .col-xxs-6, .col-xxs-5, .col-xxs-4, .col-xxs-3, .col-xxs-2, .col-xxs-1 {
    position: relative;
    min-height: 1px;
    padding-left: 15px;
    padding-right: 15px;
    float: left;
  }

.visible-xxs {
  display:none !important;
}

/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) and (max-width:479px) {

  .visible-xxs {
    display: block !important;
  }
  .visible-xs {
    display:none !important;
  }

  .hidden-xs {
    display:block !important;
  }

  .hidden-xxs {
    display:none !important;
  }

  .col-xxs-12 {
    width: 100%;
  }
  .col-xxs-11 {
    width: 91.66666667%;
  }
  .col-xxs-10 {
    width: 83.33333333%;
  }
  .col-xxs-9 {
    width: 75%;
  }
  .col-xxs-8 {
    width: 66.66666667%;
  }
  .col-xxs-7 {
    width: 58.33333333%;
  }
  .col-xxs-6 {
    width: 50%;
  }
  .col-xxs-5 {
    width: 41.66666667%;
  }
  .col-xxs-4 {
    width: 33.33333333%;
  }
  .col-xxs-3 {
    width: 25%;
  }
  .col-xxs-2 {
    width: 16.66666667%;
  }
  .col-xxs-1 {
    width: 8.33333333%;
  }
  .col-xxs-pull-12 {
    right: 100%;
  }
  .col-xxs-pull-11 {
    right: 91.66666667%;
  }
  .col-xxs-pull-10 {
    right: 83.33333333%;
  }
  .col-xxs-pull-9 {
    right: 75%;
  }
  .col-xxs-pull-8 {
    right: 66.66666667%;
  }
  .col-xxs-pull-7 {
    right: 58.33333333%;
  }
  .col-xxs-pull-6 {
    right: 50%;
  }
  .col-xxs-pull-5 {
    right: 41.66666667%;
  }
  .col-xxs-pull-4 {
    right: 33.33333333%;
  }
  .col-xxs-pull-3 {
    right: 25%;
  }
  .col-xxs-pull-2 {
    right: 16.66666667%;
  }
  .col-xxs-pull-1 {
    right: 8.33333333%;
  }
  .col-xxs-pull-0 {
    right: auto;
  }
  .col-xxs-push-12 {
    left: 100%;
  }
  .col-xxs-push-11 {
    left: 91.66666667%;
  }
  .col-xxs-push-10 {
    left: 83.33333333%;
  }
  .col-xxs-push-9 {
    left: 75%;
  }
  .col-xxs-push-8 {
    left: 66.66666667%;
  }
  .col-xxs-push-7 {
    left: 58.33333333%;
  }
  .col-xxs-push-6 {
    left: 50%;
  }
  .col-xxs-push-5 {
    left: 41.66666667%;
  }
  .col-xxs-push-4 {
    left: 33.33333333%;
  }
  .col-xxs-push-3 {
    left: 25%;
  }
  .col-xxs-push-2 {
    left: 16.66666667%;
  }
  .col-xxs-push-1 {
    left: 8.33333333%;
  }
  .col-xxs-push-0 {
    left: auto;
  }
  .col-xxs-offset-12 {
    margin-left: 100%;
  }
  .col-xxs-offset-11 {
    margin-left: 91.66666667%;
  }
  .col-xxs-offset-10 {
    margin-left: 83.33333333%;
  }
  .col-xxs-offset-9 {
    margin-left: 75%;
  }
  .col-xxs-offset-8 {
    margin-left: 66.66666667%;
  }
  .col-xxs-offset-7 {
    margin-left: 58.33333333%;
  }
  .col-xxs-offset-6 {
    margin-left: 50%;
  }
  .col-xxs-offset-5 {
    margin-left: 41.66666667%;
  }
  .col-xxs-offset-4 {
    margin-left: 33.33333333%;
  }
  .col-xxs-offset-3 {
    margin-left: 25%;
  }
  .col-xxs-offset-2 {
    margin-left: 16.66666667%;
  }
  .col-xxs-offset-1 {
    margin-left: 8.33333333%;
  }
  .col-xxs-offset-0 {
    margin-left: 0%;
  }

}

/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {

  .visible-xs {
    display:block !important;
  }

}

/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {

  .visible-xs {
    display:none !important;
  }

}

/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {

}

/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {

}

【讨论】:

  • 正是我需要的谢谢!所以我不需要再做一遍了:)
  • 也不要忘记类 .visible-xs-inline, .visible-xs-inline-block 任何时候覆盖 .visible-xs
【解决方案5】:

已删除对 480px 的引用。相反,它说:

/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

Bootstrap 3 中没有低于 768px 的断点。

如果您想使用 @screen-sm-min 和其他 mixin,那么您需要使用 LESS 进行编译,请参阅 http://getbootstrap.com/css/#grid-less

这里有一个关于如何使用 Bootstrap 3 和 LESS 的教程:http://www.helloerik.com/bootstrap-3-less-workflow-tutorial

【讨论】:

    【解决方案6】:

    如果您使用 http://lesscss.org/ 构建 CSS,您应该能够使用这些断点。

    /* Extra small devices (phones, less than 768px) */
    /* No media query since this is the default in Bootstrap */
    
    /* Small devices (tablets, 768px and up) */
    @media (min-width: @screen-sm-min) {  }
    
    /* Medium devices (desktops, 992px and up) */
    @media (min-width: @screen-md-min) {  }
    
    /* Large devices (large desktops, 1200px and up) */
    @media (min-width: @screen-lg-min) {  }
    

    来自https://getbootstrap.com/docs/3.4/css/#grid-media-queries

    事实上,@screen-sm-min 是一个变量,当使用 Less 构建时,它会被 _variable 中指定的值替换。如果不使用 Less,可以将这个变量替换为值:

    /* Extra small devices (phones, less than 768px) */
    /* No media query since this is the default in Bootstrap */
    
    /* Small devices (tablets, 768px and up) */
    @media (min-width: 768px) {  }
    
    /* Medium devices (desktops, 992px and up) */
    @media (min-width: 992px) {  }
    
    /* Large devices (large desktops, 1200px and up) */
    @media (min-width: 1200px) {  }
    

    Bootstrap 3 正式支持 Sass:https://github.com/twbs/bootstrap-sass。如果你使用的是 Sass(你应该),语法会有点不同。

    /* Extra small devices (phones, less than 768px) */
    /* No media query since this is the default in Bootstrap */
    
    /* Small devices (tablets, 768px and up) */
    @media (min-width: $screen-sm-min) { }
    
    /* Medium devices (desktops, 992px and up) */
    @media (min-width: $screen-md-min) {  }
    
    /* Large devices (large desktops, 1200px and up) */
    @media (min-width: $screen-lg-min) {  }
    

    【讨论】:

      【解决方案7】:

      这是 Bootstrap 4 中使用的选择器。BS4 中没有“最低”设置,因为“特小”是默认设置。 IE。您将首先对 XS 尺寸进行编码,然后再进行这些媒体覆盖。

      @media(min-width:576px){}
      @media(min-width:768px){}
      @media(min-width:992px){}
      @media(min-width:1200px){}
      

      【讨论】:

        【解决方案8】:

        对于 bootstrap 3,我的导航栏组件中有以下代码

        /**
         * Navbar styling.
         */
        @mobile:          ~"screen and (max-width: @{screen-xs-max})";
        @tablet:          ~"screen and (min-width: @{screen-sm-min})";
        @normal:          ~"screen and (min-width: @{screen-md-min})";
        @wide:            ~"screen and (min-width: @{screen-lg-min})";
        @grid-breakpoint: ~"screen and (min-width: @{grid-float-breakpoint})";
        

        然后你可以使用类似的东西

        @media wide { selector: style }
        

        这将使用您将变量设置为的任何值。

        转义允许您使用任意字符串作为属性或变量值。 ~"anything" 或 ~'anything' 中的任何内容都按原样使用,除了插值外没有任何变化。

        http://lesscss.org

        【讨论】:

          【解决方案9】:

          为了在依赖旧 Bootstrap 版本的项目中并行使用 Bootstrap 4 的 mixin,您可以尝试使用 alias via npm 安装两者:

          npm i bootstrap-3@npm:bootstrap-sass@\^3 bootstrap-4@npm:bootstrap@\^4 bootstrap-5@npm:bootstrap@\^5 bootstrap
          

          注意:此命令安装版本 3、4、5 以及最新版本。如果只需要 3 和 4,请进行调整!由于空间的原因,我通过符号链接使用 node_modules,我需要跨项目的所有这些:-)

          然后得到你需要的部分:

          // Bootstrap 3
          @import '../../../node_modules/bootstrap-3/assets/stylesheets/_bootstrap.scss';
          
          // Extend: Bootstrap 4 Media Queries
          @import "../../../node_modules/bootstrap-4/scss/_functions.scss";
          @import "../../../node_modules/bootstrap-4/scss/_variables.scss";
          @import '../../../node_modules/bootstrap-4/scss/mixins/_breakpoints.scss';
          

          当然要调整路径!

          如果有任何效果,您现在可以使用:

          @include media-breakpoint-up(sm) {}
          

          在引导程序 3 中。

          否则将 mixins 替换为 search / 替换为 px。

          【讨论】:

            【解决方案10】:

            附有示例的 Bootstrap 4 媒体查询

            @media (min-width: 576px) { 
            .responsive{
            color:red
            }
            }
            
            // Medium devices (tablets, 768px and up)
            @media (min-width: 768px) {
            .responsive{
            color:orange
            }
            }
            
            // Large devices (desktops, 992px and up)
            @media (min-width: 992px) { 
            .responsive{
            color:blue
            }
            }
            
            // Extra large devices (large desktops, 1200px and up)
            @media (min-width: 1200px) { 
            .responsive{
            color:green
            }
            }
            <h2>Bootstrao 4 </h2>
            <div class="responsive"> see colour herer</div>

            【讨论】:

              【解决方案11】:
              @media screen and (max-width: 767px) {
              
              }
              
              @media screen and (min-width: 768px) and (max-width: 991px){
              
              
              }
              
              @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape){
              
              
              }
              
              @media screen and (min-width: 992px) {
              
              
              
              }
              

              【讨论】:

                猜你喜欢
                • 2015-09-22
                • 2014-07-22
                • 2016-09-26
                • 2014-05-09
                • 2015-01-15
                • 2014-10-03
                • 1970-01-01
                • 2019-04-21
                • 1970-01-01
                相关资源
                最近更新 更多