繁体   English   中英

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

[英]Twitter Bootstrap 3: how to use media queries?

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

引导程序 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){}

引导程序 5

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

2021 年 5 月 20 日更新:从 3.4.1、4.6.0、5.0.0 版本开始,信息仍然准确。

根据 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) {

}

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

@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 */

@screen-sm-max@screen-md-max也有变量,它们分别比@screen-md-min@screen-lg-min少 1 个像素,通常用于@media(max-width)

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

这些是来自 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){}

这里有两个例子。

一旦视口宽度变为 700 像素或更小,使所有 h1 标签变为 20 像素。

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

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

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

希望这有帮助:0)

引导程序 3

对于 Bootstrap 3 (v3.4.1) 的最终版本,使用了以下媒体查询,这与概述可用响应类的文档相对应。 https://getbootstrap.com/docs/3.4/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) {}

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

https://github.com/twbs/bootstrap/blob/v3.4.1/less/variables.less#L283 https://github.com/twbs/bootstrap/blob/v3.4.1/less/responsive-utilities.less

引导程序 5

从版本 5 的文档中,您可以看到自版本 3 以来媒体查询断点已更新,以更好地适应现代设备尺寸。

// X-Small devices (portrait phones, less than 576px)
// No media query for `xs` 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) { ... }

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

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

来源:Bootstrap 5 文档


您可以在 Bootstrap GitHub 存储库中看到 v5.0.1 的断点:-

https://github.com/twbs/bootstrap/blob/v5.0.1/scss/_variables.scss#L304 https://github.com/twbs/bootstrap/blob/v5.0.1/scss/mixins/_breakpoints.scss

更新于 2021-05-04

这是一个更模块化的示例,使用 LESS 来模拟 Bootstrap,而不导入 less 文件。

@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) {

}

根据其他用户的回答,我编写了这些自定义 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;
  }
}

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

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

尽管在某些情况下需要缩放文本,但请注意不要将站点小型化而错过重点。

无论如何,这里有一个例子。

@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 中删除。

我们在 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

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

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

Bootstrap 主要在我们的布局、网格系统和组件的源 Sass 文件中使用以下媒体查询范围或断点。

超小设备(人像手机,小于 576px) 没有xs媒体查询,因为这是 Bootstrap 中的默认设置

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

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

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

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

这是一个更简单的一站式解决方案,包括基于媒体查询的单独响应文件。

这允许所有媒体查询逻辑和包含逻辑只存在于一个页面上,即加载器。 它还允许媒体查询不会使响应式样式表本身变得混乱。

//loader.less

// this twbs include adds all bs functionality, including added libraries such as elements.less, and our custom variables
@import '/app/Resources/less/bootstrap.less';

/*
* Our base custom twbs overrides
* (phones, xs, i.e. less than 768px, and up)
* no media query needed for this one since this is the default in Bootstrap
* All styles initially go here.  When you need a larger screen override, move those     
* overrides to one of the responsive files below
*/
@import 'base.less';

/*
* Our responsive overrides based on our breakpoint vars
*/
@import url("sm-min.less") (min-width: @screen-sm-min); //(tablets, 768px and up)
@import url("md-min.less") (min-width: @screen-md-min); //(desktops, 992px and up)
@import url("large-min.less") (min-width: @screen-lg-min); //(large desktops, 1200px and up)

base.less 看起来像这样

/**
* base.less
* bootstrap overrides
* Extra small devices, phones, less than 768px, and up
* No media query since this is the default in Bootstrap
* all master site styles go here
* place any responsive overrides in the perspective responsive sheets themselves
*/
body{
  background-color: @fadedblue;
}

sm-min.less 看起来像这样

/**
* sm-min.less
* min-width: @screen-sm-min
* Small devices (tablets, 768px and up)
*/
body{
  background-color: @fadedgreen;
}

你的索引只需要加载 loader.less

<link rel="stylesheet/less" type="text/css" href="loader.less" />

十分简单..

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

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

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

对 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) {
}

:)

在最新的引导程序 (4.3.1) 中,使用 SCSS(SASS),您可以使用 /bootstrap/scss/mixins/_breakpoints.scss 中的/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;
  }
}

文档:

快乐编码;)

改善主要反应:

您可以使用<link>标签的media属性(它支持媒体查询)来只下载用户需要的代码。

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

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

因此,建议在<link>标签中使用media属性,这样可以保证更好的用户体验。

在这里您可以阅读有关此问题的 Google 文章https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css

一些工具可以帮助您根据媒体查询自动分离不同文件中的 css 代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM