我使用Bootstrap 3来构建一个响应式布局,我想根据屏幕大小调整一些字体大小。 我如何使用媒体查询来实现这种逻辑?
当前回答
引导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存储库中提取的媒体查询从以下较少的文件
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文档
你可以在引导GitHub存储库中看到v5.1.3的断点:-
https://github.com/twbs/bootstrap/blob/v5.1.3/scss/_variables.scss#L461 https://github.com/twbs/bootstrap/blob/v5.1.3/scss/mixins/_breakpoints.scss
更新日期:2021-12-19
其他回答
引导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存储库中提取的媒体查询从以下较少的文件
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文档
你可以在引导GitHub存储库中看到v5.1.3的断点:-
https://github.com/twbs/bootstrap/blob/v5.1.3/scss/_variables.scss#L461 https://github.com/twbs/bootstrap/blob/v5.1.3/scss/mixins/_breakpoints.scss
更新日期:2021-12-19
@media only screen和(max-width: 1200px) {} @media only screen (max-width: 979px) {} @media only screen (max-width: 767px) {} @media only screen和(max-width: 480px) {} @media only screen and (max-width: 320px) {} @media (min-width: 768px) and (max-width: 991px) {} @media (min-width: 992px) and (max-width: 1024px) {}
:)
在最新的bootstrap(4.3.1)中,使用SCSS(SASS),您可以使用来自/bootstrap/ SCSS /mixins/_breakpoints.scss的@mixin之一
至少是最小断点宽度的媒体。不查询最小的断点。 使@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;
}
}
文档:
介绍https://getbootstrap.com/docs/4.3/layout/overview/ responsive-breakpoints 迁移https://getbootstrap.com/docs/4.3/migration/ responsive-utilities 变量https://getbootstrap.com/docs/4.3/layout/grid/变量
快乐编码;)
这里有一个更简单的一站式解决方案,包括基于媒体查询的独立响应文件。
这允许所有媒体查询逻辑和包含逻辑只存在于一个页面,即加载器。它还允许媒体查询不会使响应式样式表本身混乱。
//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
* 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。更少的会像这样
/**
* 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" />
很简单…
基于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) {
}