根据屏幕尺寸切换 CSS 类

2024-04-01

CSS 新手在这里...

我正在研究一个响应式框架并想象我将如何完成不同的任务。

根据屏幕的大小,它们将类添加到 body 标记中,例如:

.PhoneVisible、.DesktopVisible 等...

他们还有一些类可以将链接链接到按钮中:

.btn、小按钮、中按钮、大按钮

我很困惑你会如何改变你的CSS。 IE。就像是:

    <a href="#" class="MyButtonOptions">XXXX</>

    .PhoneVisible .MyButtonOptions { btn small-button }
    .TabletVisible  .MyButtonOptions { btn  med-button }
    .DesktopVisible .MyButtonOptions { btn large-button }

您必须单独设置不同的选项吗?

i.e. .PhoneVisible .MyButtonOptions { 高度:30px; } ?

感谢所有建议!


CSS 媒体查询绝对是最佳选择。

您可以根据浏览器大小、像素密度等轻松分离 CSS。

以下是来自的示例列表CSS 技巧 http://css-tricks.com/snippets/css/media-queries-for-standard-devices/.

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
    /* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
    /* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
    /* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
    /* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
    /* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
    /* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
    /* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
    /* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
    /* Styles */
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据屏幕尺寸切换 CSS 类 的相关文章

随机推荐