如何使此图像具有响应性,同时保持其纵横比。

huangapple go评论63阅读模式
英文:

How to make this image responsive while retaining its aspect ratio

问题

我目前正在使用HTML和CSS设计网站,并希望将这个图片 定位到页面中,如下所示:

如何使此图像具有响应性,同时保持其纵横比。

我需要它具有响应性。

我为此部分所做的是:

HTML:

<div class="intro">
    <img src="https://i.stack.imgur.com/GGDym.png" class="sensei-img">
</div>

CSS:

.intro {
  width: 100vw;
  height: 100vh;
  position: relative;
  -webkit-box-sizing: border-box;
  /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box;
  /* Firefox, other Gecko */
  box-sizing: border-box;
  overflow: overlay;
  display: flex;
  flex-direction: row;
}
.intro .sensei-img {
  position: absolute;
  left: 5vw;
  bottom: -10vh;
  width: 70vw;
  height: 100vh;
}

我可以使用 position:absolute; 完美地定位图像,但似乎无法使其尺寸具有响应性(我希望它在视口更改时保持其宽高比)。我应该怎么做?

英文:

I am currently working on a website design using HTML and CSS and I want to position this image to fit in the page like this:

如何使此图像具有响应性,同时保持其纵横比。

I need it to be responsive.

What I did for its part is:

HTML:

&lt;div class=&quot;intro&quot;&gt;
    &lt;img src=&quot;https://i.stack.imgur.com/GGDym.png&quot; class=&quot;sensei-img&quot;&gt;
&lt;/div&gt;

CSS:

.intro {
  width: 100vw;
  height: 100vh;
  position: relative;
  -webkit-box-sizing: border-box;
  /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box;
  /* Firefox, other Gecko */
  box-sizing: border-box;
  overflow: overlay;
  display: flex;
  flex-direction: row;
}
.intro .sensei-img {
  position: absolute;
  left: 5vw;
  bottom: -10vh;
  width: 70vw;
  height: 100vh;
}

I can position the image perfectly with position:absolute; but I can't seem to be able to make it's size responsive (I want it to retain its ratio even if the viewport changes). What should I do?

答案1

得分: 0

只需删除此属性,即可使其比例保持不变,即使视口发生变化:
.intro .sensei-img {height: 100vh;}

该图像将默认保持其纵横比。

英文:

For its ratio to be kept even if the viewport changes you can simply remove this property:
.intro .sensei-img {height: 100vh;}

The image will keep it's aspect ratio by default.

huangapple
  • 本文由 发表于 2023年3月7日 21:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定