简体   繁体   English

错误制作自适应设计CSS

[英]Error Make Responsive Design CSS

I want to make my website to be responsive, so i using @media (max-width:1600px){} this code. 我想使我的网站具有响应性,因此我使用@media (max-width:1600px){}此代码。 I am making responsive design starting from the smallest width into the highest width. 我正在从最小的宽度到最大的宽度进行响应式设计。

so first i make responsive in 320 width: 所以首先我使响应宽度为320:

@media (max-width: 320px) {
.w3l_banner_info1 h3 {
    font-size: 17px;
    margin-top: -10px;
    line-height: 15px;
}
}

then in 360 width: 然后以360宽度:

@media (max-width: 360px) {
  .w3l_banner_info1 h3 {
  font-size: 20px;
  margin-top: -10px;
  line-height: 18px;
}
}

everything is going normal until i make responsive design in 800 width: 一切正常,直到我进行800宽度的自适应设计为止:

 @media (max-width: 800px) {
   .w3l_banner_info1 h3 {
    font-size: 28px;
    line-height: 64px;
    margin-top: -80px;
    margin-bottom: 10px;
    margin-left: 120px;
    margin-right: -100px;
}
}

The h3 in 320 and 360 width will changing like in 800 width. 320和360宽度的h3会像800宽度一样变化。 but if i remove the margin-left in 800 width, then the h3 in 320 and 360 width will be normal. 但是,如果我删除800宽度中margin-left ,那么320和360宽度中的h3将是正常的。 please help any advice, if i should make responsive design starting from the higest width? 请帮助任何建议,如果我应该从最大宽度开始进行响应式设计?

The problem you're mentioning is that your @media (max-width: 800px) rule matches if the window width is ≤ 800px, meaning it applies while both the 320px and 360px rules also apply. 您要提到的问题是,如果window宽度360px ,则@media (max-width: 800px)规则匹配,这意味着它适用,而320px360px规则也适用。

These rules are applied in order from top to bottom; 这些规则是按从上到下的顺序应用的; and a duplicate rule will override the previous rule. 重复的规则将覆盖之前的规则。

Example

  1. The 320px selector will apply the font-size , margin-top and line-height rules 320px选择器将应用font-sizemargin-topline-height规则
  2. Then the 360px selector will override these rules 然后360px选择器将覆盖这些规则
  3. Then the 800px selector will override these rules and add the additional margin rules. 然后800px选择器将覆盖这些规则并添加其他margin规则。

How to fix your issue 如何解决您的问题

I'd suggest taking advantage of the css and operator to change your rules as follows - 我建议利用CSS and运算符来更改您的规则,如下所示-

@media (max-width : 360px) {

}

@media (min-width : 360px) and (max-width : 800px) {

}

@media (min-width : 800px) {

}

Suggestion : If you're looking for a tried-and-tested example of standard media queries, try copying what Bootstrap 3 uses: http://getbootstrap.com/css/#responsive-utilities 建议 :如果您正在寻找一个经过测试的标准媒体查询示例,请尝试复制Bootstrap 3所使用的内容: http : //getbootstrap.com/css/#响应实用程序

Extra small devices [Phones] (<768px) 小型设备[电话](<768px)

Small devices [Tablets] (≥768px) 小型设备[平板电脑](≥768px)

Medium devices [Desktops] (≥992px) 中型设备[桌面](≥992px)

Large devices [Desktops] (≥1200px) 大型设备[桌面](≥1200px)

If you're using max-width , then yes, use the highest width first. 如果您使用的是max-width ,那么可以,首先使用最大宽度。 It all comes down to the fact that the parser takes the last rule it reads (that matches the specific media query). 归结为这样一个事实,即解析器采用了它读取的最后一条规则(与特定的媒体查询匹配)。

Put max-width:800px , after it max-width:360px and finally max-width: 320px as Descending. max-width:800px放在max-width:800px max-width:360px ,最后将max-width: 320px作为降序。

@media (max-width: 800px) {
   .w3l_banner_info1 h3 {
    background-color: blue;
   }
} 

@media (max-width: 360px) {
  .w3l_banner_info1 h3 {
  background-color: green;
  }
}    
@media (max-width: 320px) {
.w3l_banner_info1 h3 {
   background-color: red
  }
}

 @media (max-width: 800px) { .w3l_banner_info1 h3 { background-color: blue; } } @media (max-width: 380px) { .w3l_banner_info1 h3 { background-color: green; } } @media (max-width: 360px) { .w3l_banner_info1 h3 { background-color: red } } 
 <div class="w3l_banner_info1"> <h3>This is Test</h3> </div> 

Note:use full page and resize browser to see result. 注意:使用整页并调整浏览器大小以查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM