【问题标题】:How to use @media correctly in css如何在 css 中正确使用 @media
【发布时间】:2017-03-16 10:07:21
【问题描述】:

如何在 css 中使用 @media 来获得特定的分辨率?所以我想让我的侧边栏更改取决于用户分辨率,所以我使用@media。

这是示例代码:

    @media (max-width: 1920px) and (min-width: 1080px){ /*for 1920 x 1080*/
  .logo1{
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 100%;
  }
}

@media (max-width: 1680px) and (min-width: 1050px){ /*for 1680 x 1050*/
  .logo1{
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 90%;
  }
}

@media (max-width: 1600px) and (min-width: 900px){ /*for 1600 x 900*/
  .logo1{
    margin-top: 50%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 40%;
  }
}

@media (max-width: 1440px) and (min-width: 900px){ /*for 1440 x 900*/
  .logo1{
    margin-top: 40%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 50%;
  }
}

@media (max-width: 1400px) and (min-width: 1050px){ /*for 1400 x 1050*/
  .logo1{
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 90%;
  }
}

@media (max-width: 1366px) and (min-width:768px){ /*for 1366 x 768 until 1024 x 768*/
  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 20%;
  }
}

在那些代码中,我对使用分辨率进行了注释,它运行良好,直到我从 1366x768 打开我的网站并在下面打开,它被@media (max-width: 1400px) and (min-width: 1050px) 覆盖。我如何防止它出现这个问题?谢谢

【问题讨论】:

  • 你用错了宽度和高度。
  • 天哪,所以我应该使用最小宽度和最小高度? @Roberrrt

标签: html css responsive-design media-queries screen-resolution


【解决方案1】:

您误以为媒体查询中的第二个参数定义了某个高度。它没有,它仍然定义了 width。

@media (max-width: 1920px) and (min-width: 1080px){
// You're assuming the screen's maximal width to be 1920,
// and it's minimal width to be 1080px, which means this CSS
// is applied when the screen's width is 1080px or more,
// but less than 1920px;

  .logo1 {
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2) {
    margin-top: 100%;
  }
}

你真正想要的是这个:

@media (min-width: 1920px) { /*1920px or larger*/
  .logo1 {
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 100%;
  }
}

@media (min-width: 1680px) { /*for 1680 x 1050*/
  .logo1{
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 90%;
  }
}

@media (min-width: 1600px) { /*for 1600 x 900*/
  .logo1{
    margin-top: 50%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 40%;
  }
}

@media (min-width: 1440px) { /*for 1440 x 900*/
  .logo1 {
    margin-top: 40%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 50%;
  }
}

@media (min-width: 1400px) { /*for 1400 x 1050*/
  .logo1{
    margin-top: 70%;
  }

  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 90%;
  }
}

@media (min-width: 1366px) { /*for 1366 x 768 until 1024 x 768*/
  .socmed a:nth-child(1), .socmed a:nth-child(2){
    margin-top: 20%;
  }
}

请记住,您使用这些规则更改的是“范围”而不是特定的视口。您不会根据 1920x1080 屏幕更改 CSS,但您会更改它当屏幕有足够的空间,即宽度为 1920 像素或更多时

【讨论】:

  • 我已更新我的答案以包含正确的值
  • 抱歉,当我在 1366*768 中打开时它可以工作,但是当我在 1920 年打开时,它会从“@media (min-width: 1366px)”中获取值。如何解决这个问题?
  • 那是因为1920的屏幕宽度会导致1915左右的浏览器宽度,这也需要以下更小的屏幕尺寸。这有点谜。
  • 在这种情况下我可以使用 min-height 吗?
  • 天哪,这实际上是我的错,我没有改变@media (min-width: 1366px)中的值.logo,非常感谢:D
【解决方案2】:

您只指定宽度和查询重叠。 “1920x1080”中的“1080”代表高度。

【讨论】:

  • 我应该使用什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
相关资源
最近更新 更多