响应式CSS @media查询

时间:2016-05-21 06:33:48

标签: html css media responsive

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

@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即将到来,而其他两个查询无效。即使我尝试使用最小宽度: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;
    }
}

4 个答案:

答案 0 :(得分:1)

这是错误的代码.. @media screen和(min-width:320px){ 此代码将在320px及更高版本的屏幕上运行 如果你把代码放在这里当然这个代码将运行在480px和680px }

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

答案 1 :(得分:0)

尝试打击代码:

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

答案 2 :(得分:0)

试试这个。

<!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 :(得分:0)

(编辑说明:更正了一些拼写错误和错误)

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

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

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

所以你有两个选择:

1。)转动订单:

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

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

作为替代方案:

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

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

这是一种移动优先方法:首先列出移动规则(即至少 320px宽的所有内容),然后再进一步提升。因此,360px设备将在第二次媒体查询后停止读取规则,桌面计算机将读取所有这三个规则,并且第三个规则中的规则将覆盖第一个查询中的规则。

相关问题