简体   繁体   English

为什么@media(max-device-width:360px)受@media影响(max-device-width:1024px)?

[英]Why is @media (max-device-width: 360px) affected by the @media (max-device-width: 1024px)?

Just curious why one of the "media size" is affected by the bigger "bigger media size"? 只是好奇为什么其中一个“媒体大小”受到更大“更大的媒体规模”的影响? I have more code but where thing else works but these two. 我有更多的代码,但其他东西工作,但这两个。

 @media only screen and (max-device-width: 360px){ .contact_center_image{ position: relative; margin-left: 20%; bottom: 50px; } .image_size{ width: 400px; height: 300px; } } @media only screen and (max-device-width: 1024px){ .contact_center_image{ position: relative; margin-left: 23%; bottom: 50px; } .image_size{ width: 1000px; height: 700px; } } 
 <div class="contact_center_image"> <img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Image" class="image_size"> </div> 

CSS stands for "Cascading Style Sheets", meaning there is a vertical hierarchy. CSS代表“Cascading Style Sheets”,意思是有一个垂直层次结构。 Your lower code takes dominance when both rules apply, and in this case, a screen size less than 360px is also less than 1024px. 当两个规则都适用时,较低的代码占主导地位,在这种情况下,小于360px的屏幕尺寸也小于1024px。 If you want the 360px rule to have greater priority, you need to move it below the 1024px snippet. 如果您希望360px规则具有更高的优先级,则需要将其移至1024px片段下方。

@media only screen and (max-device-width: 1024px){
    .contact_center_image{
        position: relative;
        margin-left: 23%;
        bottom: 50px;       
    }
    .image_size{
        width: 1000px;
        height: 700px;
    }
}


@media only screen and (max-device-width: 360px){
    .contact_center_image{
        position: relative;
        margin-left: 20%;
        bottom: 50px;       
    }
    .image_size{
        width: 400px;
        height: 300px;
    }
}
@media only screen and (max-device-width: 1024px){

This conditional will return true for any device width size up to and including the spacified value. 对于任何设备宽度大小(包括空间值),此条件将返回true

So think of it as: 所以把它想象成:

@media only screen and (any-device-width-up-to-1024+1px){

Therefore 360px width is less than 1024px width so both your @media rules will be applied. 因此360px宽度小于1024px宽度,因此将应用您的@media规则。

Try using min-device-width rules instead, or a combination of both max- and min- for each @media scope. 尝试使用min-device-width规则,或者对每个@media范围使用max-min-的组合。

While you can have multiple rules Cascading over each other (when both apply, the last one is applied last), this will cause a lot of style repetitions and you'd need to update multiple @media statements to make a change propergate through these cascades. 虽然你可以有多个规则相互叠加 (当两者都适用时,最后一个应用最后),这将导致很多样式重复,你需要更新多个@media语句,通过这些级联进行更改。

Example Tweak: 示例调整:

Below, the first rule will apply to widths of 361 up to 1024 the second will apply to widths from 0 up to 360. 下面,第一条规则将适用于361到1024的宽度,第二条规则适用于从0到360的宽度。

@media only screen and (max-device-width: 1024px)
                   and (min-device-width: 361px) {
    .contact_center_image{
        position: relative;
        margin-left: 23%;
        bottom: 50px;       
    }
    .image_size{
        width: 1000px;
        height: 700px;
    }
}


@media only screen and (max-device-width: 360px){
    .contact_center_image{
        position: relative;
        margin-left: 20%;
        bottom: 50px;       
    }
    .image_size{
        width: 400px;
        height: 300px;
    }
}

暂无
暂无

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

相关问题 如何在媒体查询中混合使用“最大宽度”和“最大设备宽度” - How to mix `max-width` and `max-device-width` in media query 媒体查询不适用于 max-width 但适用于 max-device-width - Media query not working with max-width but working with max-device-width CSS媒体查询有关max-width属性的信息,使平板电脑和台式机的尺寸为1024px - CSS Media Queries regarding max-width property to make 1024px size for Tablet and Desktop 如何为笔记本电脑1024px和iPad 1024px宽度设置不同的媒体查询? - How to set different media-queries for laptop 1024px and ipad 1024px width? 当@media屏幕和(max-width:360px)应该居中时,如何将右div的内容居中 - how to center the content of right div in center also when @media screen and (max-width : 360px) it should be at center 使用最大设备宽度调整大小时如何隐藏图像? - How to hide image when resizing using max-device-width? 带有CSS标签max-device-width的html中的if子句,可能吗? - if clause in html with CSS tag max-device-width, is it possible? Chrome、Firefox 等是否为移动设备指定了 max-device-width? - Does Chrome,Firefox,etc specify max-device-width to mobile? 智能手机接受(最大设备宽度:x),但不接受(最大宽度:x) - smartphone accepts (max-device-width: x) but not (max-width: x) 标准1024像素分辨率的最大页面宽度,以便不显示水平滚动条? - Max Page width for standard 1024px resolution so that no horizontal scroll bar displays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM