【问题标题】:Responsive CSS @media queries响应式 CSS @media 查询
【发布时间】:2016-05-21 06:33:48
【问题描述】:

为了使我的网站具有响应性,我正在使用这样的媒体查询

@media screen and (max-width: 320px){code}
@media screen and (max-width: 480px){code}
@media screen and (max-width: 640px){code}

但是当我尝试测试我的网站时,我遇到了一些问题。一种问题是@media 320px 没问题,但是当我将网页 320px 重新调整为 480px 或 640px 默认情况下,320px 即将到来,其他两个查询不起作用。即使我正在尝试使用 min-width:320px 但结果是相同的。现在我该怎么办? 我的另一个问题是:最大宽度和最小宽度有什么不同?什么时候应该使用哪一个?

也试过这个。 代码:

    @media screen and (min-width: 320px){code}
    @media screen and (min-width: 480px){code}
    @media screen and (min-width: 640px){code}

完整的响应代码,

代码:

/*640px*/
@media screen and (min-width : 640px) {
    .colum {
        max-width: 960px;
        margin: 0 auto;
    }
    .navbar-header h1 {
        margin-left: 20px;
    }
    .welcome_text h4 {
        font-size: 25px;
    }
    .welcome_text h1 {
        font-size: 50px;
        letter-spacing: 20px;
    }
    
    
    .promo_title h1, .work_title h1 {
        max-width: 640px;
    }
    .promo_title p, .work_title p{font-size: 18px;
    color: #7e8287;
    width: 620px;
    padding: 0;
    }
    .promo_list{
        margin-bottom: 390px;
    }
    
    .fix1{
        overflow: hidden;
    }
    .clear1 {
        clear: both;
        margin-top: 20px;
    }
    .extramarg {
        margin-top: 30px;
    }
    .rightborder {
    position: absolute;   
    height: 175px;
    border-right: 1px solid #d9dbdb;
    right: 313px;
    }
    .single_list {
        border-right: none;
        width: 315px;
    }
    .rightimg {
        background: none;
        right: 0;
        top: 0;
        width: 0;
        height: 0;
        opacity:0;
    }
    .service_list {
        padding: 60px 25px 20px;
        text-align: center;
    }
    .leftarea {
        width: 640px;
        padding: 0 25px;
    }
    .service_list h1:before {
        left: 36%;
    }
    .service_item .ss_list {
        width: 295px;
        padding-left: 0;
        padding-bottom: 30px;
        text-align: center;
    }
    .work_title p {
        margin: 0;
    }
    .work_slider {
        width: 50%;
    }
    .slider_detail h3 {
        padding-top: 60px;
    }
    .promo_title h1, .work_title h1 {
        font-size: 40px;
    }
    .msg_area .colum {
        width: 640px;
    }
    .msg_area textarea {
        width: 570px;
    }
    .contact input[type="text"], .contact input[type="email"] {
        width: 540px;
    }
    .submit a {
        padding: 10px;
    }
    .promo_title, .work_title {
        padding-top: 60px;
    }
}

/*480 */
@media screen and (min-width : 480px) {
    .colum {
        max-width: 320px;
        margin: 0 auto;
    }
    .navbar-header h1 {
        margin-left: 20px;
    }
    .welcome_text h1 {
    font-size: 25px;
    margin: 12px 0;
    letter-spacing: 12px;
    }
    .welcome_text h4 {
        font-size: 15px;
    }
    .promo_title h1, .work_title h1 {
        max-width: 320px;
    }
    .promo_title p, .work_title p{
    font-size: 12px;
    color: #7e8287;
    width: 340px;
    padding:0;
    }
    .single_list {
        border-bottom: 1px solid #d9dbdb;
        border-right: 0;
        padding: 20px 0;
        width: 320px;
    }
    .promo_list div:last-child {
      border-bottom: 0;
    }
    .promo_area {
        margin-bottom: -230px;
    }
    .floatleft {
        float: none;
    }
    .leftarea {
        width: 320px;
    }
    .leftarea h1 {
       text-align: center
    }
    .service_list p {
            padding: 35px 0;
    text-align: center;
    }
    .service_list h1:before {
        left:75px;
    }
    .service_list h1 {
        font-size: 38px;
    }
    .service_list {
        padding: 60px 0;
    }
    .service_item .ss_list {
        width: 320px;
        padding-left: 0;
        padding-bottom: 30px;
        text-align: center;
        }
    .work_slider {
        width: 80%;
        margin: 0 auto;
    }
    .slider_detail h3 {
        padding-top: 80px;
    }
    .promo_title h1, .work_title h1 {
        font-size: 30px;
    }
    .rightimg {
      background:none;
      right: 0;
      top: 0;
      width:0;
      height: 0;
      opacity: 0;
    }
    .contact input[type="text"], .contact input[type="email"], .contact textarea{
        width:320px;
    }
    .submit a {
        padding: 10px;
    }
    .promo_title, .work_title {
        padding: 40px 0;
    }
}


