我使用Bootstrap 3来构建一个响应式布局,我想根据屏幕大小调整一些字体大小。 我如何使用媒体查询来实现这种逻辑?
当前回答
根据其他用户的回答,我编写了这些自定义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;
}
}
其他回答
基于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) {
}
改进主响应:
您可以使用<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属性,可以保证更好的用户体验。
在这里你可以阅读谷歌关于这个问题的文章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
我们在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
:)
在最新的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/变量
快乐编码;)
使用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) {
}