html - @media query <img> src swap 不工作

标签 html css

我在这里查看了一些问题和答案,但仍然找不到我的问题。

我想做的是: 根据屏幕大小打开或关闭这三个图像的显示属性。一次只能显示一张图像。

除了@media 查询部分中列出的内容外,我没有将任何 css 应用到 .imagephone。

.imgcontainer{
    background-color: red;
    width: 100%;
    overflow: hidden;
    max-width: 1500px;
    margin: auto;
}

.imagetablet{
    display: none;
}

.imagedesktop{
    display: none;
}

/**************@Media Tablets****************/
    
    @media screen and (max-width="800px"){
        .imagephone{display:none;}
        .imagetablet{display:block;}
        .imagedesktop{display:none;}
        }

/**************@Media Desktop*****************/
    
    @media screen and (max-width="1200px"){
        .imagephone{display:none;}
        .imagetablet{display:none;}
        .imagedesktop{display:block;}
        }
<div class="imgcontainer">
        <img class="imagephone" src="BannerPicSmall.png">
        <img class="imagetablet" src="BannerPicMedium.png">
        <img class="imagedesktop" src="BannerPicLarge.png">
    </div>

最佳答案

 	.imgcontainer{
    background-color: white;
    width: 100%;
    overflow: hidden;
    max-width: 1500px;
    margin: auto;
}

.imagetablet{
    display: none;
}

.imagedesktop{
    display: none;
}

/**************@Media Tablets****************/
    
    @media screen and (max-width:800px){
        .imagephone{display:none !important;}
        .imagetablet{display:block !important;}
        .imagedesktop{display:none !important;}
        }

/**************@Media Desktop*****************/
    
    @media screen and (max-width:1200px){
        .imagephone{display:none !important;}
        .imagetablet{display:none !important;}
        .imagedesktop{display:block !important;}
        }

   
<div class="imgcontainer">
        <img class="imagephone" src="http://www.claudelorrain.org/An-Artist-Studying-Nature.jpg">
        <img class="imagetablet" src="http://hdwallpaperia.com/wp-content/uploads/2015/01/Nature-Wallpaper-daydreaming-34811101-1024-7686-640x480.jpg">
        <img class="imagedesktop" src="http://hdwallpaperia.com/wp-content/uploads/2013/10/Beautiful-Nature-Desktop-Wallpaper-640x360.jpg">
    </div>

试试这个。

希望对您有所帮助。

关于html - @media query <img> src swap 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40438040/

相关文章:

javascript - 如何使用 CSS 创建具有 2 种或更多颜色的元素(链接),使其看起来像彩虹?

html - 来自youtube的视频源html5

转换 : all used? 时,JQuery Slide Toggle 在这个粘性页脚上不起作用

ios - Cordova - 使用固定页眉和页脚滚动 (ios)

javascript - 为什么从可拖动的 jquery 中获取错误的左侧和顶部位置值

html - IE7 和 IE6 在使用无序列表(额外间距等)进行导航时遇到问题

html - 如何在两个 tr 或行之间添加空格 - CSS

html - 当字体粗细为 :bold 时,拉丁语扩展字符在表格中被置换

css - 如何防止后台附件 :fixed; from lagging my website?

html - 这个 CSS 有什么问题(尝试创建一个 "digg like"分页页脚)