【问题标题】:Why is it that my media queries not working in my code?为什么我的媒体查询在我的代码中不起作用?
【发布时间】:2019-10-15 20:39:45
【问题描述】:

我对 html css 还很陌生,并开始构建一个登陆页面,我是从下面显示的前端导师挑战中获得的。到目前为止,它在桌面上看起来不错。

Coming soon landing page

但是,我无法让它响应。我试图将网格模板列更改为行,但它不起作用。这是为什么呢?

这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/style.css">
    <title>Document</title>
</head>
<body>
    <nav class="top-img">
        <div class="container"> 
    <img src="./images/logo.svg" class="navbar-brand" alt="#">
</div>
</nav>

<!-- Section hero-->
<section id="hero">
    <div class="grid">
        <div class="hero-text">
            <h1>We're<span> Coming Soon</span></h1>
            <p>Hello fellow shoppers! We're currently building our new fashion store. Add your email below to stay up-to-date with announcements and or launch deals.</p>
            <div class="icons">
                <i class="fas fa-chevron-right"></i>
            <input type="text" placeholder="Email Address">
        </div>
        </div>
        <div class="hero-img">
            <img src="./images/hero-desktop.jpg" alt="#">
        </div>
    </div>
</section>

<script src="https://kit.fontawesome.com/732aafddc7.js" crossorigin="anonymous"></script>
</body>
</html>

@import url('https://fonts.googleapis.com/css?family=Josefin+Sans:300,400,600&display=swap');  

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

:root {

    /* primary */
    --desaturated-color: hsl(0, 36%, 70%);
    --soft-red: hsl(0, 93%, 68%);
    /*neutral*/
    --dark-red: hsl(0, 6%, 24%);
    /* font size - body: 16px */
    --body-font: 16px;

}

.container {
    /*max-width: 1200px;*/
    padding: 2rem 3rem;
}



#hero {
    background: url(../images/bg-pattern-desktop.svg) no-repeat center center/cover;
    height: 100vh;
    position: relative;
    overflow: auto;
}

.navbar-brand {
    padding-left: 3rem;
}

.hero-text {
    width: 60%;
    display: block;
    padding-top: 3rem;
    margin-top: 10rem;
    margin-left: 6rem;
}

.hero-text h1 {
    text-transform: uppercase;
    color: var(--desaturated-color);
    font-size: 3.5rem;
    font-family: 'Josefin Sans', sans-serif;
    font-weight: 300;
    letter-spacing: 20px;
    margin-bottom: 1.5rem;
}

.hero-text p {
      font-size: var(--body-font);
      color: var(--desaturated-color);
     margin-bottom: 2rem;
     line-height: 2;
}
.hero-text span {
    color: var(--dark-red);
    font-weight: 600;
}

.top-img {
    position: absolute;
    z-index: 1;
}

.hero-img img {
     width: 100%;
     height: 100vh;
}

.grid {
    display: grid;
    grid-template-columns: 3fr 2fr;

}

input[type=text] {
    width: 100%;
    padding: 1rem;
    border-radius: 25px;
    border: 1px solid var(--desaturated-color);
    border-color: var(--desaturated-color);
}

::placeholder {
    color: var(--desaturated-color);
}

.icons {
    position: relative;
}

.icons i {
    position: absolute;
    top: 1px;
    right: 0px;
    color: #fff;
    background: linear-gradient(135deg,  hsl(0, 80%, 86%),  hsl(0, 74%, 74%));
    padding: 0.5rem 3rem;
     height: 100%;
    font-size: 1.5rem;
    border-radius: 25px;
    display: flex;
    align-items: center;
}

@media (max-width: 768px) {
    .grid {
        grid-template-rows: 3fr 2fr; 
    }
}


【问题讨论】:

    标签: html css responsive-design media-queries


    【解决方案1】:

    您在媒体查询之外指定grid-template-columns;因此,它适用于所有屏幕尺寸。要解决此问题,您只需在媒体查询中指定您不希望内容显示在列中 - 然后您当前的 grid-template-rows 元素将正常工作。

    见下文:

    @media (max-width: 768px) {
        .grid {
            grid-template-columns: none; /* ADD THIS */
            grid-template-rows: 3fr 2fr; 
        }
    }

    JSFiddle

    【讨论】:

      【解决方案2】:

      在媒体查询中,在 grid-template-rows 下添加这一行:

      网格模板列:无;

      您必须覆盖指定的列,否则它也将应用于响应版本。

      【讨论】:

        【解决方案3】:

        请输入 grid-template-columns: none;

        @media (max-width: 768px) {
         .grid {
            grid-template-rows: 3fr 2fr; 
            grid-template-columns: none; 
         }
        }
        

        或者另一种用户展示方式:block;对于移动版本。

         @media (max-width: 768px) {
         .grid {
            display: block;
         }
        }
        

        【讨论】:

          【解决方案4】:

          在响应中首先重置网格列然后应用行它将起作用

          【讨论】:

            猜你喜欢
            • 2021-10-18
            • 2016-07-29
            • 2015-04-03
            • 2021-03-02
            • 2019-08-07
            相关资源
            最近更新 更多