【问题标题】:Media query on image图片媒体查询
【发布时间】:2022-01-11 03:03:11
【问题描述】:

max-width:1220px 时,我想将main 转换为display:column。 我正在这样做

@media (max-width: 1220px) {
  .main {
    flex-direction: column;
  }

但是当宽度为 1220 像素时,图像消失了。 我做错了什么?

这是更简单的代码笔链接。

https://codepen.io/Rvssco/pen/LYzGYgV

提前谢谢你!

【问题讨论】:

  • 它没有尺寸——它是什么的 100%?当事物在列中时,无需设置高度。试着给它一些明确的尺寸,你会看到的。
  • 尝试给div.right一些高度并从div.wrap移除height: 98vh;
  • @AHaworth 谢谢!!

标签: html css image media-queries


【解决方案1】:

图像正在消失,因为您在 CSS 而不是 html 中包含了图像的 src,当改变方向时 flex-direction 无法检测到图像的存在,因此图像不会出现。你可以看看我的代码我是怎么做的。

body {
  background-color: hsl(233, 47%, 7%);
  color: white;
}

h1,
p {
  margin: 0;
}

h1 {
  font-family: "Lexend Deca", sans-serif;
}

.desc {
  font-family: "Inter", sans-serif;
  font-weight: 400;
  color: hsla(0, 0%, 100%, 0.75);
}

span {
  color: hsl(277, 64%, 61%);
}

.wrap {
  height: 98vh;
  width: 98vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

.main {
  display: flex;
  width: 85%;
  height: 80%uto;
  border-radius: 10px;
  background-color: hsl(244, 38%, 16%);
  flex-direction: row;
}

.left {
  padding: 5rem;
  padding-right: 0;
  display: flex;
  flex-direction: column;
  width: 50%;
}

.text-container .title {
  margin-bottom: 2rem;
  font-size: 2.5rem;
  font-weight: 600;
}

.desc {
  margin-bottom: 5rem;
  line-height: 1.5rem;
  word-spacing: 0.3rem;
  font-family: "Inter", sans-serif;
}

.stats-desc {
  font-family: "Inter", sans-serif;
  color: hsla(0, 0%, 100%, 0.6);
}

.stats-container {
  display: flex;
  justify-content: space-between;
  font-size: 15px;
  width: 70%;
}

.right {
  width: 50%;
  /* background-image: url(./1.png); */
  background-size: cover;
  background-repeat: no-repeat;
}

.stats h1 {
  font-family: "Inter", sans-serif;
}

.filter {
  height: 100%;
  width: 100%;
  background-color: hsla(277, 88%, 32%, 0.603);
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}



  .left {
    text-align: center;
  }


@media (max-width: 1000px) {
  .main {
    /* display: block; */  /* Option 2*/
    flex-direction: column-reverse;
    background-color:red;  /*Used for testing please remove*/
  }

  .left {
    text-align: center;
  }

/*Small modification in filer and right classes*/
  .filter {
  height: 100%;
  width: 100%;
  background-color: hsla(277, 88%, 32%, 0.603);
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
}

  .right {
  height: 50%;
  width: 100%;
  background-size: cover;
  background-repeat: no-repeat;
}
} 
<div class="wrap">
      <div class="main" role="main" >
        <div class="left">
          <div class="text-container">
            <h1 class="title">
              Get <span>insights</span> that help your business grow.
            </h1>
          </div>
          <p class="desc">
            Discover the benefits of data analytics and make better decisions
            regarding revenue, customer experience, and overall efficiency.
          </p>
          <div class="stats-container">
            <div class="stats">
              <h1>10k+</h1>
              <p class="stats-desc">COMPANIES</p>
            </div>
            <div class="stats">
              <h1>314</h1>
              <p class="stats-desc">TEMPLATES</p>
            </div>
            <div class="stats">
              <h1>12M+</h1>
              <p class="stats-desc">QUERIES</p>
            </div>
          </div>
        </div>
        <div class="right">
            <img  class="filter" src= ./1.png>
        </div>
      </div>
    </div>

我不知道这背后的确切原因,可能有更深入的知识的人可以发表评论。

如果您仍想使用 CSS 中的 url,您仍然可以使用它,但要更改

flex-direction:column 

diplay:block

其次,当您使用媒体查询时,您需要重新定义用于图像及其显示的类(.filter 和 .right)。否则更改将不会应用于它们,这是我观察到的明显错误。

此外,您还重新定义了媒体查询两次,并且 CSS 中使用 {} 括号有误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2019-03-17
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2022-01-25
    • 2015-09-02
    相关资源
    最近更新 更多