【问题标题】:CSS Media query not working on my browser [closed]CSS媒体查询在我的浏览器上不起作用[关闭]
【发布时间】:2019-03-18 14:00:33
【问题描述】:

我正在使用 CSS 和 flexbox 构建响应式网站,但我的 @media 查询遇到了问题。当 min-width 值达到 992 px 时,媒体查询应该会产生变化。 (即文本应移动到图像的右侧)。我多次查看它并尝试在多个浏览器上运行它,但似乎仍然无法弄清楚。 任何帮助将不胜感激。

//main.css

@import url('https://fonts.googleapis.com/css?family=Roboto+Mono:400,700i');

* {
    margin=0;
    padding=0;
    box-sizing: border-box;
}

body {
    font-family: 'Roboto Mono', monospace;
}

:root {
    --light: #fff;
    --dark: #181b24;
    --grey: #899495;
    --orange: #fc5130;
    --SectionGrey: #f2f2f2;
    --SectionWhite: #fff;
}

/* nav*/
.nav {
    background: var(--light);
}

/*end of nav*/
/*header*/
.header {
    min-height: 100vh;
    background: linear-gradient(rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.3)), url(./img/header-bcg.jpeg)center/cover fixed no-repeat;
    flex-direction: column;

}

/*end of header*/
/*banner*/
.banner {
    /* as a flex child*/
    flex: 1 0 auto;
    /*as a flex parent*/
    display: flex;
    justify-content: center;
    align-items: flex-end;
    color: var(--dark);
    text-align: center;
    padding-bottom: 4rem;
    font-weight: 700;
}

.banner__title {
    font-size: 4em;
    letter-spacing: 0.15rem;
    margin-bottom: 0.5 rem;
    font-style: italic;
    text-transform: capitalize;

}

.banner__jobs {
    letter-spacing: 0.15rem;
    text-transform: capitalize;
    font-size: 1.5rem;
    color: var(--light);
}

/*end of banner*/
/*about section*/
.about {
    padding: 2rem 0;
    background: var(--SectionWhite);

}

.about__center {
    max-width: 90vw;
    margin: 2rem auto;

}

.about-img-photo {
    margin: 3rem 0;
    position: relative;
}

.about-img__photo {
    display: block;
    width: 100%;
    height: 100%;
    position: relative;
}

.about-img:before {
    content="";
    background: var(--grey);
    width: 100%;
    height: 100%;
    position: absolute;
    bottom: -10px;
    left: -10px;
}

.about-info {
    margin: 3rem 0;
}

.about-info__title {
    font-size: 3rem;
    text-transform: capitalize;
    letter-spacing: 0.5rem;
    background: url('./img/title-bcg.svg')no-repeat;
}

.about-info__text {
    color: var(--grey);
    line-height: 1.5;
    font-size: 1.2rem;
    margin: 3rem 0;
}

.about-info__single-link {
    display: inline-block;
    text-decoration: none;
    padding: 0.5rem 1rem;
    font-size: 1.5rem;
    letter-spacing: 0.2rem;
    text-transform: capitalize;
    border: 3px solid var(--dark);
    transition: all 1s linear;
    margin-right: 1rem;

}

.dark-btn {
    background: var(--dark);
    color: var(--light);
}

.dark-btn:hover {
    background: var(--light);
    color: var(--dark);
}

.light-btn {
    color: var(--dark);
}

.light-btn:hover {
    background: var(--dark);
    color: var(--light);
}

@media screen and(min-width:992px) {
    .about__center {
        display: flex;
        justify-content: space-between;
    }

    .about-info,
    .about-img {
        flex: 0 0 calc(50% - 1rem);
    }

    .about-info {
        display: flex;
        align-items: flex-end;
    }
}

/*end of about section*/

//index.html

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Portfolio</title>

    <!--main css-->
    <link rel="stylesheet" href="main.css">
    <!--font awewsome-->
    <script src="all.js"></script>
</head>

<body>
    <header class="header">
        <!--first child-->
        <nav class="nav"></nav>
        <!--second child-->
        <section class="banner">
            <!--banner container-->
            <div class="banner__container">
                <h1 class="banner__title">I'm a clever techie</h1>
                <h1 class="banner__jobs">developer<span><i class="fas fa-angle-double-right"></i></span>
                    designer
                    <span><i class="fas fa-angle-double-right"></i></span>
                    photographer</h1>
            </div>
        </section>
    </header>
    <!--end header-->
    <!--about section-->
    <section class="about"></section>
    <div class="about__center">
        <article class="about-img">
            <img src="img/header-bcg.jpeg" alt="" class="about-img__photo">
        </article>
        <article class="about-info">
            <div class="about-info__container">
                <h1 class="about-info__title">about me</h1>
                <p class="about-info__text">hah ahdh hqdh hqdhw hqdh hqfh hqfh hwfhdvh hwfdh whfb</p>
                <div class="about-info__links">
                    <a href="#" class="about-info__single-link dark-btn">hire me</a>
                    <a href="#" class="about-info__single-link light-btn">download CV</a>
                </div>
            </div>

        </article>

    </div>
    <!--end of about section-->

    <script src="script.js"></script>
</body>

</html>

展开前的截图

浏览器窗口展开后

预期输出

【问题讨论】:

  • 你能提供你看到的截图吗?
  • 是的,将它们与预期输出一起添加到我的帖子中。谢谢。

标签: html css flexbox media-queries webpage


【解决方案1】:

'and' 和括号 '(' 之间没有空格,这就是它破坏 css 的原因。如下所示放置一个空格,它应该可以与您自己的代码一起使用:

