[英]Why isn't media query on max-width working?
I want to control the size of my logo using media query. 我想使用媒体查询来控制徽标的大小。
The original width
of my logo is 210px
. 我徽标的原始
width
为210px
。
I want it to be 166px
when the screen width
is greater than 56.865em
and same when it is less than this width
, ie, for mobile site. 我想这是
166px
当屏幕width
大于56.865em
和同当小于这个width
,即移动网站。
I have written following code for this: 我为此编写了以下代码:
@media only screen and (min-width: 56.875em){
.site-branding img{
max-width: 166px;
}
}
@media only screen and (max-width: 56.875em){
.site-branding img {
max-width: 166px !important;
}
}
Only the first code block is working. 只有第一个代码块有效。 Why isn't second working?
为什么第二步不起作用? (When the screen width is decreased, the
width
of logo becomes 210px
again). (当屏幕宽度减小时,徽标的
width
再次变为210px
)。
Is there any rule that you can't use both min
and max
media-queries to control same element? 是否有任何规则不能同时使用
min
和max
媒体查询来控制同一元素?
The max-width
rule won't work because it is overridden by the min-width
since both have same value. max-width
规则无效,因为它们都具有相同的值,因此会被min-width
覆盖。
an easy approach, instead of doing 2 media queries
is simply setting the logo's width
in the general CSS and then apply a media query: 一个简单的方法,而不是执行
2 media queries
,只是在常规CSS中设置徽标的width
,然后应用媒体查询:
via non-mobile approach using the max-width
通过非移动方法使用
max-width
or 要么
via the mobile first approach using min-width
通过使用
min-width
的移动优先方法
max-width
max-width
选项 .logo img { width: 210px } @media (max-width: 56.865em) { .logo img { width: 166px } }
<div class="logo"> <img src="//lorempixel.com/300/300"> </div>
min-width
min-width
选项 .logo img { width: 166px } @media (min-width: 56.865em) { .logo img { width: 210px } }
<div class="logo"> <img src="//lorempixel.com/300/300"> </div>
First, I want the logo size 166px all the time.
首先,我希望徽标尺寸始终为166px。
So if you want after all is to have the logo's width
at 166px
all the time, meaning you want to change the current 210px
to 166px
因此,如果您始终希望徽标的
width
166px
为166px
,则意味着要将当前的210px
更改为166px
Then you don't need media queries. 然后,您不需要媒体查询。 Assuming you are changing a custom CSS file and you can't/want to change the Base CSS file (which contains the
width:210px
) all you need is to be more specific. 假设您要更改自定义CSS文件,并且您不能/想要更改基本CSS文件(其中包含
width:210px
),您需要做的只是更具体一点。
See more about CSS specificity in MDN and in W3C Specs 查看有关MDN和W3C规范中 CSS特异性的更多信息
/* emulating Base CSS */ .logo img { width: 210px } /*being more specific */ .header .logo img { width: 166px }
<div class="header"> <div class="logo"> <img src="//lorempixel.com/300/300"> </div> </div>
This drove me crazy but I found that commenting out text before @media
will block the statement. 这让我发疯,但我发现在
@media
之前注释掉文本会阻止该语句。
<!-- DO NOT COMMENT LIKE THIS BEFORE @media -->
/* USE THIS Comment version */
Hope it helps someone out! 希望它能帮助某人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.