html - 通过 CSS 调整背景大小

标签 html css

我已经尝试了一些方法,但还是不行

这是一个简单的代码:

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Convite</title>
<style>
body {
background-image: url("bgg.jpg");   
}

@media screen and (max-width : 480px) {
    background-image: url("bgg.jpg");
    background-size: auto; 480px;


    .1 {height:300px;
        width:480px;
         }  
    .2 {height:107px;
        width:480px;
         }
    .3 {height:34px;
        width:480px;
         }
}

</style>
</head>
<body>
<br><br>
    <center>
        <span class="1"><img src="1.png"></span>
        <br><br>
        <span class="2"><img src="2.png"></span>
        <br><br>
        <span class="3"><img src="3.png"></span>
    </center>
</body>
</html>

所以我有 4 个元素(3 个图像和 1 个背景) 背景的原始尺寸为 1280 x 720p 我想按 % 调整元素的大小,但它仍然不起作用 视口(viewport)不工作

我做错了什么?

最佳答案

您的 CSS 和 HTML 有很多问题。这是固定版本。

错误如下:

  1. 缺少 DOCTYPE
  2. 使用数字作为 CSS 类的前缀
  3. 滥用 <br>调整布局
  4. 媒体查询语法不正确
  5. 图片标签中缺少 Alt
  6. 使用过时的 <center>标签
  7. 错误使用<span>标签,这是一个内联元素。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Convite</title>
<style>
body {
    background-image: url("bgg.jpg");   
}
.center {
    margin: auto;
}

@media screen and (max-width : 480px) {
    body {
        background-image: url("bgg.jpg"); /* this line is redundant */
        background-size: cover;
    }

    .foo1 {
        height:300px;
        width:480px;
    }  
    .foo2 {
        height:107px;
        width:480px;
    }
    .foo3 {
        height:34px;
        width:480px;
    }
}

</style>
</head>
<body>
    <div class="center">
        <div class="foo1"><img src="1.png" alt="" /></div>
        <div class="foo2"><img src="2.png" alt="" /></div>
        <div class="foo3"><img src="3.png" alt="" /></div>
    </div>
</body>
</html>

最后,如果你要做响应式网站,请不要指定固定的高度和宽度。

关于html - 通过 CSS 调整背景大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49972228/

相关文章:

html - CSS 网格 - 在 2 个水平 div 之间自动填充

html - 如何将一个 div 显示为由另一个 div 内的 div 悬停的叠加层?

javascript - Bootstrap - 创建 100 列网格,与圆形结合

css - 警告 : copy(): Unable to find the wrapper &quot;. .file"

javascript - IE7 和 IE8 中 div 高度变化的极其奇怪的行为

html - 如何创建具有不寻常结构的表(html、css、js?)

javascript - 如何在有过渡和无过渡的情况下实现相同的 Action ?

javascript - 当参数包含 html 标记时 Ajax 调用失败

html - 鼠标悬停圆圈 HTML5 Canvas

javascript - 通过 js 或 css 悬停?