/* Smartphones (portrait and landscape) ----------- */
@media screen and (min-width : 320px) {
    .colum {
        max-width: 320px;
        margin: 0 auto;
    }
    .navbar-header h1 {
        margin-left: 20px;
    }
    .welcome_text h1 {
    font-size: 25px;
    margin: 12px 0;
    letter-spacing: 12px;
    }
    .welcome_text h4 {
        font-size: 15px;
    }
    .promo_title h1, .work_title h1 {
        max-width: 320px;
    }
    .promo_title p, .work_title p{
    font-size: 12px;
    color: #7e8287;
    width: 300px;
    padding:0;
    }
    .single_list {
        border-bottom: 1px solid #d9dbdb;
        border-right: 0;
        padding: 20px 0;
        width: 320px;
    }
    .promo_list div:last-child {
      border-bottom: 0;
    }
    .promo_area {
        margin-bottom: -230px;
    }
    .floatleft {
        float: none;
    }
    .leftarea {
        width: 320px;
    }
    .leftarea h1 {
       text-align: center
    }
    .service_list p {
            padding: 35px 0;
    text-align: center;
    }
    .service_list h1:before {
        left:75px;
    }
    .service_list h1 {
        font-size: 38px;
    }
    .service_list {
        padding: 60px 0;
    }
    .service_item .ss_list {
        width: 320px;
        padding-left: 0;
        padding-bottom: 30px;
        text-align: center;
        }
        .work_slider {
        width: 100%;
        }
    .slider_detail h3 {
        padding-top: 60px;
    }
    .promo_title h1, .work_title h1 {
        font-size: 30px;
    }
    .rightimg {
      background:none;
      right: 0;
      top: 0;
      width:0;
      height: 0;
      opacity: 0;
    }
    .contact input[type="text"], .contact input[type="email"], .contact textarea{
        width:320px;
    }
    .submit a {
        padding: 10px;
    }
    .promo_title, .work_title {
        padding: 40px 0;
    }
}

【问题讨论】:

  • 将@media 查询倒序排列,因为所有 3 种情况下的查询都为真
  • 你的意思是 320px 然后 480px 然后 640px?
  • 是的..这将使查询按预期运行..
  • 非常感谢。

标签: html css media responsive


【解决方案1】:

那是错误的代码.. @media 屏幕和(最小宽度:320px){ 此代码将在 320px 及以上的屏幕上运行 如果你把代码放在这里当然这个代码将以 480px 和 680px 运行 }

顶部阻止该代码来自 480px 和 680px 让它像这样 @media screen and (min-width: 320px) and (max-width: 479px){ 这里的代码 }

【讨论】:

    【解决方案2】:

    试试这个。

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body {
        background-color: lightblue;
    }
    
    @media screen and (max-width: 850px) {
        body {
            background-color: lightcoral;
        }
    }
    @media screen and (max-width: 480px) {
        body {
            background-color: lightgreen;
        }
    }
    </style>
    </head>
    <body>
    
    <h1>Resize the browser window to see the effect!</h1>
    <p>The media query will only apply if the media type is screen and the viewport is 480px wide or wider.</p>
    
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      试试吹码:

      @media screen and (max-width: 320px){code} @media (min-width: 320px) and (max-width: 480px) {code} @media (min-width: 480px) and (max-width: 640px) {code} 希望您的问题能得到解决

      【讨论】:

        【解决方案4】:

        (编辑说明:更正了一些错别字和错误)

        您必须考虑 CSS 是“级联”(=下降),这意味着:如果您对同一元素有两个不同的规则,则后者(=较低)将覆盖前面的规则。所以在你的第一个代码示例中:

        @media screen and (max-width: 320px){code}
        @media screen and (max-width: 480px){code}
        @media screen and (max-width: 640px){code}
        

        第三个媒体查询(最大宽度:640)中写入的任何内容都将覆盖第一个中写入的内容,因为宽度低于 320 像素的所有内容低于 640 像素。

        所以你有两个选择:

        1.) 翻转订单:

        @media screen and (max-width: 640px){code}
        @media screen and (max-width: 480px){code}
        @media screen and (max-width: 320px){code}
        

        这样,如果屏幕是 300 像素宽,它将首先遵循前两个查询的规则,然后是第三个查询中的规则,这将覆盖其他查询。这被称为桌面优先方法,因为它首先列出了更大(桌面)屏幕(在媒体查询之上)的规则,然后分三个步骤被较小设备的规则覆盖(首先最大 640,然后是 480,然后是 320px )。因此,450 像素宽的平板手机将读取第一个和第二个查询中的规则(第二个覆盖第一个),然后停止并忽略第三个媒体查询中的规则,因为它的宽度低于 320 像素。

        作为替代方案:

        2.) 按升序使用媒体查询,但使用最小宽度:

        @media screen and (min-width: 320px){code}
        @media screen and (min-width: 480px){code}
        @media screen and (min-width: 640px){code}
        

        这是一种移动优先的方法:它首先列出移动规则(即所有至少 320 像素宽的东西),然后再往上走。因此,360px 设备将在第二次媒体查询后停止读取规则,台式计算机将读取所有这三个,同样,第三条规则中的规则将覆盖第一次查询中的规则。

        【讨论】:

          猜你喜欢
          • 2018-12-22
          • 1970-01-01
          • 2021-01-11
          • 1970-01-01
          • 2018-03-25
          • 2021-10-28
          • 2015-10-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多