Twitter Bootstrap 3:如何使用媒体查询?

Rui*_*Rui 366 css3 media-queries twitter-bootstrap

我正在使用Bootstrap 3来构建响应式布局,我想根据屏幕大小调整一些字体大小.如何使用媒体查询来制作这种逻辑?

Wil*_*ken 643

Bootstrap 3

以下是BS3中使用的选择器,如果您想保持一致:

@media(max-width:767px){}
@media(min-width:768px){}
@media(min-width:992px){}
@media(min-width:1200px){}
Run Code Online (Sandbox Code Playgroud)

注意:仅供参考,这可能对调试很有用:

<span class="visible-xs">SIZE XS</span>
<span class="visible-sm">SIZE SM</span>
<span class="visible-md">SIZE MD</span>
<span class="visible-lg">SIZE LG</span>
Run Code Online (Sandbox Code Playgroud)

Bootstrap 4

以下是BS4中使用的选择器.BS4中没有"最低"设置,因为"超小"是默认设置.即你首先编码XS大小,然后将这些媒体覆盖.

@media(min-width:576px){}
@media(min-width:768px){}
@media(min-width:992px){}
@media(min-width:1200px){}
Run Code Online (Sandbox Code Playgroud)

更新2019-02-11:从版本3.4.0开始,BS3信息仍然准确,新网格更新BS4,从4.3.0开始准确.

  • 仅供参考,这可能对调试很有用:`<span class ="visible-xs"> SIZE XS </ span> <span class ="visible-sm"> SIZE SM </ span> <span class ="visible- md"> SIZE MD </ span> <span class ="visible-lg"> SIZE LG </ span>` (115认同)
  • +1通过_increasing_屏幕大小排序查询,与BS3的移动优先方法保持一致. (14认同)
  • 480px(@ screen-xs)怎么样?那后来出现了吗?从[这里](http://getbootstrap.com/customize/#media-queries-breakpoints)得到它. (5认同)

Chr*_*wer 251

基于bisio的答案和Bootstrap 3代码,我能够为任何想要将完整的媒体查询集复制并粘贴到其样式表中的人提出更准确的答案:

/* Large desktops and laptops */
@media (min-width: 1200px) {

}

/* Landscape tablets and medium desktops */
@media (min-width: 992px) and (max-width: 1199px) {

}

/* Portrait tablets and small desktops */
@media (min-width: 768px) and (max-width: 991px) {

}

/* Landscape phones and portrait tablets */
@media (max-width: 767px) {

}

/* Portrait phones and smaller */
@media (max-width: 480px) {

}
Run Code Online (Sandbox Code Playgroud)


car*_*iam 134

如果您正在使用LESS或SCSS/SASS并且您正在使用LESS/SCSS版本的Bootstrap,那么您也可以使用变量,前提是您可以访问它们.@ full-decent的答案的简短翻译如下:

@media(max-width: @screen-xs-max){}
@media(min-width: @screen-sm-min){}  /* deprecated: @screen-tablet, or @screen-sm */
@media(min-width: @screen-md-min){}  /* deprecated: @screen-desktop, or @screen-md */
@media(min-width: @screen-lg-min){}  /* deprecated: @screen-lg-desktop, or @screen-lg */
Run Code Online (Sandbox Code Playgroud)

还有@screen-sm-max和的变量,分别@screen-md-max比通常使用的@screen-md-min和小1 和1个像素.@screen-lg-min@media(max-width)

编辑:如果您正在使用SCSS/SASS,变量以a $而不是a 开头@,所以它就是$screen-xs-max等等.

  • @JesseGoodfellow上面的例子使用了LESS语法; 如果您使用SASS/SCSS https://github.com/twbs/bootstrap-sass/blob/master/vendor/assets/stylesheets/bootstrap/_variables.scss,您需要`$ screen-xs-max`等等(如果你在本地使用LESS/SCSS但是导入了Bootstrap的CSS版本,你就完全没有了.) (7认同)
  • 我无法让它发挥作用.编译器抱怨我是否使用除硬编码数字之外的任何内容. (2认同)
  • `@ screen-tablet`,`@ screen-desktop`和`@ screen-lg-desktop`已被弃用.可能是时候更新你已经很棒的答案了.;-) (2认同)
  • 假设你建立引导程序; 这应该标记为答案 (2认同)

小智 44

这些是来自Bootstrap3的值:

/* Extra Small */
@media(max-width:767px){}

/* Small */
@media(min-width:768px) and (max-width:991px){}

/* Medium */
@media(min-width:992px) and (max-width:1199px){}

/* Large */
@media(min-width:1200px){}
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案.必须具备"和"条件 (4认同)

Jef*_*wrs 29

这是两个例子.

一旦视口变为700px宽或更小,使所有h1标签为20px.

@media screen and ( max-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}
Run Code Online (Sandbox Code Playgroud)

使所有h1的20px直到视口达到700px或更大.

@media screen and ( min-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助:0)


Sul*_*n C 20

