【发布时间】:2021-11-13 01:46:33
【问题描述】:
昨天有人向我提供了一个关于如何维护一个永远不会溢出其视口的响应框的答案。
CSS responsive box that maintains aspect ratio but NEVER overflows the viewport
但是我从来没有遇到过另一个问题,我试图在容器中放置一个图像,不幸的是,所有尝试使用 object-fit:、width: & max- 的变体来调整图像大小以适应动态容器宽度:,不工作。
有人可以帮助我如何放置包含在响应式容器中并占据 100% 宽度的图像。提前致谢 - CES
:root{
--varWidth: 80vw;
--varHeight: calc(80vh + 3.5vh);
}
.aspectRatio3x2 {
--varAspectRatio: calc( 3 / 2);
--varCalcHeight: min(calc(var(--varWidth) / var(--varAspectRatio)), var(--varHeight));
height: var(--varCalcHeight);
width: calc(var(--varCalcHeight) * var(--varAspectRatio));
background-color: gold;
}
.imgMain, .imgOther {
object-fit: contain;
width:100%;
max-width:100%;
border: 2px solid hsla(0, 0%, 100%, .15);
}
.imgMain{
border-bottom: none;
}
/* Other CSS */
.contentContainer {
width: 100%;
height: auto;
padding: min(4vh, 3.5vw);
background: hsl(0 0% 10%);
background:red;
}
.pageDefault {
width:100%;
background-color: black;
}
.pageDefault article {
position: relative;
margin: 0 auto;
background: lime;
}
<div class="contentContainer">
<main id="idMainContent" class="pageDefault">
<article class="aspectRatio3x2">
<figure class="">
<picture class="">
<img class="imgMain" src="../../images/3x2.jpg" />
</picture>
<figcaption></figcaption>
</figure>-->
</article>
</main>
</div>
【问题讨论】:
-
你能把你需要的图片添加到你的sn-p吗?
-
首先感谢您昨天的回答,我已经找了好几个星期了。不知道你的意思,但图片的网址是:-evansante.com/images/portfolio/mainbnw.jpg
-
谢谢,图片的纵横比是 16 / 9 而不是 3 / 2
-
我已经包含了上面的 3/2 或者你可以去链接...抱歉造成混淆
标签: css image responsive-design