Html 使用媒体查询内联样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13023157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:31:17  来源:igfitidea点击:

Use media query inline style

htmlcssmedia-queries

提问by HymanMahoney

I know it sounds ridiculous, but can I use a css media query in an inline style? The reason is I'm echoing a background in PHP and need to use a retina size image if the device is retina.

我知道这听起来很荒谬,但是我可以在内联样式中使用 css 媒体查询吗?原因是我在 PHP 中回显背景,如果设备是视网膜,则需要使用视网膜大小的图像。

ie: <div style="background:url(normal.png); <- need to add style for retina device

ie: <div style="background:url(normal.png); <- need to add style for retina device

回答by Marko

Not within an inline style declaration as far as I know.

据我所知,不在内联样式声明中。

However, if you're echoing from within PHP and reallycan't access the stylesheet I would suggest echoing an inline <style />element with the media query and using a class for the div.

但是,如果您在 PHP 中回显并且确实无法访问样式表,我建议您<style />使用媒体查询回显内联元素并为 div 使用一个类。

i.e.

IE

<style type="text/css">
    .bg {
        background: url(background.jpg);
    }
    @media only screen and (max-device-width: 480px) { /* Change to whatever media query you require */
        .bg {
             background: url(background_highres.jpg);
        }
    }

</style>
<div class="bg">...</div>