gpt4 book ai didi

html - 图像在容器内不合适并且图像切换没有响应(响应性)

转载 作者:行者123 更新时间:2023-11-27 23:40:04 27 4
gpt4 key购买 nike

我正在尝试使用 index.html 和 style.css 文件制作响应式网站。

我在类(class)照片下有 2 张图片(检查 html 中的 div class="photo"代码)。

看看我的两张图片

(注意像素的差异和不同的文本图像是相同的)

当我在 Google chrome 中运行我的程序时,名为 photo-big.jpg 的图像无法正确放入容器中(div class="section header")为什么?

查看它应该适合的容器:

enter image description here

看到图像不适合容器:

enter image description here

当我将浏览器尺寸更改为小于 960 像素时,名为 photo-small.jpg 的图像应该会加载但它不会加载,但问题是photo-big resize itself some 我不知道为什么会这样?

查看图像在容器内自行调整大小

如果可能,请详细解释一下。

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

.page {
display: flex;
flex-wrap: wrap;
}

.section {
width: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}

.menu {
background-color: black;
height: 80px;
}

.header {
background-color: #b2d6ff;
}

.content {
background-color: #eaedf0;
height: 600px;
}

.sign-up {
background-color: #d6e9fe;
}

.feature-1 {
background-color: #f5cf8e;
}

.feature-2 {
background-color: #f09a9d;
}

.illustration img {
display: block;
width: 100%;
}

.feature-3 {
background-color: #c8c6fa;
}

.header {
height: auto;
justify-content: inherit;
align-items: inherit;
}

.photo img {
width: 100%;
display: block;
}

@media only screen and (max-width: 400px) {
body {
background-color: #f09a90;
}
}

@media only screen and (min-width: 401px) and (max-width: 960px) {
body {
background-color: #f5cf8e;
}
.sign-up,
.feature-1,
.feature-2,
.feature-3 {
width: 50%;
}
}

@media only screen and (min-width: 961px) {
body {
background-color: #b2d6ff;
}
.page {
width: 960px;
margin: 0 auto;
}
.feature-1,
.feature-2,
.feature-3 {
width: 33.3%;
}
.header {
height: 400px;
}
.sign-up {
height: 200px;
order: 1;
}
.content {
order: 2;
}
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Responsive Desing</title>
<link rel="stylesheet" href="style.css" />
</head>

<body>
<div class="page">
<div class="section menu"></div>

<div class="section header">
<div class="photo">
<img src="images/photo-small.jpg" srcset="images/photo-small.jpg 1000w, images/photo-big.jpg 2000w" sizes="(min-width:960 px) 960px,100vw" />
</div>
</div>

<div class="section content">
<div class="illustration">
<img src="images/illustration-small.png" srcset="
images/illustration-small.png 1x,
images/illustration-big.png 2x
" style="max-width: 500px" />
</div>
</div>
<div class="section sign-up">
<img src="images/sign-up.svg" />
</div>
<div class="section feature-1">
<img src="images/feature.svg" />
</div>
<div class="section feature-2">
<img src="images/feature.svg" />
</div>
<div class="section feature-3">
<img src="images/feature.svg" />
</div>
</div>
</body>

</html>

最佳答案

来自 object-fit on MDN :

cover The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.

你需要的新样式是

.header img {
object-fit: cover;
}

关于html - 图像在容器内不合适并且图像切换没有响应(响应性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57078203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com