从Bootstrap v3.3.6开始,使用以下媒体查询,这些查询与概述可用响应类的文档相对应(http://getbootstrap.com/css/#responsive-utilities).

/* Extra Small Devices, .visible-xs-* */
@media (max-width: 767px) {}

/* Small Devices, .visible-sm-* */
@media (min-width: 768px) and (max-width: 991px) {}

/* Medium Devices, .visible-md-* */
@media (min-width: 992px) and (max-width: 1199px) {}

/* Large Devices, .visible-lg-* */
@media (min-width: 1200px) {}
Run Code Online (Sandbox Code Playgroud)

从Bootstrap GitHub存储库中提取的媒体查询来自以下较少的文件: -

https://github.com/twbs/bootstrap/blob/v3.3.6/less/responsive-utilities.less https://github.com/twbs/bootstrap/blob/v3.3.6/less/variables.less


int*_*ico 20

这是一个更模块化的例子,使用LESS模仿Bootstrap而不导入较少的文件.

@screen-xs-max: 767px;
@screen-sm-min: 768px;
@screen-sm-max: 991px;
@screen-md-min: 992px;
@screen-md-max: 1199px;
@screen-lg-min: 1200px;

//xs only
@media(max-width: @screen-xs-max) {

}
//small and up
@media(min-width: @screen-sm-min) {

}
//sm only
@media(min-width: @screen-sm-min) and (max-width: @screen-sm-max) {

}
//md and up
@media(min-width: @screen-md-min) {

}
//md only
@media(min-width: @screen-md-min) and (max-width: @screen-md-max) {

}
//lg and up
@media(min-width: @screen-lg-min) {

}
Run Code Online (Sandbox Code Playgroud)


dam*_*amd 20

基于其他用户的答案,我编写了这些自定义mixin以便于使用:

减少投入

.when-xs(@rules) { @media (max-width: @screen-xs-max) { @rules(); } }
.when-sm(@rules) { @media (min-width: @screen-sm-min) { @rules(); } }
.when-md(@rules) { @media (min-width: @screen-md-min) { @rules(); } }
.when-lg(@rules) { @media (min-width: @screen-lg-min) { @rules(); } }
Run Code Online (Sandbox Code Playgroud)

用法示例

body {
  .when-lg({
    background-color: red;
  });
}
Run Code Online (Sandbox Code Playgroud)

SCSS输入

@mixin when-xs() { @media (max-width: $screen-xs-max) { @content; } }
@mixin when-sm() { @media (min-width: $screen-sm-min) { @content; } }
@mixin when-md() { @media (min-width: $screen-md-min) { @content; } }
@mixin when-lg() { @media (min-width: $screen-lg-min) { @content; } }
Run Code Online (Sandbox Code Playgroud)

用法示例:

body {
  @include when-md {
    background-color: red;
  }
}
Run Code Online (Sandbox Code Playgroud)

产量

@media (min-width:1200px) {
  body {
    background-color: red;
  }
}
Run Code Online (Sandbox Code Playgroud)


use*_*584 12

或简单的Sass-Compass:

@mixin col-xs() {
    @media (max-width: 767px) {
        @content;
    }
}
@mixin col-sm() {
    @media (min-width: 768px) and (max-width: 991px) {
        @content;
    }
}
@mixin col-md() {
    @media (min-width: 992px) and (max-width: 1199px) {
        @content;
    }
}
@mixin col-lg() {
    @media (min-width: 1200px) {
        @content;
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

#content-box {
    @include border-radius(18px);
    @include adjust-font-size-to(18pt);
    padding:20px;
    border:1px solid red;
    @include col-xs() {
        width: 200px;
        float: none;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

请记住,避免文本缩放是响应布局存在的主要原因.响应式网站背后的整个逻辑是创建功能布局,有效地显示您的内容,使其在多种屏幕尺寸上易于阅读和使用.

虽然在某些情况下有必要缩放文本,但请注意不要小型化您的网站并忽略这一点.

无论如何,这是一个例子.

@media(min-width:1200px){

    h1 {font-size:34px}

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

    h1 {font-size:32px}

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

    h1 {font-size:28px}

}
@media(max-width:767px){

    h1 {font-size:26px}

}
Run Code Online (Sandbox Code Playgroud)

另请注意,480引导视图已在引导程序3中删除.


小智 5

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

/* 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) { ... }
Run Code Online (Sandbox Code Playgroud)

另见Bootstrap


Gan*_*tta 5

您可以在我的示例中看到字体大小和背景颜色根据屏幕大小而变化

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
    background-color: lightgreen;
}
/* Custom, iPhone Retina */ 
@media(max-width:320px){
    body {
        background-color: lime;
        font-size:14px;
     }
}
@media only screen and (min-width : 320px) {
     body {
        background-color: red;
        font-size:18px;
    }
}
/* Extra Small Devices, Phones */ 
@media only screen and (min-width : 480px) {
     body {
        background-color: aqua;
        font-size:24px;
    }
}
/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {
     body {
        background-color: green;
        font-size:30px;
    }
}
/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {
     body {
        background-color: grey;
        font-size:34px;
    }
}
/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {
     body {
        background-color: black;
        font-size:42px;
    }
}
</style>
</head>
<body>
<p>Resize the browser window. When the width of this document is larger than the height, the background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)