简体   繁体   English

宽度不会根据媒体查询响应

[英]Width doesn't respond according to the media query

I was experimenting with media queries to see the effects. 我正在尝试使用媒体查询来查看效果。 So I tried using min-width(480px) query to change the width of a div from 100% to 520px when the window was maximised but the width of the div stays 100% . 因此, 当窗口最大化div的宽度保持100% 时,我尝试使用min-width(480px)查询将div的宽度从100%更改为520px

The code: 编码:

#box {
    margin: auto;
    background: white;
    width: 100%;
}
// Media Queries
@media only screen and (min-width: 480px) {
    #box {
        width: 200px;
        max-width: 200px;
        background: black;
    }
}

So my question is, why does the width of the #box stay as 100% when the window is maximised ? 所以我的问题是,为什么当窗口最大化时 #box的宽度保持为100%
What am I doing wrong? 我究竟做错了什么?

jsFiddled here is your code with min-width:480px. 这里的jsFiddled是你的代码,最小宽度:480px。 It applies when the size of available space is bigger than 480px (the black box) 它适用于可用空间大小大于480px(黑匣子)的情况

try max-width . 尝试最大宽度 This context will apply when available screen space is less then 480 pixels. 当可用屏幕空间小于480像素时,将应用此上下文。 jsFiddled here , black box will be applied when available space width is lesser than 480px jsFiddled here ,当可用空间宽度小于480px时,将应用黑盒子

@media only screen and (max-width: 480px) {
    #box {
        width: 200px;
        max-width: 200px;
        background: black;
    }
}

So, your #box is by default 100% width except when the available space is greater than 480px. 因此,除非可用空间大于480px,否则#box默认为100%宽度。 your code is working OK. 你的代码工作正常。

Maybe it's the comment : // Media Queries witch caused an error ? 也许这是评论: //媒体查询女巫导致错误?

I think you have the media query wrong. 我认为你的媒体查询错了。 As you have it now, it's changes the content over 480px. 正如你现在拥有它,它改变 480像素的内容。 Where I think you want it under 480px. 我觉得你想要它在480px以下。

So it should be: 所以它应该是:

@media only screen and (max-width: 480px) {
  //code
}

Hence, ( max -width: xxx) not, ( min -width: xxx) . 因此, ( max -width: xxx)不是, ( min -width: xxx)

Example fiddle 示例小提琴

I had commented the code using // syntax by accident which isn't supported in CSS, hence the code below that line of comment not working. 我偶然使用//语法对代码进行了评论,这在CSS中是不受支持的,因此该评论行下面的代码不起作用。 It now works after I changed it /**/ syntax. 它现在可以在我更改它/**/语法后工作。

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

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