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

CSS

JimLEYHarry

2020-03-20

我正在使用Bootstrap 3构建响应式布局,在该布局中我想根据屏幕尺寸调整一些字体大小。如何使用媒体查询进行这种逻辑处理?

第2470篇《Twitter Bootstrap 3:如何使用媒体查询?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

16个回答
Stafan路易 2020.03.20

改善主要回应:

您可以使用标记media属性<link>(它支持媒体查询),以便仅下载用户所需的代码。

<link href="style.css" rel="stylesheet">
<link href="deviceSizeDepending.css" rel="stylesheet" media="(min-width: 40em)">

这样,浏览器将下载所有CSS资源,而与媒体属性无关不同之处在于,如果将media属性的media-query评估为false,则该.css文件及其内容将不会被渲染阻止。

Therefore, it is recommended to use the media attribute in the <link> tag since it guarantees a better user experience.

Here you can read a Google article about this issue https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css

Some tools that will help you to automate the separation of your css code in different files according to your media-querys

Webpack https://www.npmjs.com/package/media-query-plugin https://www.npmjs.com/package/media-query-splitting-plugin

PostCSS https://www.npmjs.com/package/postcss-extract-media-query

斯丁泡芙 2020.03.20

:)

在最新的引导程序(4.3.1)中,可以使用SCSS(SASS)使用@mixin中的一种 /bootstrap/scss/mixins/_breakpoints.scss

至少具有最小断点宽度的介质。不查询最小的断点。使@content适用于给定的断点且更宽。

@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints)

介质最大为最大断点宽度。没有查询最大的断点。使@content适用于给定的断点并变窄。

@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints)

跨多个断点宽度的介质。使@content适用于最小和最大断点之间

@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints)

断点的最小和最大宽度之间的媒体。最小的断点没有最小值,最大的断点没有最大值。使@content仅适用于给定的断点,而不适用于更宽或更窄的视口。

@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints)

例如:

.content__extra {
  height: 100%;

  img {
    margin-right: 0.5rem;
  }

  @include media-breakpoint-down(xs) {
    margin-bottom: 1rem;
  }
}

说明文件:

快乐的编码;)

TomLEY 2020.03.20

Bootstrap主要在源Sass文件中为布局,网格系统和组件使用以下媒体查询范围(或断点)。

超小型设备(纵向电话,小于576px)无媒体查询,xs因为这是Bootstrap中的默认设置

小型设备(横向手机,576像素及以上)

@media (min-width: 576px) { ... }

中型设备(平板电脑,768像素及以上)

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

大型设备(台式机,992px及以上)

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

超大型设备(大型台式机,1200px及以上)

@media (min-width: 1200px) { ... }

由于我们用Sass编写了源CSS,因此所有媒体查询都可以通过Sass mixins获得:

xs断点无需媒体查询,因为它有效 @media (min-width: 0) { ... }

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

为了深入了解它,请通过下面的链接

https://getbootstrap.com/docs/4.3/layout/overview/

西里Near 2020.03.20

@media only屏幕和(最大宽度:1200px){}

@media only屏幕和(最大宽度:979px){}

@media only屏幕和(最大宽度:767px){}

@media only屏幕和(最大宽度:480px){}

@media only屏幕和(最大宽度:320像素){}

@media(最小宽度:768像素)和(最大宽度:991像素){}

@media(最小宽度:992px)和(最大宽度:1024px){}

LLSam 2020.03.20

对IE使用媒体查询;

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) 
and (orientation : landscape) and (-ms-high-contrast: none), (-ms-high-contrast: active) {
}
@media only screen 
and (min-device-width : 360px) 
and (max-device-width : 640px) 
and (orientation : portrait) and (-ms-high-contrast: none), (-ms-high-contrast: active) {
}
SamSam 2020.03.20

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

<!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>

乐米亚 2020.03.20

我们在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) { ... }

另请参阅Bootstrap

Mandy村村 2020.03.20

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

尽管在某些情况下有必要缩放文本,但是请注意不要缩小站点大小,以免错过重点。

反正这是一个例子。

@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}

}

另外请记住,480视口已被放在引导程序3中。

西里神奇 2020.03.20

或简单的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;
    }
}

例:

#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;
    }
}
Green前端 2020.03.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(); } }

用法示例

body {
  .when-lg({
    background-color: red;
  });
}

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; } }

用法示例:

body {
  @include when-md {
    background-color: red;
  }
}

输出量

@media (min-width:1200px) {
  body {
    background-color: red;
  }
}
TonySamGreen 2020.03.20

从Bootstrap v3.3.6开始,使用以下媒体查询,这些媒体查询与概述可用响应类的文档相对应(http://getbootstrap.com/css/#response-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) {}

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

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

LL 2020.03.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) {

}
Pro猿A 2020.03.20

这是两个例子。

一旦视口变为700px或更小,则将所有h1标签设为20px。

@media screen and ( max-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}

使所有h1都为20px,直到视口达到700px或更大。

@media screen and ( min-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}

希望这可以帮助:0)

伽罗理查德 2020.03.20

这些是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){}
宝儿理查德 2020.03.20

基于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) {

}
番长樱梅 2020.03.20

引导程序3

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

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

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

<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>

引导程序4

这是BS4中使用的选择器。BS4中没有“最低”设置,因为“超小”是默认设置。也就是说,您首先要编码XS大小,然后再覆盖这些媒体。

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

更新2019年2月11日:BS3信息从3.4.0版本开始仍然是准确的,已为新网格更新了BS4,从4.3.0版开始是准确的。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android