以前@media screen and(min-width:992px) {

已更正@media screen and (min-width:992px) {

【讨论】:

    【解决方案2】:

    我已经检查了您的代码并进行了一些更改:

    • 我只使用了“@media”而不是“@media screen and...”
    • 我使用了“最大宽度”而不是“最小宽度”。

    希望对你有帮助!

    @import url('https://fonts.googleapis.com/css?family=Roboto+Mono:400,700i');
    
    * {
        margin=0;
        padding=0;
        box-sizing: border-box;
    }
    
    body {
        font-family: 'Roboto Mono', monospace;
    }
    
    :root {
        --light: #fff;
        --dark: #181b24;
        --grey: #899495;
        --orange: #fc5130;
        --SectionGrey: #f2f2f2;
        --SectionWhite: #fff;
    }
    
    /* nav*/
    .nav {
        background: var(--light);
    }
    
    /*end of nav*/
    /*header*/
    .header {
        min-height: 100vh;
        background: linear-gradient(rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.3)), url('https://killaexpeditions.com/wp-content/uploads/2018/03/inca-trail-machu-picchu-header.jpg')center/cover fixed no-repeat;
        flex-direction: column;
    
    }
    
    /*end of header*/
    /*banner*/
    .banner {
        /* as a flex child*/
        flex: 1 0 auto;
        /*as a flex parent*/
        display: flex;
        justify-content: center;
        align-items: flex-end;
        color: var(--dark);
        text-align: center;
        padding-bottom: 4rem;
        font-weight: 700;
    }
    
    .banner__title {
        font-size: 4em;
        letter-spacing: 0.15rem;
        margin-bottom: 0.5 rem;
        font-style: italic;
        text-transform: capitalize;
    
    }
    
    .banner__jobs {
        letter-spacing: 0.15rem;
        text-transform: capitalize;
        font-size: 1.5rem;
        color: var(--light);
    }
    
    /*end of banner*/
    /*about section*/
    .about {
        padding: 2rem 0;
        background: var(--SectionWhite);
    
    }
    
    .about__center {
        max-width: 90vw;
        margin: 2rem auto;
    
    }
    
    .about-img-photo {
        margin: 3rem 0;
        position: relative;
    }
    
    .about-img__photo {
        display: block;
        width: 100%;
        height: 100%;
        position: relative;
    }
    
    .about-img:before {
        content="";
        background: var(--grey);
        width: 100%;
        height: 100%;
        position: absolute;
        bottom: -10px;
        left: -10px;
    }
    
    .about-info {
        margin: 3rem 0;
    }
    
    .about-info__title {
        font-size: 3rem;
        text-transform: capitalize;
        letter-spacing: 0.5rem;
        background: url('./img/title-bcg.svg')no-repeat;
    }
    
    .about-info__text {
        color: var(--grey);
        line-height: 1.5;
        font-size: 1.2rem;
        margin: 3rem 0;
    }
    
    .about-info__single-link {
        display: inline-block;
        text-decoration: none;
        padding: 0.5rem 1rem;
        font-size: 1.5rem;
        letter-spacing: 0.2rem;
        text-transform: capitalize;
        border: 3px solid var(--dark);
        transition: all 1s linear;
        margin-right: 1rem;
    
    }
    
    .dark-btn {
        background: var(--dark);
        color: var(--light);
    }
    
    .dark-btn:hover {
        background: var(--light);
        color: var(--dark);
    }
    
    .light-btn {
        color: var(--dark);
    }
    
    .light-btn:hover {
        background: var(--dark);
        color: var(--light);
    }
    
    @media(max-width:992px) {
        .about__center {
            display: flex;
            justify-content: space-between;
        }
    
        .about-info,
        .about-img {
            flex: 0 0 calc(50% - 1rem);
        }
    
        .about-info {
            display: flex;
            align-items: flex-end;
        }
    }
    
    /*end of about section*/
    <!DOCTYPE html>
    <html lang="">
    
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
        <title>Portfolio</title>
    
        <!--main css-->
        <link rel="stylesheet" href="main.css">
        <!--font awewsome-->
        <script src="all.js"></script>
    </head>
    
    <body>
        <header class="header">
            <!--first child-->
            <nav class="nav"></nav>
            <!--second child-->
            <section class="banner">
                <!--banner container-->
                <div class="banner__container">
                    <h1 class="banner__title">I'm a clever techie</h1>
                    <h1 class="banner__jobs">developer<span><i class="fas fa-angle-double-right"></i></span>
                        designer
                        <span><i class="fas fa-angle-double-right"></i></span>
                        photographer</h1>
                </div>
            </section>
        </header>
        <!--end header-->
        <!--about section-->
        <section class="about"></section>
        <div class="about__center">
            <article class="about-img">
                <img src="https://killaexpeditions.com/wp-content/uploads/2018/03/inca-trail-machu-picchu-header.jpg" alt="" class="about-img__photo">
            </article>
            <article class="about-info">
                <div class="about-info__container">
                    <h1 class="about-info__title">about me</h1>
                    <p class="about-info__text">hah ahdh hqdh hqdhw hqdh hqfh hqfh hwfhdvh hwfdh whfb</p>
                    <div class="about-info__links">
                        <a href="#" class="about-info__single-link dark-btn">hire me</a>
                        <a href="#" class="about-info__single-link light-btn">download CV</a>
                    </div>
                </div>
    
            </article>
    
        </div>
        <!--end of about section-->
    
        <script src="script.js"></script>
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 1970-01-01
      • 2011-09-28
      • 2013-04-24
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多