【发布时间】:2021-10-14 01:16:34
【问题描述】:
在响应式 CSS 的示例中,常见的做法是将一个 UI 布局完全声明为“默认”布局(即不在媒体查询中),然后媒体查询添加属性和/或 覆盖这些属性以创建不同的布局/外观。
像这样:
/**** Small screen layout: ****/
.toolbar {
background-color: #000;
width: 100%; /**** Declared here for the default layout ****/
position: absolute; /**** Declared here for the default layout ****/
}
/**** Large screen layout: ****/
@media (min-width: 500px)
{
.toolbar {
position: relative; /**** Declared again, just to 'remove' it ****/
width: auto; /**** Declared again, just to 'remove' it ****/
border-radius: 5px;
box-shadow: 0px 5px 10px rgba(0,0,0,0.3);
}
}
我想这样做:只在媒体查询之外声明公共属性
/**** Common properties ****/
.toolbar {
background-color: #000;
}
/**** Small-screen-only properties: ****/
@media (max-width: 499px)
{
.toolbar {
width: 100%; /**** Only declared once, where required ****/
position: absolute; /**** Only declared once, where required ****/
}
}
/**** Large-screen-only properties: ****/
@media (min-width: 500px)
{
.toolbar {
border-radius: 5px;
box-shadow: 0px 5px 10px rgba(0,0,0,0.3);
}
}
我想使用第二种方法,因为我更喜欢这样工作,但它有更多选择器供浏览器解析。
因此,第二种方法在实际场景中的性能是否会明显更差?
【问题讨论】:
标签: css performance browser responsive