Css Safari:媒体查询未按预期宽度触发
我已经写了一个CSS媒体查询 像这样-Css Safari:媒体查询未按预期宽度触发,css,safari,media-queries,breakpoints,Css,Safari,Media Queries,Breakpoints,我已经写了一个CSS媒体查询 像这样- @媒体屏幕和(最大宽度:59.9375em){ .左{ 显示:无; } }使用px(像素)而不是em。 em不是固定的,而是相对的。为不同的浏览器解析不同的内容 @media screen and (max-width: 59.9375px) { .left { display: none; } } 试试这个css黑客: @media screen and (min-color-index:0) and(-webkit-min-devic
@媒体屏幕和(最大宽度:59.9375em){
.左{
显示:无;
}
}
使用px(像素)而不是em。
em不是固定的,而是相对的。为不同的浏览器解析不同的内容
@media screen and (max-width: 59.9375px) {
.left {
display: none;
}
}
试试这个css黑客:
@media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0) {
@media {
.left {
display: none;
}
}}
资料来源:窗口宽度与实际宽度的对比实际上是一个非常有趣的话题,但简而言之,这取决于滚动条在不同浏览器中的放置方式
@media screen and (max-width: 59.9375px) {
.left {
display: none;
}
}
一些浏览器覆盖在内容/站点顶部的滚动条上。其他浏览器会缩短内容/站点的宽度,并在其旁边设置滚动条。这显然会在不同浏览器计算视口宽度的方式上产生一些差异
潜在的问题是将em
用作测量单位
请务必记住,em
是基于当前字体大小的度量单位,因此可由浏览器进行解释
根据您的字体系列
和总体字体大小
,60em通常在800px左右。这意味着您的查询将更加具体,如下所示:
@media screen and (max-width: 800px) {
.left {
display: none;
}
}
@media screen and (max-width: 800px) {
.left {
display: none !important;
}
}
如果不确定要覆盖的样式,可以始终应用以下重要规则:
@media screen and (max-width: 800px) {
.left {
display: none;
}
}
@media screen and (max-width: 800px) {
.left {
display: none !important;
}
}
如果您希望而不是,请使用!重要信息
在CSS中标记,然后需要确保注意以下两种情况:
CSS从上到下读取 这意味着,如果为
.left
元素指定了规则,则需要将其放置在媒体查询的之前,而不是之后
错误的布局如下所示:
@media screen and (max-width: 800px) { //media query BEFORE rule
.left {
display: none;
}
}
.left {
.display:block;
}
.left {
.display:block;
}
@media screen and (max-width: 800px) { //media query AFTER rule
.left {
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
.left { //only 1 selector used in query therefore overwritten by the previous rule - this should have atleast 2 selectors to overwrite the previous rule
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
body .container .left { //3 selectors used in query
display: none;
}
}
正确的布局如下所示:
@media screen and (max-width: 800px) { //media query BEFORE rule
.left {
display: none;
}
}
.left {
.display:block;
}
.left {
.display:block;
}
@media screen and (max-width: 800px) { //media query AFTER rule
.left {
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
.left { //only 1 selector used in query therefore overwritten by the previous rule - this should have atleast 2 selectors to overwrite the previous rule
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
body .container .left { //3 selectors used in query
display: none;
}
}
要记住的下一点是:
嵌套CSS选择器优先 在媒体查询规则中使用相同数量的父选择器(或更多) 选择器系列错误:
@media screen and (max-width: 800px) { //media query BEFORE rule
.left {
display: none;
}
}
.left {
.display:block;
}
.left {
.display:block;
}
@media screen and (max-width: 800px) { //media query AFTER rule
.left {
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
.left { //only 1 selector used in query therefore overwritten by the previous rule - this should have atleast 2 selectors to overwrite the previous rule
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
body .container .left { //3 selectors used in query
display: none;
}
}
正确的选择器系列:
@media screen and (max-width: 800px) { //media query BEFORE rule
.left {
display: none;
}
}
.left {
.display:block;
}
.left {
.display:block;
}
@media screen and (max-width: 800px) { //media query AFTER rule
.left {
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
.left { //only 1 selector used in query therefore overwritten by the previous rule - this should have atleast 2 selectors to overwrite the previous rule
display: none;
}
}
.container .left { //2 selectors used in query
.display:block;
}
@media screen and (max-width: 800px) {
body .container .left { //3 selectors used in query
display: none;
}
}
根据css规则,您必须在.left类之后使用媒体查询 比如说
.left {
display:inline;
}
@media screen and (max-width: 59.9375em) {
.left {
display: none !important; //important will override all the .left class.
}
}
您应该阅读以下两篇文章:
TLDR:em值基于根字体大小值,但在Safari与其他浏览器的情况下,
em
要么相对于初始值,要么相对于根值(浏览器为媒体查询选择一个或另一个,但不是两个,这可能会导致不同浏览器之间的差异)您的想法是正确的,但需要更改数量(59.9375)以及测量单位,因为59.9375px很小。目前没有像59 px那么小的移动屏幕,因此此媒体查询永远不会启动。这只是一个演示。根据您的要求进行更改。我如何知道用户的屏幕宽度。(别忘了将答案标记为有用)@FritsThis非常有趣-你能解释一下黑客是如何达到预期效果的吗?我不知道这到底是怎么回事,我更新了我的帖子以精确地告诉你我的消息来源。这一条对我和你都有效?还有一个类似的问题非常有趣,它会引起一个有趣的半相关阅读:)绝对没有理由使用!重要信息
请点击此处。你为什么要那样做?特别是以后,您可能想再次使用该类,然后您将不得不再次使用!重要信息
。在大多数情况下,像这样提高特异性是一个坏主意。Safari在closing}中非常完美,请检查它们是否都已关闭并且css代码中没有错误。