【问题标题】:Optimizing css for a media query为媒体查询优化 css
【发布时间】:2021-10-09 16:14:02
【问题描述】:

早上好,

我为我的网站创建了一个媒体查询。我感觉它正在减慢我的网站,因为它没有优化。 在手机上,我有一个特定的图像。对于平板电脑和台式机,我要显示另一个图像。 我的媒体查询工作得很好。但它没有优化。 谁能帮我优化一下?

这是查询:

body, html {
    height : 100%;
    width: 100%;
    margin:0;
}

@media only screen and (min-width: 320px)  {

.Frontpage-image {
    background-image: url("https://dit.be/wp-content/uploads/2021/01/mobiel-01.svg");
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    height: 100vh;
}
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
    .Frontpage-image {
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
        height:100vh;
    }
}
    

@media screen and (min-width: 768px) {
    .Frontpage-image{
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
        height: 100vh;
    }
}
@media screen and (min-width: 1025px) {
    .Frontpage-image {
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
        height:100vh;
    }
}

【问题讨论】:

  • 嗨,您是指更短/更整洁的代码还是运行时优化?
  • 其实两者都有,但运行时优化对我来说更重要
  • 本页底部有一个代码审查 stackexchange 站点。

标签: css media-queries


【解决方案1】:

请使用此 CSS 代码。
在媒体查询中,在查询之外定义的样式会被继承。
所以我的代码和你的完全一样,但是代码已经大大减少了。

body, html {
    height : 100%;
    width: 100%;
    margin:0;
}
.Frontpage-image {
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    height: 100vh;
}

@media only screen and (min-width: 320px)  {

.Frontpage-image {
    background-image: url("https://dit.be/wp-content/uploads/2021/01/mobiel-01.svg");
}
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
    .Frontpage-image {
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
    }
}
    

@media screen and (min-width: 768px) {
    .Frontpage-image{
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
    }
}
@media screen and (min-width: 1025px) {
    .Frontpage-image {
        background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
    }
}

您可以查看this answer

【讨论】:

  • 请您解释一下代码如何优化事物,谢谢。
  • 请检查我更新的答案。
  • 谢谢,代码减少了,但它总是加载第一个图像,无论是否需要,所以存在运行时开销(我认为问题是关于)。
【解决方案2】:

这里有一些优化技巧

  1. 使用小尺寸图片
  2. 使用小尺寸视频或从 YouTube 或 Vimeo 嵌入视频
  3. 压缩此 CSS 代码。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2012-12-27
  • 2012-11-27
  • 1970-01-01
  • 2022-01-25
  • 2021-08-23
  • 2012-02-15
  • 2013-02-28
相关资源
最近更新 更多