html - 背景图片、手机和媒体查询

标签 html css media-queries

我正在努力让它响应手机。我有无法正确调整大小的背景图片。我试过媒体查询但不知道为什么它不起作用。我的代码如下;

.container3{
  display: flex;
  align-items: center;
  background-size: cover;
  height: 700px;
  text-shadow: 0.25px 0.25px 0.25px #000000;
  background-image: url('Images/homepage.jpg');
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
}

@media all and (max-width:480px)and(min-width:0px) {
  .container3 {
    width: 100%;
    background-image: url('Images/homepage.jpg');
  }
}
<html>	
  <title>Unity</title>
  <link href="Style.css" rel="stylesheet"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
  <meta name="viewport" content="width=500, initial-scale=1">
  <body>

    <div class="container3">
      <div class="title">
        <h1>
          bringing people together
        </h1>
        <p>free advertising space</p>

      </div>	
    </div>


  </body>
</html>

最佳答案

.container3{
  display: flex;
  align-items: center;
  background-size: cover;
  height: 700px;
  text-shadow: 0.25px 0.25px 0.25px #000000;
  background-image: url('Images/homepage.jpg');
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
}

@media all and (max-width:888px) {
  .container3 {
    width: 100%;
    background-image: url('http://feelgrafix.com/data/background/background-17.jpg');
  }
}
<html>	
  <title>Unity</title>
  <link href="Style.css" rel="stylesheet"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
  <meta name="viewport" content="width=500, initial-scale=1">
  <body>

    <div class="container3">
      <div class="title">
        <h1>
          bringing people together
        </h1>
        <p>free advertising space</p>

      </div>	
    </div>


  </body>
</html>

它不起作用,因为语法错误,你应该在@media all and (max-width:480px)and(min-width:0px)中使用空格。像这样:@media all and (max-width:480px) and (min-width:0px) 还有,它是多余的,因为设备的宽度总是大于 0px,所以你可以删除第二部分。

我使用了 max-width:888px,因此您可以在代码段中看到示例 img。

关于html - 背景图片、手机和媒体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347900/

相关文章:

javascript - js 从模态中抓取类

javascript - 如何一一获取目录下的文件链接并使用?

html - 过渡不适用于边框图像和渐变

css - 添加解析条件时我的媒体查询不起作用,我该怎么办?

css - CSS 媒体查询有多慢?

ios - CSS - iPhone 6 上未应用高度

java - JSTL : Why my data are not being printed in JSP?

html - 我为 div 元素添加 tabIndex 并为其添加 onblur 属性。它在我的笔记本电脑上运行良好,但在移动设备上无法运行

html - 为什么文本内容会影响表格单元格的宽度?

javascript - 在 IE 中隐藏第二个垂直滚动条?